如何使用Python中的Wand优化图像大小

7
我希望使用wand来调整和优化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/),但是我不知道如何在Wand中进行这些操作(我对Wand和PIL都是完全的新手)。

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

1个回答

7

更新的答案

使用 设置优化将需要一些额外的MagickWand库扩展/配置。这是因为需要在wand数据结构上设置quality属性,而不是图像实例上设置。感到困惑吗?幸运的是,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)

原始答案

有许多类型的格式“优化”,但我认为你正在寻找一种减小图像大小的方法。

我相信wand.Image.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)

以上内容不会像JPEG格式一样将质量降低到75%,而是指示使用哪个PNG压缩库/算法/滤镜。请参见PNG compression & Better PNG Compression的示例。
+-----+
| 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进行压缩。请注意,这只是级别和滤波器,而不是优化策略。可以使用CLI选项-define png:compression-strategy=zs设置优化策略,但是尚未实现图像工件方法。

谢谢@emcconville,我忘记提到我已经尝试过使用上面的代码来压缩质量,但似乎并没有减小文件大小。 - nlhma
你是否已经使用Glenn Randers-Pehrson的pngcrush运行了相同的图像? - emcconville
感谢您提供的更新答案,不幸的是,这似乎也没有影响到大小。我还没有使用pngcrush运行这些图像,但我确实尝试了上面提到的PIL库代码。PIL可以将大小减小到原始图像的三分之一,而我用于wand的代码则不能。 - nlhma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接