Add Encoding.flate_jpeg to recognize deflated JPEG images
FlateDecode+DCTDecode compressed images are essentially deflated JPEGs, typically created by OCRmyPDF's optimizer. This change ensures pdfinfo correctly identifies them and should_visible_page_image_use_jpg treats them as JPEG-origin images, allowing JPEG output when appropriate.
This commit is contained in:
@@ -735,7 +735,8 @@ def ocr_engine_direct(
|
||||
def should_visible_page_image_use_jpg(pageinfo: PageInfo) -> bool:
|
||||
"""Determines whether the visible page image should be saved as a JPEG.
|
||||
|
||||
If all images were JPEGs originally, permit a JPEG as output.
|
||||
If all images were JPEGs originally (including FlateDecode+DCTDecode),
|
||||
permit a JPEG as output.
|
||||
|
||||
Args:
|
||||
pageinfo: The PageInfo object containing information about the page.
|
||||
@@ -744,7 +745,7 @@ def should_visible_page_image_use_jpg(pageinfo: PageInfo) -> bool:
|
||||
A boolean indicating whether the visible page image should be saved as a JPEG.
|
||||
"""
|
||||
return bool(pageinfo.images) and all(
|
||||
im.enc == Encoding.jpeg for im in pageinfo.images
|
||||
im.enc in (Encoding.jpeg, Encoding.flate_jpeg) for im in pageinfo.images
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -108,10 +108,18 @@ class ImageInfo:
|
||||
self._type = 'image'
|
||||
|
||||
self._bpc = int(pim.bits_per_component)
|
||||
try:
|
||||
self._enc = FRIENDLY_ENCODING.get(pim.filters[0])
|
||||
except IndexError:
|
||||
self._enc = None
|
||||
if (
|
||||
len(pim.filters) == 2
|
||||
and pim.filters[0] == '/FlateDecode'
|
||||
and pim.filters[1] == '/DCTDecode'
|
||||
):
|
||||
# Special case: FlateDecode followed by DCTDecode
|
||||
self._enc = Encoding.flate_jpeg
|
||||
else:
|
||||
try:
|
||||
self._enc = FRIENDLY_ENCODING.get(pim.filters[0])
|
||||
except IndexError:
|
||||
self._enc = None
|
||||
|
||||
try:
|
||||
self._color = FRIENDLY_COLORSPACE.get(pim.colorspace or '')
|
||||
|
||||
@@ -36,6 +36,7 @@ class Encoding(Enum):
|
||||
lzw = auto()
|
||||
flate = auto()
|
||||
runlength = auto()
|
||||
flate_jpeg = auto()
|
||||
|
||||
|
||||
FloatRect = tuple[float, float, float, float]
|
||||
|
||||
Reference in New Issue
Block a user