119 lines
4.0 KiB
Python
119 lines
4.0 KiB
Python
# Pillow 8 이상을 확인하세요.
|
|
import sys, math, io, os
|
|
from PIL import Image, ImageCms
|
|
|
|
def get_script_path():
|
|
return os.path.dirname(os.path.realpath(sys.argv[0]))
|
|
|
|
def size_rate(width,height):
|
|
if width >= height:
|
|
type = "landscape"
|
|
else:
|
|
type = "portrait"
|
|
rateval = max(width,height) / min(width,height)
|
|
dataset = {"1:1":abs(rateval-1.0),
|
|
"5:4":abs(rateval-1.25),
|
|
"4:3":abs(rateval-1.33),
|
|
"3:2":abs(rateval-1.50),
|
|
"A":abs(rateval-1.41)}
|
|
res = {"rate": min(dataset, key=dataset.get),
|
|
"score": dataset[min(dataset, key=dataset.get)],
|
|
"type":type}
|
|
return res
|
|
|
|
def dpiTxt(imginfo):
|
|
if 'dpi' in imginfo:
|
|
dpiTxt = math.ceil(imginfo['dpi'][0])
|
|
else:
|
|
dpiTxt = 72
|
|
return dpiTxt
|
|
|
|
def get_icc(img):
|
|
if 'icc_profile' in img.info:
|
|
profile = ImageCms.getOpenProfile(io.BytesIO(img.info["icc_profile"])).tobytes()
|
|
else:
|
|
if img.mode != "CMYK":
|
|
image = ImageCms.profileToProfile(img,
|
|
get_script_path() + "/../color_profiles/sRGB Color Space Profile.icm",
|
|
get_script_path() + "/../color_profiles/sRGB Color Space Profile.icm",
|
|
renderingIntent=0)
|
|
profile = ImageCms.getOpenProfile(io.BytesIO(image.info["icc_profile"])).tobytes()
|
|
else:
|
|
image = ImageCms.profileToProfile(img,
|
|
get_script_path() + "/../color_profiles/USWebCoatedSWOP.icc",
|
|
get_script_path() + "/../color_profiles/USWebCoatedSWOP.icc",
|
|
renderingIntent=0, outputMode='CMYK')
|
|
profile = ImageCms.getOpenProfile(io.BytesIO(image.info["icc_profile"])).tobytes()
|
|
return profile
|
|
|
|
def build_filename(orgname,imgformat,mode):
|
|
if imgformat == "JPEG": ext = "jpg"
|
|
elif imgformat == "PNG": ext = "png"
|
|
elif format == "GIF": ext = "gif"
|
|
elif format == "BMP": ext = "bmp"
|
|
else: ext = "ext"
|
|
newfname = orgname + "_converted_" + str(mode) + "." + ext
|
|
return newfname
|
|
|
|
def convert_image(filename,img):
|
|
img.save(filename, dpi=(300, 300), quality=100, format=img.format,
|
|
mode=img.mode, icc_profile=get_icc(img))
|
|
|
|
def web_def_image(filename,img):
|
|
img.save(filename, dpi=(72, 72), quality=95, format=img.format,
|
|
mode="RGB", icc_profile="sRGB")
|
|
|
|
def get_imginfo(filename):
|
|
img = Image.open(filename)
|
|
res = {"filename":filename,
|
|
"format":img.format,
|
|
"mode":img.mode,
|
|
"width":img.width,
|
|
"height":img.height,
|
|
"dpi":str(dpiTxt(img.info)),
|
|
"img":img}
|
|
return res
|
|
|
|
Image.MAX_IMAGE_PIXELS = None
|
|
|
|
filename = str(sys.argv[1])
|
|
|
|
infoImg = get_imginfo(filename)
|
|
|
|
if dpiTxt(infoImg['img'].info) != 300:
|
|
convert_image(build_filename(filename,infoImg['format'],"to_300dpi"),infoImg['img'])
|
|
img_t = get_imginfo(build_filename(filename,infoImg['format'],"to_300dpi"))
|
|
conv_filename = img_t["filename"]
|
|
conv_format = img_t["format"]
|
|
conv_mode = img_t["mode"]
|
|
conv_width = img_t["width"]
|
|
conv_height = img_t["height"]
|
|
conv_dpi = img_t["dpi"]
|
|
else:
|
|
conv_filename = ""
|
|
conv_format = ""
|
|
conv_mode = ""
|
|
conv_width = ""
|
|
conv_height = ""
|
|
conv_dpi = ""
|
|
|
|
#web_def_image(,infoImg['img'])
|
|
|
|
print(
|
|
str(infoImg['filename']) + "\t"
|
|
+ str(infoImg['format']) + "\t"
|
|
+ infoImg['mode'] + "\t"
|
|
+ str(infoImg['width']) + "\t"
|
|
+ str(infoImg['height']) + "\t"
|
|
+ size_rate(infoImg['width'],infoImg['height'])["type"] + "\t"
|
|
+ size_rate(infoImg['width'],infoImg['height'])["rate"] + "\t"
|
|
+ str(size_rate(infoImg['width'],infoImg['height'])["score"]) + "\t"
|
|
+ str(dpiTxt(infoImg['img'].info)) + "\t"
|
|
+ conv_filename + "\t"
|
|
+ conv_format + "\t"
|
|
+ conv_mode + "\t"
|
|
+ str(conv_width) + "\t"
|
|
+ str(conv_height) + "\t"
|
|
+ str(conv_dpi)
|
|
)
|