optimize: on aggressive settings try JPG to PNG transcoding
If the color count of an image is low such as when black and white documents are scanned in color, PNG with lossy quantization may produce a superior encoding to JPEG. This is expensive to test however.
This commit is contained in:
@@ -17,6 +17,9 @@
|
||||
|
||||
from functools import lru_cache
|
||||
from subprocess import run
|
||||
from tempfile import NamedTemporaryFile
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from . import get_version
|
||||
from ..exceptions import MissingDependencyError
|
||||
@@ -36,16 +39,32 @@ def available():
|
||||
|
||||
|
||||
def quantize(input_file, output_file, quality_min, quality_max):
|
||||
args = [
|
||||
'pngquant',
|
||||
'--force',
|
||||
'--skip-if-larger',
|
||||
'--output',
|
||||
output_file,
|
||||
'--quality',
|
||||
f'{quality_min}-{quality_max}',
|
||||
'--',
|
||||
input_file,
|
||||
]
|
||||
proc = run(args)
|
||||
proc.check_returncode()
|
||||
if input_file.endswith('.jpg'):
|
||||
im = Image.open(input_file)
|
||||
with NamedTemporaryFile(suffix='.png') as tmp:
|
||||
im.save(tmp)
|
||||
args = [
|
||||
'pngquant',
|
||||
'--force',
|
||||
'--skip-if-larger',
|
||||
'--output',
|
||||
output_file,
|
||||
'--quality',
|
||||
f'{quality_min}-{quality_max}',
|
||||
'--',
|
||||
tmp.name,
|
||||
]
|
||||
run(args)
|
||||
else:
|
||||
args = [
|
||||
'pngquant',
|
||||
'--force',
|
||||
'--skip-if-larger',
|
||||
'--output',
|
||||
output_file,
|
||||
'--quality',
|
||||
f'{quality_min}-{quality_max}',
|
||||
'--',
|
||||
input_file,
|
||||
]
|
||||
run(args)
|
||||
|
||||
@@ -420,6 +420,9 @@ def optimize(input_file, output_file, log, context):
|
||||
|
||||
jpegs, pngs = extract_images_generic(pike, root, log, options)
|
||||
transcode_jpegs(pike, jpegs, root, log, options)
|
||||
if options.optimize >= 2:
|
||||
# Try pngifying the jpegs
|
||||
transcode_pngs(pike, jpegs, jpg_name, root, log, options)
|
||||
transcode_pngs(pike, pngs, png_name, root, log, options)
|
||||
|
||||
jbig2_groups = extract_images_jbig2(pike, root, log, options)
|
||||
|
||||
Reference in New Issue
Block a user