mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Write optimized JPEG data by default
This commit is contained in:
parent
8dd533bd5f
commit
6115eb9330
@ -84,7 +84,7 @@ def blend_image(img, bgcolor='#ffffff'):
|
|||||||
overlay(img, nimg)
|
overlay(img, nimg)
|
||||||
return nimg
|
return nimg
|
||||||
|
|
||||||
def image_to_data(img, compression_quality=95, fmt='JPEG', png_compression_level=9):
|
def image_to_data(img, compression_quality=95, fmt='JPEG', png_compression_level=9, jpeg_optimized=True, jpeg_progressive=True):
|
||||||
''' Serialize image to bytestring in the specified format.
|
''' Serialize image to bytestring in the specified format.
|
||||||
compression_quality is for JPEG and goes from 0 to 100.
|
compression_quality is for JPEG and goes from 0 to 100.
|
||||||
png_compression_level is for PNG and goes from 0-9 '''
|
png_compression_level is for PNG and goes from 0-9 '''
|
||||||
@ -92,10 +92,16 @@ def image_to_data(img, compression_quality=95, fmt='JPEG', png_compression_level
|
|||||||
buf = QBuffer(ba)
|
buf = QBuffer(ba)
|
||||||
buf.open(QBuffer.WriteOnly)
|
buf.open(QBuffer.WriteOnly)
|
||||||
fmt = fmt.upper()
|
fmt = fmt.upper()
|
||||||
|
is_jpeg = fmt in ('JPG', 'JPEG')
|
||||||
w = QImageWriter(buf, fmt.encode('ascii'))
|
w = QImageWriter(buf, fmt.encode('ascii'))
|
||||||
if img.hasAlphaChannel() and fmt in 'JPEG JPG'.split():
|
if is_jpeg:
|
||||||
img = blend_image(img)
|
if img.hasAlphaChannel():
|
||||||
if fmt == 'PNG':
|
img = blend_image(img)
|
||||||
|
if jpeg_optimized and hasattr(QImageWriter, 'setOptimizedWrite'):
|
||||||
|
w.setOptimizedWrite(True)
|
||||||
|
if jpeg_progressive and hasattr(QImageWriter, 'setProgressiveScanWrite'):
|
||||||
|
w.setProgressiveScanWrite(True)
|
||||||
|
elif fmt == 'PNG':
|
||||||
cl = min(9, max(0, png_compression_level))
|
cl = min(9, max(0, png_compression_level))
|
||||||
w.setQuality(10 * (9-cl))
|
w.setQuality(10 * (9-cl))
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user