strip off RGB color profile from bilevel TIFF images produced by gimp (closes: #164)

This commit is contained in:
Johannes Schauer Marin Rodrigues 2023-05-30 06:25:26 +02:00
parent 7fa67bb337
commit aea472101b
Signed by: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -22,7 +22,7 @@ import sys
import os import os
import zlib import zlib
import argparse import argparse
from PIL import Image, TiffImagePlugin, GifImagePlugin from PIL import Image, TiffImagePlugin, GifImagePlugin, ImageCms
if hasattr(GifImagePlugin, "LoadingStrategy"): if hasattr(GifImagePlugin, "LoadingStrategy"):
# Pillow 9.0.0 started emitting all frames but the first as RGB instead of # Pillow 9.0.0 started emitting all frames but the first as RGB instead of
@ -46,6 +46,7 @@ import platform
import hashlib import hashlib
from itertools import chain from itertools import chain
import re import re
import io
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -1431,6 +1432,21 @@ def get_imgmetadata(
iccp = None iccp = None
if "icc_profile" in imgdata.info: if "icc_profile" in imgdata.info:
iccp = imgdata.info.get("icc_profile") iccp = imgdata.info.get("icc_profile")
# GIMP saves bilevel tiff images with an RGB ICC profile which
# is useless and produces an error in Adobe Acrobat -- ignore it
if color == Colorspace["1"] and imgformat == ImageFormat.TIFF:
with io.BytesIO(imgdata.info.get("icc_profile")) as f:
prf = ImageCms.ImageCmsProfile(f)
if (
prf.profile.model == "sRGB"
and prf.profile.manufacturer == "GIMP"
and prf.profile.profile_description == "GIMP built-in sRGB"
):
logger.warning(
"Ignoring RGB ICC profile for bilevel TIFF produced by GIMP."
)
logger.warning("https://gitlab.gnome.org/GNOME/gimp/-/issues/9518")
iccp = None
logger.debug("width x height = %dpx x %dpx", imgwidthpx, imgheightpx) logger.debug("width x height = %dpx x %dpx", imgwidthpx, imgheightpx)
@ -4248,7 +4264,7 @@ and left/right, respectively. It is not possible to specify asymmetric borders.
print( print(
"Reading image from standard input...\n" "Reading image from standard input...\n"
"Re-run with -h or --help for usage information.", "Re-run with -h or --help for usage information.",
file=sys.stderr file=sys.stderr,
) )
try: try:
images = [sys.stdin.buffer.read()] images = [sys.stdin.buffer.read()]