1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
| """ 照片尺寸,宽*高(单位:像素) 1寸照片:295*413 2寸照片:413*626 5寸照片(横版):1500*1050 6寸照片(横版):1800*1200 """ from PIL import Image,ImageDraw
WIDTH_1IN = 295 HEIGHT_1IN = 413
WIDTH_2IN = 413 HEIGHT_2IN = 626
WIDTH_5IN = 1500 HEIGHT_5IN = 1050
WIDTH_6IN = 1950 HEIGHT_6IN = 1300
def cut_photo(photo,choice): """ 将照片按照比例进行裁剪成1寸、2寸 :param photo: 待处理的照片 :param choice: <int> 1表明1寸,2表明2寸 :return: 处理后的照片 """ width = photo.size[0] height = photo.size[1] rate = height / width if choice == 1: if rate < (HEIGHT_1IN/WIDTH_1IN): x = (width - int(height / HEIGHT_1IN * WIDTH_1IN)) / 2 y = 0 cutted_photo = photo.crop((x, y, x + (int(height / HEIGHT_1IN * WIDTH_1IN)), y + height))
else: x = 0 y = (height - int(width / WIDTH_1IN * HEIGHT_1IN)) / 2 cutted_photo = photo.crop((x, y, x + width, y + (int(width / WIDTH_1IN * HEIGHT_1IN)))) return cutted_photo
if choice == 2: if rate < (HEIGHT_2IN/WIDTH_2IN): x = (width - int(height / HEIGHT_2IN * WIDTH_2IN)) / 2 y = 0 cutted_photo = im.crop((x, y, x + (int(height / HEIGHT_2IN * WIDTH_2IN)), y + height))
else: x = 0 y = (height - int(width / WIDTH_2IN * HEIGHT_2IN)) / 2 cutted_photo = im.crop((x, y, x + width, y + (int(width / WIDTH_2IN * HEIGHT_2IN))))
return cutted_photo
def resize_photo(photo,choice): ''' 缩放照片 :param photo: 待处理的照片 :param choice: <int> 1表明1寸,2表明2寸 :return: 处理后的照片 ''' if choice == 1: resized_photo = photo.resize((WIDTH_1IN,HEIGHT_1IN)) return resized_photo if choice == 2: resized_photo = photo.resize((WIDTH_2IN, HEIGHT_2IN)) return resized_photo
def layout_photo_5_1(photo): """ 在5寸照片上排版1寸照片 :param photo: 待处理照片1寸 :return: 处理后的照片 """ bk = Image.new("RGB", [WIDTH_5IN,HEIGHT_5IN], (255,255,255)) draw = ImageDraw.Draw(bk) draw.line([(0,HEIGHT_5IN/2),(WIDTH_5IN,HEIGHT_5IN/2)],fill=128) draw.line([(WIDTH_5IN*0.25,0),(WIDTH_5IN*0.25,HEIGHT_5IN)],fill=128) draw.line([(WIDTH_5IN*0.5,0),(WIDTH_5IN*0.5,HEIGHT_5IN)],fill=128) draw.line([(WIDTH_5IN*0.75,0),(WIDTH_5IN*0.75,HEIGHT_5IN)],fill=128)
focus_point = [0.125 * WIDTH_5IN,0.25 * HEIGHT_5IN] start_point = [focus_point[0] - 0.5 * WIDTH_1IN, focus_point[1] - 0.5 * HEIGHT_1IN] for i in range(0,2): for k in range(0,4): bk.paste(photo, (int(start_point[0] + (k * WIDTH_5IN / 4)), int(start_point[1] + 0.5 * i * HEIGHT_5IN))) return bk
def layout_photo_5_2(photo): """ 在5寸照片上排版2寸照片 :param photo: 待处理照片2寸 :return: 处理后的照片 """ bk = Image.new("RGB", [HEIGHT_5IN,WIDTH_5IN], (255,255,255)) draw = ImageDraw.Draw(bk) draw.line([(0,WIDTH_5IN/2),(WIDTH_5IN,WIDTH_5IN/2)],fill=128) draw.line([(HEIGHT_5IN*0.5,0),(HEIGHT_5IN*0.5,WIDTH_5IN)],fill=128) focus_point = [0.25 * HEIGHT_5IN, 0.25 * WIDTH_5IN] start_point = [focus_point[0] - 0.5 * WIDTH_2IN, focus_point[1] - 0.5 * HEIGHT_2IN] for i in range(0,2): for k in range(0,2): bk.paste(photo, (int(start_point[0] + (k * HEIGHT_5IN / 2)), int(start_point[1] + 0.5* i * WIDTH_5IN))) return bk
def layout_photo_5_mix(photo1,photo2): """ 在5寸照片上混合排版1寸、2寸照片 :param photo1: 待处理照片1寸 :param photo1: 待处理照片2寸 :return: 处理后的照片 """ bk = Image.new("RGB", [WIDTH_5IN,HEIGHT_5IN], (255,255,255)) draw = ImageDraw.Draw(bk) draw.line([(0,HEIGHT_5IN/2),(WIDTH_5IN,HEIGHT_5IN/2)],fill=128) draw.line([(WIDTH_5IN*0.25,0),(WIDTH_5IN*0.25,HEIGHT_5IN)],fill=128) draw.line([(WIDTH_5IN*0.5,0),(WIDTH_5IN*0.5,HEIGHT_5IN)],fill=128)
focus_point = [0.125 * WIDTH_5IN,0.25 * HEIGHT_5IN] start_point = [focus_point[0] - 0.5 * WIDTH_1IN, focus_point[1] - 0.5 * HEIGHT_1IN] focus_point2 = [0.75 * WIDTH_5IN, 0.25 * HEIGHT_5IN] start_point2 = [focus_point2[0] - 0.5 * HEIGHT_2IN, focus_point2[1] - 0.5 * WIDTH_2IN]
for i in range(0,2): for k in range(0,2): bk.paste(photo1, (int(start_point[0] + (k * WIDTH_5IN / 4)), int(start_point[1] + 0.5 * i * HEIGHT_5IN)))
bk.paste(photo2,(int(start_point2[0]),int(start_point2[1]))) bk.paste(photo2,(int(start_point2[0]),int(start_point2[1] + 0.5 * HEIGHT_5IN))) return bk
def layout_photo_6_1(photo): """ 在6寸照片上排版2寸照片 :param photo: 待处理照片1寸 :return: 处理后的照片 """ bk = Image.new("RGB", [HEIGHT_6IN,WIDTH_6IN], (255,255,255)) draw = ImageDraw.Draw(bk) draw.line([(0,WIDTH_6IN*0.25),(WIDTH_6IN,WIDTH_6IN*0.25)],fill=128) draw.line([(0,WIDTH_6IN*0.5),(WIDTH_6IN,WIDTH_6IN*0.5)],fill=128) draw.line([(0,WIDTH_6IN*0.75),(WIDTH_6IN,WIDTH_6IN*0.75)],fill=128) draw.line([(HEIGHT_6IN*0.25,0),(HEIGHT_6IN*0.25,WIDTH_6IN)],fill=128) draw.line([(HEIGHT_6IN*0.5,0),(HEIGHT_6IN*0.5,WIDTH_6IN)],fill=128) draw.line([(HEIGHT_6IN*0.75,0),(HEIGHT_6IN*0.75,WIDTH_6IN)],fill=128) focus_point = [0.125 * HEIGHT_6IN, 0.125 * WIDTH_6IN] start_point = [focus_point[0] - 0.5 * WIDTH_1IN, focus_point[1] - 0.5 * HEIGHT_1IN] for i in range(0,4): for k in range(0,4): bk.paste(photo, (int(start_point[0] + (k * HEIGHT_6IN / 4)), int(start_point[1] + i * 0.25 * WIDTH_6IN ))) return bk
def layout_photo_6_2(photo): """ 在6寸照片上排版2寸照片 :param photo: 待处理照片2寸 :return: 处理后的照片 """ bk = Image.new("RGB", [WIDTH_6IN,HEIGHT_6IN], (255,255,255)) draw = ImageDraw.Draw(bk) draw.line([(0,HEIGHT_6IN/2),(WIDTH_6IN,HEIGHT_6IN/2)],fill=128) draw.line([(WIDTH_6IN*0.25,0),(WIDTH_6IN*0.25,HEIGHT_6IN)],fill=128) draw.line([(WIDTH_6IN*0.5,0),(WIDTH_6IN*0.5,HEIGHT_6IN)],fill=128) draw.line([(WIDTH_6IN*0.75,0),(WIDTH_6IN*0.75,HEIGHT_6IN)],fill=128) focus_point = [0.125 * WIDTH_6IN,0.25 * HEIGHT_6IN] start_point = [focus_point[0] - 0.5 * WIDTH_2IN, focus_point[1] - 0.5 * HEIGHT_2IN] for i in range(0,2): for k in range(0,4): bk.paste(photo, (int(start_point[0] + (k * WIDTH_6IN / 4)), int(start_point[1] + 0.5 * i * HEIGHT_6IN))) return bk
def layout_photo_6_mix1(photo1,photo2): """ 在6寸照片上混合排版1寸、2寸照片 :param photo1: 待处理照片1寸 :param photo1: 待处理照片2寸 :return: 处理后的照片 """ bk = Image.new("RGB", [WIDTH_6IN,HEIGHT_6IN], (255,255,255)) draw = ImageDraw.Draw(bk) draw.line([(0,HEIGHT_6IN*0.5),(WIDTH_6IN,HEIGHT_6IN/2)],fill=128) draw.line([(0,HEIGHT_6IN*0.25),(WIDTH_6IN*0.5,HEIGHT_6IN*0.25)],fill=128) draw.line([(0,HEIGHT_6IN*0.75),(WIDTH_6IN*0.5,HEIGHT_6IN*0.75)],fill=128) draw.line([(WIDTH_6IN*0.25,0),(WIDTH_6IN*0.25,HEIGHT_6IN)],fill=128) draw.line([(WIDTH_6IN*0.5,0),(WIDTH_6IN*0.5,HEIGHT_6IN)],fill=128) draw.line([(WIDTH_6IN*0.75,0),(WIDTH_6IN*0.75,HEIGHT_6IN)],fill=128) focus_point = [0.125 * WIDTH_6IN, 0.125 * HEIGHT_6IN] start_point = [focus_point[0] - 0.5 * HEIGHT_1IN, focus_point[1] - 0.5 * WIDTH_1IN] for i in range(0,4): for k in range(0,2): bk.paste(photo1, (int(start_point[0] + (0.25 * k * WIDTH_6IN )), int(start_point[1] + 0.25 * i * HEIGHT_6IN))) focus_point2 = [0.625 * WIDTH_6IN, 0.25 * HEIGHT_6IN] start_point2 = [focus_point2[0] - 0.5 * WIDTH_2IN, focus_point2[1] - 0.5 * HEIGHT_2IN] for i in range(0,2): for k in range(0,2): bk.paste(photo2,(int(start_point2[0] + (0.25 * k * WIDTH_6IN)), int(start_point2[1] + 0.5 * i * HEIGHT_6IN))) bk.show() return bk
def layout_photo_6_mix2(photo1,photo2): """ 在6寸照片上混合排版1寸、2寸照片 :param photo1: 待处理照片1寸 :param photo1: 待处理照片2寸 :return: 处理后的照片 """ bk = Image.new("RGB", [HEIGHT_6IN,WIDTH_6IN], (255,255,255)) draw = ImageDraw.Draw(bk)
draw.line([(350,0),(350,WIDTH_6IN)],fill=128) draw.line([(700,0),(700,WIDTH_6IN)],fill=128)
draw.line([(0,WIDTH_6IN*0.25),(700,WIDTH_6IN*0.25)],fill=128) draw.line([(0,WIDTH_6IN*0.5),(700,WIDTH_6IN*0.5)],fill=128) draw.line([(0,WIDTH_6IN*0.75),(700,WIDTH_6IN*0.75)],fill=128) draw.line([(700,WIDTH_6IN/3),(HEIGHT_6IN,WIDTH_6IN/3)],fill=128) draw.line([(700,WIDTH_6IN*2/3),(HEIGHT_6IN,WIDTH_6IN*2/3)],fill=128)
focus_point = [0.5 * 350, 0.125 * WIDTH_6IN] start_point = [focus_point[0] - 0.5 * WIDTH_1IN, focus_point[1] - 0.5 * HEIGHT_1IN]
for i in range(0,4): for k in range(0,2): bk.paste(photo1, (int(start_point[0] + (k * 350)), int(start_point[1] + i * 0.25 * WIDTH_6IN )))
focus_point2 = [0.5 * HEIGHT_6IN+350, WIDTH_6IN/6] start_point2 = [focus_point2[0] - 0.5 * WIDTH_2IN, focus_point2[1] - 0.5 * HEIGHT_2IN] for i in range(0,3): bk.paste(photo2, (int(start_point2[0]), int(start_point2[1] + i * WIDTH_6IN /3))) return bk
im = Image.open('blue-white-removebg-preview.png') width = im.size[0] height = im.size[1] rate = height / width layout_photo_5_1(resize_photo(cut_photo(im,1),1)).save('5_1.jpg') layout_photo_5_2(resize_photo(cut_photo(im,2),2)).save('5_2.jpg') layout_photo_6_1(resize_photo(cut_photo(im,1),1)).save('6_1.jpg') layout_photo_6_2(resize_photo(cut_photo(im,2),2)).save('6_2.jpg') layout_photo_5_mix(resize_photo(cut_photo(im,1),1),resize_photo(cut_photo(im,2),2).rotate(90,expand=True)).save('5_1_mix.jpg') layout_photo_6_mix1(resize_photo(cut_photo(im,1),1).rotate(90,expand=True),resize_photo(cut_photo(im,2),2)).save('6_mix1.jpg') layout_photo_6_mix2(resize_photo(cut_photo(im,1),1),resize_photo(cut_photo(im,2),2)).save('6_mix2.jpg')
|