在Python的PIL中,如何改变图像的质量?

发布于 2024-10-05 23:44:17 字数 1436 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弃爱 2024-10-12 23:44:18

如果图片格式是JPEG,这里有一个例子:

from PIL import Image
im = Image.open("C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg")
im.save("C:\Users\Public\Pictures\Sample Pictures\Jellyfish_compressed.jpg", quality=10)

您需要阅读的参考文献是:

  • [图像模块][1],特别是“保存”功能,它允许您传入与每种图像格式相关的选项。
    • 每种图像格式的选项都位于不同的页面中,您可以在文档中找到它。

If the picture format is JPEG, here's an example:

from PIL import Image
im = Image.open("C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg")
im.save("C:\Users\Public\Pictures\Sample Pictures\Jellyfish_compressed.jpg", quality=10)

The references you need to be reading are:

  • [The Image module][1], particularly the "save" function, which allows you to pass in options relevant for each image format.
    • Each image format's options are in a different page, you can find it in the docs.
断桥再见 2024-10-12 23:44:18

解决了。

我做到了....

im.save( blah, quality=5)

Solved.

I did....

im.save( blah, quality=5)
梦幻之岛 2024-10-12 23:44:18

a) 更改大小:Image.resize(size, filter) b) 显式将其转换为 JPEG(如果不是)并设置所需的质量。 c) 结合使用 a) 和 b)

无论您做什么,都需要在尺寸和质量之间进行权衡。

a) change the size: Image.resize(size, filter) b) explicitly convert it to JPEG (if it is not) and set the desired quality. c) use a combination of a) and b)

Whatever you do, there is a trade-off between size and quality.

漆黑的白昼 2024-10-12 23:44:18

这对我来说很有用,可以使用 For 循环使用 PIL 调整图像大小。变量 PRODUCTS 是一个包含所有产品名称的列表,但您也可以对文件中的每一行使用 readlines() 来执行此操作:

def resize_images(self):
    products = PRODUCTS
    for name in products:
        try:
            fp = open(filename + name + ".jpg", "rb")
            img = Image.open(fp)
            img.load()
            img.save(filename + name + "_2" + ".jpg", quality=23)
            fp.close()
        except:
            print name 

This worked for me to use a For Loop to resize images using PIL. The variable PRODUCTS is a list that had all product names in it, but you can also use readlines() for each line in a file to do so:

def resize_images(self):
    products = PRODUCTS
    for name in products:
        try:
            fp = open(filename + name + ".jpg", "rb")
            img = Image.open(fp)
            img.load()
            img.save(filename + name + "_2" + ".jpg", quality=23)
            fp.close()
        except:
            print name 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文