图像尺寸缩小到无处可去 [英] Image size shrunk out of nowhere

查看:62
本文介绍了图像尺寸缩小到无处可去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是复制了图像,然后将其保存到当前目录中的另一个临时文件夹中,没有进行任何修改,但是图像大小有所减小……为什么?

I simply copied my image and saved it to another temp folder in the current directory, nothing is modified, but the image size is somehow reduced... why?

from PIL import Image
import os

image_path = "/Users/moomoochen/Desktop/XXXXX.jpg"
img = Image.open(image_path)
pathname, filename = os.path.split(image_path)

new_pathname = (pathname + "/temp")

if not os.path.exists(new_pathname):
  os.makedirs(new_pathname)
  img.save(os.path.join(new_pathname, filename))

图像大小从3.2 MB减小到350 KB,我想念了什么?

The image size is reduced quite a bit, from 3.2 MB down to 350 KB, what did I miss?

推荐答案

PIL/Pillow 将图像另存为JPEG时,其默认质量为75,这可能低于该质量保存原始图像的位置,因此文件较小.

When PIL/Pillow saves your image as a JPEG, it uses a default quality of 75 and this is likely lower than the quality at which your original image was saved, hence the file is smaller.

您可以使用jhead轻松检查输入和输出文件的质量,如下所示:

You can readily check the quality of your input and output files with jhead like this:

jhead image.jpg

示例输出

File name    : image.jpg
File size    : 199131 bytes
File date    : 2018:11:13 09:42:59
Resolution   : 1374 x 1182
JPEG Quality : 75

如果要保留更多质量,可以在保存时指定一个不同于75的值.不建议超过95,因为这样会增加文件大小,并且没有任何好处:

If you wish to retain more quality, you can specify a different value from 75 when saving. It is not recommended to go above 95 as it increases file size with no benefit:

img.save('result.jpg', quality=90)

这篇关于图像尺寸缩小到无处可去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆