我写了一段Python代码,可以将图像调整为最大1200 x 1200的框架,保持其宽高比。我遇到了这样一种情况,即输入图像(1080 x 1350)大小为249.8KB,而输出图像(960 x 1200)大小为317.2KB。尽管使用了optimize = True
并保持了质量,但仍然会出现这种情况。我的代码如下:
from PIL import Image
from wand.image import Image as Wand
MAX_RES = 1200
photo = Image.open("input.jpg")
breadth,height = photo.size
qual = Wand(filename="input.jpg").compression_quality
if(not((breadth <= MAX_RES) and (height <= MAX_RES))):
resizeRatio = max (float(breadth)/MAX_RES, float(height)/MAX_RES)
photo = photo.resize((int(breadth/resizeRatio),int(height/resizeRatio)))
photo.save("output.jpg",optimization = True,quality=qual)
使用Image.ANTIALIAS
会使图像大小进一步增加。
identify
命令行工具来查找图像属性。你的原始图像可能比调整大小后的图像质量低。 - user4815162342wand
获取质量,你可以在代码中看到Wand(filename="input.jpg").compression_quality
。即使保持质量并减小大小,输出文件的大小仍然比输入文件大。 - Sagnik Sinha