如何在Python中使用wand优化图像大小

2024-02-24

我想使用魔杖调整和优化 png 和 jpg 图像大小。

使用 PIL,如果我指定优化选项,我可以保存大约三分之一大小的相同图像。

with open(filename, 'rb') as f:
    pimage = PImage.open(f)
    resized_pimage = pimage.resize((scaled_width, scaled_height), PImage.ANTIALIAS)            bytes_buffer = io.BytesIO()
    resized_pimage.save(bytes_buffer, format="PNG", optimize=True)

但是,我不确定 Wand 的等效选项是什么:

with default_storage.open(filename, 'rb') as f:
    img = WImage(file=f)
    img.resize(width=scaled_width, height=scaled_height, filter='gaussian')
    with WImage(width=scaled_width, height=scaled_height) as png:
        png.composite(img, top=0, left=0)
        png.format = 'png'
        bytes_buffer = io.BytesIO()
        png.save(file=bytes_buffer)

我读了一些关于 ImageMagic 图像优化的文章(例如http://www.smashingmagazine.com/2015/06/efficient-image-resizing-with-imagemagick/ http://www.smashingmagazine.com/2015/06/efficient-image-resizing-with-imagemagick/)但我如何在 Wand 中执行这些操作并不明显(我在 Wand 或 PIL 中都是新手)。

任何帮助/指示将不胜感激。


更新答案

设置优化wand /questions/tagged/wand将需要一些额外的 MagickWand 库扩展/配置。这是由于quality需要设置的属性wand数据结构,而不是图像的实例。使困惑?我是。幸运的是,Python 的 Wand 库使这一切变得简单。尝试以下操作。

# Require wand's API library and basic ctypes
from wand.api import library
from ctypes import c_void_p, c_size_t

# Tell Python's wand library about the MagickWand Compression Quality (not Image's Compression Quality)
library.MagickSetCompressionQuality.argtypes = [c_void_p, c_size_t]

# Do work as before
from wand.image import Image

with Image(filename=filename) as img:
    img.resize(width=scaled_width, height=scaled_hight)
    # Set the optimization level through the linked resources of 
    # the image instance. (i.e. `wand.image.Image.wand`)
    library.MagickSetCompressionQuality(img.wand, 75)
    img.save(filename=output_destination)

原答案

“优化”有很多种类型png /questions/tagged/png格式,但我的印象是您正在寻求一种减小图像大小的方法。

我相信wand.Image.compression_quality http://docs.wand-py.org/en/0.4.2/wand/image.html#wand.image.BaseImage.compression_quality就是您所寻找的。

from wand.image import Image

with Image(filename=filename) as img:
    img.resize(width=scaled_width, height=scaled_hight)
    img.compression_quality = 75
    img.save(filename=output_destination)

上述不会将质量降低到 75%,正如您所期望的那样JPEG格式,但指示使用哪个 PNG 压缩库/算法/过滤器。看PNG 压缩和更好的 PNG 压缩 http://www.imagemagick.org/Usage/formats/#png_quality例子。

+-----+
| 7 5 |
+-----+
| 0 . | Huffman compression (no-zlib)
| 1 . | zlib compression level 1
| 2 . | zlib compression level 2
| 3 . | zlib compression level 3
| 4 . | zlib compression level 4
| 5 . | zlib compression level 5
| 6 . | zlib compression level 6
| 7 . | zlib compression level 7
| 8 . | zlib compression level 8
| 9 . | zlib compression level 9
| . 0 | No data encoding/filtering before compression
| . 1 | "Sub" data encoding/filtering before compression
| . 2 | "Up" data encoding/filtering before compression
| . 3 | "Average" data encoding/filtering before compression
| . 4 | "Paeth" data encoding/filtering before compression
| . 5 | "Adaptive" data encoding/filtering before compression
+-----+

因此将质量设置为75将使用 zlib 级别进行压缩7执行后adaptive筛选。请注意,这只是级别和过滤器,而不是优化策略。可以使用 CLI 选项设置优化策略-define png:compression-strategy=zs然而wand /questions/tagged/wand尚未实现图像伪影方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在Python中使用wand优化图像大小 的相关文章

随机推荐