diff --git a/src/__init__.py b/src/__init__.py index 68075c01b..cc367ab04 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -2396,7 +2396,9 @@ class Colorspace: def __init__(self, type_): """Supported are GRAY, RGB and CMYK.""" - if type_ == CS_GRAY: + if isinstance( type_, mupdf.FzColorspace): + self.this = type_ + elif type_ == CS_GRAY: self.this = mupdf.FzColorspace(mupdf.FzColorspace.Fixed_GRAY) elif type_ == CS_CMYK: self.this = mupdf.FzColorspace(mupdf.FzColorspace.Fixed_CMYK) @@ -7759,8 +7761,8 @@ def _insert_image(self, else: pm = mupdf.fz_convert_pixmap( arg_pix, - mupdf.FzColorspace(0), - mupdf.FzColorspace(0), + mupdf.FzColorspace(), + mupdf.FzColorspace(), mupdf.FzDefaultColorspaces(None), mupdf.FzColorParams(), 1, @@ -9429,7 +9431,7 @@ def __init__(self, *args): self.this = mupdf.fz_convert_pixmap( spix, cs, - mupdf.FzColorspace(0), + mupdf.FzColorspace(), mupdf.FzDefaultColorspaces(None), mupdf.FzColorParams(), 1 diff --git a/tests/resources/test_3058.pdf b/tests/resources/test_3058.pdf new file mode 100644 index 000000000..fb48230ac Binary files /dev/null and b/tests/resources/test_3058.pdf differ diff --git a/tests/test_pixmap.py b/tests/test_pixmap.py index 2e67797f2..9b3d5d148 100644 --- a/tests/test_pixmap.py +++ b/tests/test_pixmap.py @@ -187,3 +187,23 @@ def product(x, y): pix.save(os.path.abspath(f'{__file__}/../../tests/test_3050_out.png')) assert digest1 != digest0 assert digest1 == digest_expected + +def test_3058(): + doc = fitz.Document(os.path.abspath(f'{__file__}/../../tests/resources/test_3058.pdf')) + images = doc[0].get_images(full=True) + pix = fitz.Pixmap(doc, 17) + + # First bug was that `pix.colorspace` was DeviceRGB. + assert str(pix.colorspace) == 'Colorspace(CS_CMYK) - DeviceCMYK' + + pix = fitz.Pixmap(fitz.csRGB, pix) + assert str(pix.colorspace) == 'Colorspace(CS_RGB) - DeviceRGB' + + # Second bug was that the image was converted to RGB via greyscale proofing + # color space, so image contained only shades of grey. This compressed + # easily to a .png file, so we crudely check the bug is fixed by looking at + # size of .png file. + path = os.path.abspath(f'{__file__}/../../tests/test_3058_out.png') + pix.save(path) + s = os.path.getsize(path) + assert 1800000 < s < 2600000, f'Unexpected size of {path}: {s}'