一个朋友经常碰到要将大批量的照片输出到写真机,就不停的将图片加载进photoshop,然后排版,效率够慢的,便写个脚本把这些工作简化了
config.txt配置信息
1 item_hspace=0.5
2 item_vspace=0.5
3 item_width = 20 #15cm
4 item_height = 15
5 DPI=96
6 canvas_heigth=88 #200cm
7 canvas_rotation=90
8 source = D:\共享\2010.9.20\小天鹅幼儿园小4班照片
9 outfile=final2.tiff
main.py处理代码
1 #-*- coding:utf-8 -*-
2 #
3 #name: config.py
4 #brief:
5 # 系统配置文件
6 #author: scott
7 #
8
9
10 import os,sys,time,os.path,traceback
11 import zlib,binascii
12 import PIL,Image
13 import config
14
15 DPI=96.0
16
17 POINTS_PER_METER = DPI/2.54 * 100 #每米多少个像素
18 POINT_PER_CMETER = DPI/2.54
19
20 ITEM_HSPACE=0
21 ITEM_VSPACE=1
22 ITEM_WIDTH =0 #照片的宽度
23 ITEM_HEIGTH = 0 #照片宽度决定了高度
24 CANVAS_HEIGHT= 0 #画板高度,长度是无限制的
25 CANVAS_ROTATION = -90 #最终输出照片进行旋转输出
26
27 conf = config.SimpleConfig()
28 conf.open('config.txt')
29 DPI = conf.getPropertyValueAsFloat('DPI',96.0)
30 ITEM_HSPACE = conf.getPropertyValueAsFloat('item_hspace',0.5) * POINT_PER_CMETER
31 ITEM_VSPACE = conf.getPropertyValueAsFloat('item_vspace',0.5) * POINT_PER_CMETER
32 ITEM_WIDTH = conf.getPropertyValueAsFloat('item_width',15) * POINT_PER_CMETER
33 ITEM_HEIGHT = conf.getPropertyValueAsFloat('item_height',10) * POINT_PER_CMETER
34
35 CANVAS_HEIGHT = conf.getPropertyValueAsFloat('canvas_heigth',200) * POINT_PER_CMETER
36 CANVAS_ROTATION = conf.getPropertyValueAsFloat('canvas_rotation',-90)
37
38 ITEM_HSPACE = int(ITEM_HSPACE)
39 ITEM_VSPACE = int(ITEM_VSPACE)
40 ITEM_WIDTH = int(ITEM_WIDTH)
41 ITEM_HEIGHT = int(ITEM_HEIGHT)
42 CANVAS_HEIGHT = int( CANVAS_HEIGHT)
43
44 print conf.getPropertyValueAsFloat('canvas_rotation',-90)
45
46 rows = CANVAS_HEIGHT / (ITEM_HEIGHT+ITEM_VSPACE)
47
48 source = conf.getPropertyValue('source')
49 final = conf.getPropertyValue('outfile')
50
51 files = []
52
53 imgsize=[0,0]
54
55 for f in os.listdir(source):
56 if os.path.isdir( "%s/%s"%(source,f)):
57 continue
58 try:
59 imgfile = Image.open("%s/%s"%(source,f))
60 size = imgfile.size
61 if size[0]<size[1]:
62 imgfile = imgfile.rotate(90)
63 imgfile.save("%s/%s"%(source,f))
64 except:
65 continue
66 files.append( "%s/%s"%(source,f))
67 #计算输出文件的大小
68 cols = len(files)/(rows)
69 cols = int(cols)
70 if len(files)%(rows) != 0:
71 cols +=1
72
73 CANVAS_WIDTH = cols * (ITEM_VSPACE+ITEM_WIDTH)
74 imgsize = (CANVAS_WIDTH,CANVAS_HEIGHT)
75 imgcanvas = Image.new('RGB',imgsize,0xffffff)
76
77 print CANVAS_WIDTH,CANVAS_HEIGHT
78 print cols,rows
79
80 for c in range(cols):
81 x = c * (ITEM_WIDTH+ITEM_HSPACE)
82 for r in range( int(rows)):
83 y = r * (ITEM_HEIGHT+ITEM_VSPACE)
84 if len(files):
85 file = files[0]
86 files.remove(file)
87 try:
88 print 'open file:',file
89 imgfile = Image.open(file)
90 print (x,y,x+ITEM_WIDTH,y+ITEM_HEIGHT)
91 imgfile = imgfile.resize((ITEM_WIDTH,ITEM_HEIGHT))
92 imgcanvas.paste(imgfile,(x,y,x+ITEM_WIDTH,y+ITEM_HEIGHT))
93
94 #imgcanvas.paste(imgfile,(x,y))
95 except:
96 print traceback.print_exc()
97 #sys.exit()
98 if CANVAS_ROTATION:
99 print CANVAS_ROTATION
100 imgcanvas = imgcanvas.rotate(CANVAS_ROTATION)
101 pass
102
103 imgcanvas.save(final)
104
105