#!/usr/bin/python import Image, sys, os, glob type_list = {} type_list["type_A"] = {"horizontal": (415, 482), "vertical": (415, 482)} type_list["type_B"] = {"horizontal": (150, 133), "vertical": (150, 133)} type_list["type_C"] = {"horizontal": (600, 600), "vertical": (600, 600)} type_list["type_D"] = {"horizontal": (200, 200), "vertical": (200, 400)} OriginalDIR = "./psd/" CompleteDIR = "./complete/" LogoDIR = "./logo/" def openImg(file) : targetImage = Image.open(file) w, h = targetImage.size return (targetImage, w, h) def resizeIMG(file, factor) : targetImage, w, h = openImg(file) ori_size = (w, h) # print targetImage, w, h if (float(w)/float(h)) < 0.7 : # more long height # original size width = factor["vertical"][0] height = factor["vertical"][1] # resize size if w / width > h / height : w = w * height / h h = height newImage = targetImage.resize((w, h), Image.ANTIALIAS) center_pos = int((w-width)/2) newImage = newImage.crop((center_pos, 0, width+center_pos, height)) else : h = h * width / w w = width newImage = targetImage.resize((w, h), Image.ANTIALIAS) center_pos = int((h-height)/2) newImage = newImage.crop((0, center_pos, width, height+center_pos)) style = "vertical" else : # same width and height # original size width = factor["horizontal"][0] height = factor["horizontal"][1] # resize size if w == h : if width > height : newImage = targetImage.resize((width, width), Image.ANTIALIAS) center_pos = int((width-height)/2) # cut height newImage = newImage.crop((0, center_pos, width, height+center_pos)) else : newImage = targetImage.resize((height, height), Image.ANTIALIAS) center_pos = int((height-width)/2) # cut width newImage = newImage.crop((center_pos, 0, width+center_pos, height)) elif width < height : w = w * height / h h = height newImage = targetImage.resize((w, h), Image.ANTIALIAS) center_pos = int((w-width)/2) # cut width newImage = newImage.crop((center_pos, 0, width+center_pos, height)) else : h = h * width / w w = width newImage = targetImage.resize((w, h), Image.ANTIALIAS) center_pos = int((h-height)/2) # cut height newImage = newImage.crop((0, center_pos, width, height+center_pos)) style = "horizontal" print(file + " : " + str(ori_size) + " >> " + str(newImage.size)) return style, newImage # main code start if __name__ == "__main__" : # check argv type print("please a type of style, example :") for key, value in type_list.items() : print("%s is %s" % (key, value)) type_name = raw_input("Enter a type of image:n") # type_name = "type_B" # whether is or not if type_list.has_key(type_name) : pass else : print("My program haven't your input type") exit() # test type dir type_dir = CompleteDIR + type_name if os.path.isdir(type_dir) is False : print(type_name + "'s directory No havn't") os.mkdir(type_dir) print(type_name + "'s directory created!!") # import image dir for path, tmpdirs, files in os.walk(OriginalDIR) : pass #i = 0 try : horizontal_png = Image.open(LogoDIR + type_name + "/logo_horizontal.png") vertical_png = Image.open(LogoDIR + type_name + "/logo_vertical.png") except : print("No logo files") exit() for tmpFileName in files : #if(i > 40) : break # if not exist resize image if os.path.isfile(CompleteDIR + type_name + "/" + tmpFileName[:-3] + "jpg") is False : print(OriginalDIR + tmpFileName) style, resizedIMG = resizeIMG(OriginalDIR + tmpFileName, type_list[type_name]) # Logo mark if style is "horizontal" : resizedIMG = Image.composite(horizontal_png, resizedIMG, horizontal_png) elif style is "vertical" : resizedIMG = Image.composite(vertical_png, resizedIMG, vertical_png) else : pass resizedIMG.save(CompleteDIR + type_name + "/" + tmpFileName[:-3] + "jpg", 'JPEG', quality=90) else : pass #i = i + 1
targetImage.resize((w, h), Image.ANTIALIAS)
resize 함수에서 ANTIALIAS 값을 넣어줘야 이미지 리사이징이 부드럽게 처리된다.