[英]How to optimize image size using wand in python
I want to resize and optimize png and jpg image size using wand. 我想使用魔杖调整大小并优化png和jpg图像大小。
With PIL, I'm able to save the same image with about a 3rd of the size if I specify the optimize option. 使用PIL,如果指定优化选项,我可以以大约3倍的大小保存相同的图像。
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)
However, I'm not sure what the equivalent option for Wand is: 但是,我不确定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)
I read a few articles about image optimization for ImageMagic (eg http://www.smashingmagazine.com/2015/06/efficient-image-resizing-with-imagemagick/ ) but it is not obvious how I can do these in Wand (I'm a complete newbie in either Wand or PIL). 我读了一些关于ImageMagic图像优化的文章(例如http://www.smashingmagazine.com/2015/06/efficient-image-resizing-with-imagemagick/ )但是我不知道如何在Wand中做到这些(我是魔杖或PIL的完全新手。
Any help/pointer would be greatly appreciated. 任何帮助/指针将不胜感激。
Setting the optimization with wand will require some additional MagickWand library extension/configuration. 使用wand设置优化需要一些额外的MagickWand库扩展/配置。 This is due to the
quality
attribute needing to be set on the wand
data-structure, and not the image's instance. 这是因为需要在
wand
数据结构上设置quality
属性,而不是图像的实例。 Confused? 困惑? I am.
我是。 Luckily Python's Wand library makes this easy.
幸运的是,Python的Wand库使这很容易。 Try the following.
请尝试以下方法。
# 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)
There are many types of "optimization" for png format, but I'm under the impression your seeking a way to reduce image size. png格式有许多类型的“优化”,但我的印象是你想要一种减少图像尺寸的方法。
I believe wand.Image.compression_quality
is what your looking for. 我相信
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)
The above will not reduce quality to 75% as you would expect with the JPEG
format, but instruct which PNG-compression library/algo/filter to use. 上面的内容不会像
JPEG
格式那样将质量降低到75%,但会指示使用哪个PNG压缩库/算法/过滤器。 See PNG compression & Better PNG Compression examples. 请参阅PNG压缩和更好的PNG压缩示例。
+-----+
| 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
+-----+
So setting the quality to 75
will compress using zlib level 7
after performing an adaptive
filter. 因此,在执行
adaptive
滤波器后,将质量设置为75
将使用zlib 7
级进行压缩。 Note this is just the level and filter, not the optimization strategy. 请注意,这只是级别和过滤器,而不是优化策略。 The optimization strategy can be set with the CLI option
-define png:compression-strategy=zs
however wand has yet to implement image artifact methods. 可以使用CLI选项
-define png:compression-strategy=zs
设置优化策略,但是wand尚未实现图像工件方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.