How to optimize image size with stick in python

I want to resize and optimize the size of png and jpg images with a stick.

With PIL, I can save the same image from about the 3rd size if I specify the optimization option.

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 if the equivalent option for Wand is:

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 several articles on image optimization for ImageMagic (e.g. http://www.smashingmagazine.com/2015/06/efficient-image-resizing-with-imagemagick/ ), but it’s not obvious how I can do this in Wand ( I'm a complete newbie to Wand or PIL).

Any help / pointer is appreciated.

+4
source share
2 answers

Updated Answer

/ MagickWand. , quality wand, . ? . , Python Wand . .

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

# Tell Python wand library about the MagickWand Compression Quality (not Image 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)

75%, JPEG, , PNG-/algo/filter . . 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
+-----+

, 75 zlib 7 adaptive. , , . CLI -define png:compression-strategy=zs .

+1

" " " ". , , Wand-0.4.4 , "compression_quality".

@property
def compression_quality(self):
    """(:class:`numbers.Integral`) Compression quality of this image.

    .. versionadded:: 0.2.0

    """
    return library.MagickGetImageCompressionQuality(self.wand)

@compression_quality.setter
@manipulative
def compression_quality(self, quality):
    """Set compression quality for the image.

    :param quality: new compression quality setting
    :type quality: :class:`numbers.Integral`

    """
    if not isinstance(quality, numbers.Integral):
        raise TypeError('compression quality must be a natural '
                        'number, not ' + repr(quality))
    r = library.MagickSetImageCompressionQuality(self.wand, quality)
    if not r:
        raise ValueError('Unable to set compression quality to ' +
                         repr(quality))
0

Source: https://habr.com/ru/post/1622958/


All Articles