-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[] : Yelp

, 25 2017 . 19:30 +
Yelp 100 , , #yelfies. -, . , 30%. , . , !


Yelp 12 . lossless- (PNG, GIF) PNG, JPEG. Python Pillow, :

# do a typical thumbnail, preserving aspect ratio
new_photo = photo.copy()
new_photo.thumbnail(
    (width, height),
    resample=PIL.Image.ANTIALIAS,
)
thumbfile = cStringIO.StringIO()
save_args = {'format': format}
if format == 'JPEG':
    save_args['quality'] = 85
new_photo.save(thumbfile, **save_args)
.


-, , CDN- . , . / . . , .

  1. Pillow
    • Optimize
    • Progressive JPEG

    • PNG
    • JPEG
  2. JPEG
    • Mozjpeg (-, )

Pillow


Optimize


, : Pillow CPU (optimize=True). , .

JPEG , . , , , . PNG zlib, gzip -9 gzip -6.

, , , .

Progressive JPEG


JPEG :

  • Baseline JPEG,
  • Progressive JPEG, . Pillow (progressive=True). , ( , , )

, , . , JPEG 8x8 . , , , 8x8 . . ( ), , , . , ( ).

Baseline JPEG Progressive JPEG

, Baseline JPEG


, Progressive JPEG


PNG


Yelp JPEG PNG. JPEG , ( ). , PNG , , , . , PNG, , JPEG. PNG Yelp , , .


: PNG . : PNG,

PNG, , , .. , ? ?

2500 , , . , 300. , , 216 (Yelp RGBA RGB, , ).

88% , .

JPEG


JPEG quality. , JPEG, quality .

. , JPEG. 0 100 , ( ). JPEG, .

, . , .

, , . :

  • (Bottom-up): , 8x8. , .
  • (Top-down): , . , , , .

bottom-up , , ( , , ). 90-, , , , .

: - (SSIM) pyssim , , . ( ) , .

SSIM 2500 , .

  1. , quality = 85, .
  2. , quality = 80, .
  3. , , SSIM 80-85, . 80 85 (), SSIM: , - . .



SSIM 2500

SSIM?
, . , SSIM, , :

  1. JPEG
  2. ,
  3. PIL PNG CLI (. #2)

:

import cStringIO
import PIL.Image
from ssim import compute_ssim
 
 
def get_ssim_at_quality(photo, quality):
    """Return the ssim for this JPEG image saved at the specified quality"""
    ssim_photo = cStringIO.StringIO()
    # optimize is omitted here as it doesn't affect
    # quality but requires additional memory and cpu
    photo.save(ssim_photo, format="JPEG", quality=quality, progressive=True)
    ssim_photo.seek(0)
    ssim_score = compute_ssim(photo, PIL.Image.open(ssim_photo))
    return ssim_score
 
 
def _ssim_iteration_count(lo, hi):
    """Return the depth of the binary search tree for this range"""
    if lo >= hi:
        return 0
    else:
        return int(log(hi - lo, 2)) + 1
 
 
def jpeg_dynamic_quality(original_photo):
    """Return an integer representing the quality that this JPEG image should be
    saved at to attain the quality threshold specified for this photo class.
 
    Args:
        original_photo - a prepared PIL JPEG image (only JPEG is supported)
    """
    ssim_goal = 0.95
    hi = 85
    lo = 80
 
    # working on a smaller size image doesn't give worse results but is faster
    # changing this value requires updating the calculated thresholds
    photo = original_photo.resize((400, 400))
 
    if not _should_use_dynamic_quality():
        default_ssim = get_ssim_at_quality(photo, hi)
        return hi, default_ssim
 
    # 95 is the highest useful value for JPEG. Higher values cause different behavior
    # Used to establish the image's intrinsic ssim without encoder artifacts
    normalized_ssim = get_ssim_at_quality(photo, 95)
    selected_quality = selected_ssim = None
 
    # loop bisection. ssim function increases monotonically so this will converge
    for i in xrange(_ssim_iteration_count(lo, hi)):
        curr_quality = (lo + hi) // 2
        curr_ssim = get_ssim_at_quality(photo, curr_quality)
        ssim_ratio = curr_ssim / normalized_ssim
 
        if ssim_ratio >= ssim_goal:
            # continue to check whether a lower quality level also exceeds the goal
            selected_quality = curr_quality
            selected_ssim = curr_ssim
            hi = curr_quality
        else:
            lo = curr_quality
 
    if selected_quality:
        return selected_quality, selected_ssim
    else:
        default_ssim = get_ssim_at_quality(photo, hi)
        return hi, default_ssim

, . , Etsy ! , !

JPEG


Mozjpeg


Mozjpeg open-source libjpeg-turbo, . . 3-5 , libjpeg-turbo, !

mozjpeg , . , . , JPEG . JPEG:

- .

, ,

Mozjpeg , .

Mozjpeg + Pillow


Linux libjpeg. mozjpeg Pillow , . mozjpeg --with-jpeg8 , Pillow. Docker, Dockerfile:

FROM ubuntu:xenial
 
RUN apt-get update \
	&& DEBIAN_FRONTEND=noninteractive apt-get -y --no-install-recommends install \
	# build tools
	nasm \
	build-essential \
	autoconf \
	automake \
	libtool \
	pkg-config \
	# python tools
	python \
	python-dev \
	python-pip \
	python-setuptools \
	# cleanup
	&& apt-get clean \
	&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
 
# Download and compile mozjpeg
ADD https://github.com/mozilla/mozjpeg/archive/v3.2-pre.tar.gz /mozjpeg-src/v3.2-pre.tar.gz
RUN tar -xzf /mozjpeg-src/v3.2-pre.tar.gz -C /mozjpeg-src/
WORKDIR /mozjpeg-src/mozjpeg-3.2-pre
RUN autoreconf -fiv \
	&& ./configure --with-jpeg8 \
	&& make install prefix=/usr libdir=/usr/lib64
RUN echo "/usr/lib64\n" > /etc/ld.so.conf.d/mozjpeg.conf
RUN ldconfig
 
# Build Pillow
RUN pip install virtualenv \
	&& virtualenv /virtualenv_run \
	&& /virtualenv_run/bin/pip install --upgrade pip \
	&& /virtualenv_run/bin/pip install --no-binary=:all: Pillow==4.0.0

! Pillow mozjpeg .


? 2500 - Yelp, .

  1. Pillow 4,5%
  2. PNG 6,2%
  3. 4,5%
  4. mozjpeg 13,8%

30%, , . CDN:


, CDN ( , )


, , Yelp , .


, -. , , 4:1:1 ( Pillow, ), - .

PNG


, PNG, , pngmini, , JPEG. , 72-85%, .


WebP JPEG2k . , , JPEG/PNG, .

SVG


SVG , , , . svgo , .


, , , , . open-source thumbor. , , . .


.

Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/331588/

:  

: [1] []
 

:
: 

: ( )

:

  URL