日常经常有网站要求制定分辨率的图片上传,每次看到网上的教程都是借助ps, 美图秀秀折腾。感觉太折腾了。借助python写一份小的代码进行精度转换可以满足日常需求了。
from PIL import Image
def change_resolution(picPath, reslution):
img = Image.open(picPath)
x, y = img.size
print( x, y)
changex = float(x) / reslution[0]
changey = float(y) / reslution[1]
# 判断分辨率是否满足
if changex > 1 or changey > 1:
change = changex if changex > changey else changey
#print( change)
#print( int(reslution[0] / change), int(reslution[1] / change))
print('output reslution:')
print( int(x / change), int(y / change))
img.resize((int(x / change), int(y / change))).save('result.jpg')
if __name__ == '__main__':
change_resolution('20190123154207.jpg', (1136, 640))
以下分别是转换前后图片的对比。