【问题标题】:How to optimize image size using wand in python如何在 python 中使用 wand 优化图像大小
【发布时间】:2016-04-09 14:47:40
【问题描述】:

我想使用 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 的新手)。

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

【问题讨论】:

    标签: python optimization png wand


    【解决方案1】:

    更新答案

    使用 设置优化将需要一些额外的 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 是您要找的。​​p>

    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 将在执行adaptive 过滤器后使用zlib 级别7 进行压缩。请注意,这只是级别和过滤器,而不是优化策略。可以使用 CLI 选项 -define png:compression-strategy=zs 设置优化策略,但 尚未实现图像工件方法。

    【讨论】:

    • 谢谢@emcconville,我忘了提到我确实用上面的代码尝试了compression_quality,但这似乎并没有减少大小。
    • 您是否通过 Glenn Randers-Pehrson 的 pngcrush 运行过相同的图像?
    • 感谢您提供更新的答案,不幸的是,这似乎也不会影响大小。我没有针对 pngcrush 运行这些图像,但我确实尝试了我上面提到的 PIL 库代码。 PIL 确实将大小减小到原始图像的三分之一,而我用于 wand 的代码没有。
    猜你喜欢
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 2020-01-20
    • 2010-10-11
    • 2019-02-21
    • 1970-01-01
    相关资源
    最近更新 更多