Skip to content

Implement PNG optimizer with pillow #2068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions overviewer_core/optimizeimages.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import os
import subprocess
from PIL import Image


class Optimizer:
Expand Down Expand Up @@ -209,6 +210,27 @@ def is_crusher(self):
return True


class pillowpng(Optimizer, PNGOptimizer):

def __init__(self, colors=256, dither=True):
if colors < 2:
raise Exception("You can't use less than 2 colors")
self.colors = colors
self.dither = dither

def optimize(self, img):
orig = Image.open(img)
conv = orig.convert('P', palette=Image.ADAPTIVE, colors=self.colors,
dither=Image.FLOYDSTEINBERG if self.dither else Image.NONE)
conv.save(img, format='PNG', optimize=True)

def check_availability(self):
return True

def is_crusher(self):
return True


def optimize_image(imgpath, imgformat, optimizers):
for opt in optimizers:
if imgformat == 'png':
Expand Down
2 changes: 1 addition & 1 deletion overviewer_core/settingsDefinition.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

from .settingsValidators import *
from .observer import ProgressBarObserver, LoggingObserver, JSObserver
from .optimizeimages import pngnq, optipng, pngcrush
from .optimizeimages import pngnq, optipng, pngcrush, pillowpng
import platform
import sys

Expand Down