python - 图像尺寸突然缩小

标签 python image size python-imaging-library jpeg

我只是复制了图像并将其保存到当前目录中的另一个临时文件夹中,没有任何修改,但图像大小以某种方式减小了......为什么?

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,我错过了什么?

最佳答案

PIL/Pillow将图像保存为 JPEG 时,它使用默认质量 75,这可能低于保存原始图像的质量,因此文件较小。

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

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,因为它会增加文件大小,但没有任何好处:

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

关于python - 图像尺寸突然缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53149385/

相关文章:

python - Python中BST反序列化的错误实现

python - 使用 pytesseract 执行 OCR 时出错

swift - Mac Swift - 如何下载图像并将其加载到 NSImageView 中?

javascript - 使用 javascript 或 jquery 触发图像加载

html - 无法裁剪个人资料图片的特定部分

Python 3 : Convert decimals to date time

C++:在WINDOWS上写入BMP图像格式错误

delphi - 如何用鼠标调整cxgrid的大小?

android - Assets 创建 - 9 补丁启动画面和屏幕密度

Windows 应用商店应用中的 Python