更新答案
设置优化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尚未实现图像伪影方法。