GIMP 1bit PNGs (with metadata) bug #159

Closed
opened 1 year ago by monobot · 11 comments
monobot commented 1 year ago

Hi,

When I convert 1 bit (black and white only) PNGs created by GIMP they become much larger than the orginal.
I think its because the metadata saved with the file is causing the bug.
The metadata are the default metadata that GIMP offered to save PNGs with.

PNG with metadata
1bit.png 74,0651 bytes

Converted to PDF (much larger):
1bit.pdf 172,594 bytes

PNG but no metadata
1bit_no_metadata.png 69,971 bytes

Converted (correct size):
1bit_no_metadata.pdf 71,352 bytes

EDIT: Actually for the no-metadata PNG there was some metatdata saved: the image resolution.

Software details

GIMP 2.10.34
img2pdf 0.4.4
Debian Testing (12 Bookworm)

Conversion logs

PNG with metadata:
$ img2pdf -v -o 1bit.pdf 1bit.png
DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13
DEBUG:PIL.PngImagePlugin:STREAM b'zTXt' 41 203
DEBUG:PIL.PngImagePlugin:STREAM b'iCCP' 256 388
DEBUG:PIL.PngImagePlugin:iCCP profile name b'ICC profile'
DEBUG:PIL.PngImagePlugin:Compression method 0
DEBUG:PIL.PngImagePlugin:STREAM b'iTXt' 656 3448
DEBUG:PIL.PngImagePlugin:STREAM b'PLTE' 4116 6
DEBUG:PIL.PngImagePlugin:STREAM b'pHYs' 4134 9
DEBUG:PIL.PngImagePlugin:STREAM b'tIME' 4155 7
DEBUG:PIL.PngImagePlugin:b'tIME' 4155 7 (unknown)
DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 4174 8192
DEBUG:img2pdf:PIL format = PNG
DEBUG:img2pdf:imgformat = PNG
DEBUG:img2pdf:input dpi = 300 x 300
DEBUG:PIL.TiffImagePlugin:tag: ImageWidth (256) - type: long (4) - value: b'\xb0\t\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: ImageLength (257) - type: long (4) - value: b'\xb4\r\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: BitsPerSample (258) - type: short (3) Tag Location: 46 - Data Location: 134 - value: b'\x08\x00\x08\x00\x08\x00'
DEBUG:PIL.TiffImagePlugin:tag: Orientation (274) - type: short (3) - value: b'\x01\x00'
DEBUG:PIL.TiffImagePlugin:tag: XResolution (282) - type: rational (5) Tag Location: 70 - Data Location: 140 - value: b'\xfc)\x00\x00[\x00\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: YResolution (283) - type: rational (5) Tag Location: 82 - Data Location: 148 - value: b'\xfc)\x00\x00[\x00\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: ResolutionUnit (296) - type: short (3) - value: b'\x03\x00'
DEBUG:PIL.TiffImagePlugin:tag: Software (305) - type: string (2) Tag Location: 106 - Data Location: 156 - value: b'GIMP 2.10.34\x00'
DEBUG:PIL.TiffImagePlugin:tag: DateTime (306) - type: string (2) Tag Location: 118 - Data Location: 170 - value: b'2023:03:29 11:59:41\x00'
DEBUG:PIL.TiffImagePlugin:tag: ExifIFD (34665) - type: long (4) - value: b'\xbe\x00\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00'
DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00'
DEBUG:img2pdf:rotation = 0°
DEBUG:img2pdf:input colorspace = P
DEBUG:img2pdf:width x height = 2480px x 3508px
DEBUG:img2pdf:Converting frame: 0
DEBUG:img2pdf:input dpi = 300 x 300
DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00'
DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00'
DEBUG:img2pdf:rotation = 0°
DEBUG:img2pdf:input colorspace = P
DEBUG:img2pdf:width x height = 2480px x 3508px
DEBUG:img2pdf:Colorspace is OK: Colorspace.P
DEBUG:img2pdf:read_images() encoded an image as PNG
PNG with no metadata:
$ img2pdf -v -o 1bit_no_metadata.pdf 1bit_no_metadata.png
DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13
DEBUG:PIL.PngImagePlugin:STREAM b'PLTE' 41 6
DEBUG:PIL.PngImagePlugin:STREAM b'pHYs' 59 9
DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 80 8192
DEBUG:img2pdf:PIL format = PNG
DEBUG:img2pdf:imgformat = PNG
DEBUG:img2pdf:input dpi = 300 x 300
DEBUG:img2pdf:rotation = 0°
DEBUG:img2pdf:input colorspace = P
DEBUG:img2pdf:width x height = 2480px x 3508px
DEBUG:img2pdf:read_images() embeds a PNG
Hi, When I convert 1 bit (black and white only) PNGs created by GIMP they become much larger than the orginal. I think its because the metadata saved with the file is causing the bug. The metadata are the ~~default~~ metadata that GIMP offered to save PNGs with. PNG with metadata `1bit.png 74,0651 bytes` Converted to PDF (much larger): `1bit.pdf 172,594 bytes` PNG but no metadata `1bit_no_metadata.png 69,971 bytes` Converted (correct size): `1bit_no_metadata.pdf 71,352 bytes` EDIT: Actually for the no-metadata PNG there was *some* metatdata saved: the image resolution. #### Software details GIMP 2.10.34 img2pdf 0.4.4 Debian Testing (12 Bookworm) ### Conversion logs ##### PNG with metadata: ``` $ img2pdf -v -o 1bit.pdf 1bit.png DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13 DEBUG:PIL.PngImagePlugin:STREAM b'zTXt' 41 203 DEBUG:PIL.PngImagePlugin:STREAM b'iCCP' 256 388 DEBUG:PIL.PngImagePlugin:iCCP profile name b'ICC profile' DEBUG:PIL.PngImagePlugin:Compression method 0 DEBUG:PIL.PngImagePlugin:STREAM b'iTXt' 656 3448 DEBUG:PIL.PngImagePlugin:STREAM b'PLTE' 4116 6 DEBUG:PIL.PngImagePlugin:STREAM b'pHYs' 4134 9 DEBUG:PIL.PngImagePlugin:STREAM b'tIME' 4155 7 DEBUG:PIL.PngImagePlugin:b'tIME' 4155 7 (unknown) DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 4174 8192 DEBUG:img2pdf:PIL format = PNG DEBUG:img2pdf:imgformat = PNG DEBUG:img2pdf:input dpi = 300 x 300 DEBUG:PIL.TiffImagePlugin:tag: ImageWidth (256) - type: long (4) - value: b'\xb0\t\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: ImageLength (257) - type: long (4) - value: b'\xb4\r\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: BitsPerSample (258) - type: short (3) Tag Location: 46 - Data Location: 134 - value: b'\x08\x00\x08\x00\x08\x00' DEBUG:PIL.TiffImagePlugin:tag: Orientation (274) - type: short (3) - value: b'\x01\x00' DEBUG:PIL.TiffImagePlugin:tag: XResolution (282) - type: rational (5) Tag Location: 70 - Data Location: 140 - value: b'\xfc)\x00\x00[\x00\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: YResolution (283) - type: rational (5) Tag Location: 82 - Data Location: 148 - value: b'\xfc)\x00\x00[\x00\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: ResolutionUnit (296) - type: short (3) - value: b'\x03\x00' DEBUG:PIL.TiffImagePlugin:tag: Software (305) - type: string (2) Tag Location: 106 - Data Location: 156 - value: b'GIMP 2.10.34\x00' DEBUG:PIL.TiffImagePlugin:tag: DateTime (306) - type: string (2) Tag Location: 118 - Data Location: 170 - value: b'2023:03:29 11:59:41\x00' DEBUG:PIL.TiffImagePlugin:tag: ExifIFD (34665) - type: long (4) - value: b'\xbe\x00\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00' DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00' DEBUG:img2pdf:rotation = 0° DEBUG:img2pdf:input colorspace = P DEBUG:img2pdf:width x height = 2480px x 3508px DEBUG:img2pdf:Converting frame: 0 DEBUG:img2pdf:input dpi = 300 x 300 DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00' DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00' DEBUG:img2pdf:rotation = 0° DEBUG:img2pdf:input colorspace = P DEBUG:img2pdf:width x height = 2480px x 3508px DEBUG:img2pdf:Colorspace is OK: Colorspace.P DEBUG:img2pdf:read_images() encoded an image as PNG ``` ##### PNG with **no** metadata: ``` $ img2pdf -v -o 1bit_no_metadata.pdf 1bit_no_metadata.png DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13 DEBUG:PIL.PngImagePlugin:STREAM b'PLTE' 41 6 DEBUG:PIL.PngImagePlugin:STREAM b'pHYs' 59 9 DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 80 8192 DEBUG:img2pdf:PIL format = PNG DEBUG:img2pdf:imgformat = PNG DEBUG:img2pdf:input dpi = 300 x 300 DEBUG:img2pdf:rotation = 0° DEBUG:img2pdf:input colorspace = P DEBUG:img2pdf:width x height = 2480px x 3508px DEBUG:img2pdf:read_images() embeds a PNG ```
monobot changed title from GIMP 1bit PNGs (with metatadata) bug to GIMP 1bit PNGs (with metadata) bug 1 year ago
monobot commented 1 year ago
Poster

I think the bug may be due to the colorspace used.
I tried forcing the colorspace using the --colorspace option.
Surprisingly forcing the Black and White colorspace results in a PDF thats too large (the input PDF is a 1 bit Black and White image).

But forcing a Grayscale colorspace results in a correclty sized PDF!?

(The input PNG has metadata)

128,075 1bit_force_colorspace_black_white.pdf
 74,065 1bit.png
77,618 1bit_force_colorspace_grayscale.pdf
74,065 1bit.png
  • Why does forcing the correct Black and White colorspace casue a larger PDF?

  • img2pdf seems to be using a TiffImagePlugin (on a PNG) is that part of the problem?

  • These errors are at the end of the Black and White colorspace conversion log:

Fax3SetupState: Bits/sample must be 1 for Group 3/4 encoding/decoding. DEBUG:img2pdf:encoder error -2 when writing image file DEBUG:img2pdf:Converting colorspace 1 to L

Full conversion logs

Force Grayscale colorspace (result is correct sized PDF)

$ img2pdf -v -o 1bit_force_colorspace_grayscale.pdf --colorspace L 1bit.png

DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13
DEBUG:PIL.PngImagePlugin:STREAM b'zTXt' 41 203
DEBUG:PIL.PngImagePlugin:STREAM b'iCCP' 256 388
DEBUG:PIL.PngImagePlugin:iCCP profile name b'ICC profile'
DEBUG:PIL.PngImagePlugin:Compression method 0
DEBUG:PIL.PngImagePlugin:STREAM b'iTXt' 656 3448
DEBUG:PIL.PngImagePlugin:STREAM b'PLTE' 4116 6
DEBUG:PIL.PngImagePlugin:STREAM b'pHYs' 4134 9
DEBUG:PIL.PngImagePlugin:STREAM b'tIME' 4155 7
DEBUG:PIL.PngImagePlugin:b'tIME' 4155 7 (unknown)
DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 4174 8192
DEBUG:img2pdf:PIL format = PNG
DEBUG:img2pdf:imgformat = PNG
DEBUG:img2pdf:input dpi = 300 x 300
DEBUG:PIL.TiffImagePlugin:tag: ImageWidth (256) - type: long (4) - value: b'\xb0\t\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: ImageLength (257) - type: long (4) - value: b'\xb4\r\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: BitsPerSample (258) - type: short (3) Tag Location: 46 - Data Location: 134 - value: b'\x08\x00\x08\x00\x08\x00'
DEBUG:PIL.TiffImagePlugin:tag: Orientation (274) - type: short (3) - value: b'\x01\x00'
DEBUG:PIL.TiffImagePlugin:tag: XResolution (282) - type: rational (5) Tag Location: 70 - Data Location: 140 - value: b'\xfc)\x00\x00[\x00\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: YResolution (283) - type: rational (5) Tag Location: 82 - Data Location: 148 - value: b'\xfc)\x00\x00[\x00\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: ResolutionUnit (296) - type: short (3) - value: b'\x03\x00'
DEBUG:PIL.TiffImagePlugin:tag: Software (305) - type: string (2) Tag Location: 106 - Data Location: 156 - value: b'GIMP 2.10.34\x00'
DEBUG:PIL.TiffImagePlugin:tag: DateTime (306) - type: string (2) Tag Location: 118 - Data Location: 170 - value: b'2023:03:29 11:59:41\x00'
DEBUG:PIL.TiffImagePlugin:tag: ExifIFD (34665) - type: long (4) - value: b'\xbe\x00\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00'
DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00'
DEBUG:img2pdf:rotation = 0°
DEBUG:img2pdf:input colorspace (forced) = Colorspace.L
DEBUG:img2pdf:width x height = 2480px x 3508px
DEBUG:img2pdf:Converting frame: 0
DEBUG:img2pdf:input dpi = 300 x 300
DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00'
DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00'
DEBUG:img2pdf:rotation = 0°
DEBUG:img2pdf:input colorspace (forced) = Colorspace.L
DEBUG:img2pdf:width x height = 2480px x 3508px
DEBUG:img2pdf:Colorspace is OK: Colorspace.L
DEBUG:img2pdf:read_images() encoded an image as PNG

Force Black and White colorspace (result is a too large sized PDF)

$ img2pdf -v -o 1bit_force_colorspace_black_white.pdf --colorspace 1 1bit.png

DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13
DEBUG:PIL.PngImagePlugin:STREAM b'zTXt' 41 203
DEBUG:PIL.PngImagePlugin:STREAM b'iCCP' 256 388
DEBUG:PIL.PngImagePlugin:iCCP profile name b'ICC profile'
DEBUG:PIL.PngImagePlugin:Compression method 0
DEBUG:PIL.PngImagePlugin:STREAM b'iTXt' 656 3448
DEBUG:PIL.PngImagePlugin:STREAM b'PLTE' 4116 6
DEBUG:PIL.PngImagePlugin:STREAM b'pHYs' 4134 9
DEBUG:PIL.PngImagePlugin:STREAM b'tIME' 4155 7
DEBUG:PIL.PngImagePlugin:b'tIME' 4155 7 (unknown)
DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 4174 8192
DEBUG:img2pdf:PIL format = PNG
DEBUG:img2pdf:imgformat = PNG
DEBUG:img2pdf:input dpi = 300 x 300
DEBUG:PIL.TiffImagePlugin:tag: ImageWidth (256) - type: long (4) - value: b'\xb0\t\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: ImageLength (257) - type: long (4) - value: b'\xb4\r\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: BitsPerSample (258) - type: short (3) Tag Location: 46 - Data Location: 134 - value: b'\x08\x00\x08\x00\x08\x00'
DEBUG:PIL.TiffImagePlugin:tag: Orientation (274) - type: short (3) - value: b'\x01\x00'
DEBUG:PIL.TiffImagePlugin:tag: XResolution (282) - type: rational (5) Tag Location: 70 - Data Location: 140 - value: b'\xfc)\x00\x00[\x00\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: YResolution (283) - type: rational (5) Tag Location: 82 - Data Location: 148 - value: b'\xfc)\x00\x00[\x00\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: ResolutionUnit (296) - type: short (3) - value: b'\x03\x00'
DEBUG:PIL.TiffImagePlugin:tag: Software (305) - type: string (2) Tag Location: 106 - Data Location: 156 - value: b'GIMP 2.10.34\x00'
DEBUG:PIL.TiffImagePlugin:tag: DateTime (306) - type: string (2) Tag Location: 118 - Data Location: 170 - value: b'2023:03:29 11:59:41\x00'
DEBUG:PIL.TiffImagePlugin:tag: ExifIFD (34665) - type: long (4) - value: b'\xbe\x00\x00\x00'
DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00'
DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00'
DEBUG:img2pdf:rotation = 0°
DEBUG:img2pdf:input colorspace (forced) = Colorspace.1
DEBUG:img2pdf:width x height = 2480px x 3508px
DEBUG:img2pdf:Converting frame: 0
DEBUG:img2pdf:input dpi = 300 x 300
DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00'
DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00'
DEBUG:img2pdf:rotation = 0°
DEBUG:img2pdf:input colorspace (forced) = Colorspace.1
DEBUG:img2pdf:width x height = 2480px x 3508px
DEBUG:img2pdf:Converting monochrome to CCITT Group4
setting <module 'PIL.TiffImagePlugin' from '/usr/lib/python3/dist-packages/PIL/TiffImagePlugin.py'>.STRIP_SIZE = 1087480
DEBUG:PIL.TiffImagePlugin:Tiffinfo Keys: []
DEBUG:PIL.TiffImagePlugin:Saving using libtiff encoder
DEBUG:PIL.TiffImagePlugin:Items: [(256, 2480), (257, 3508), (258, (8,)), (259, 4), (262, 3), (273, (0, 1086240, 2172480, 3258720, 4344960, 5431200, 6517440, 7603680, 8689920)), (278, 438), (279, (1086240, 1086240, 1086240, 1086240, 1086240, 1086240, 1086240, 1086240, 9920)), (284, 1), (320, (0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280, 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280, 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280))]
DEBUG:PIL.TiffImagePlugin:Converted items: [(256, 2480), (257, 3508), (258, 8), (259, 4), (262, 3), (278, 438), (284, 1), (320, (0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280, 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280, 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280))]
Fax3SetupState: Bits/sample must be 1 for Group 3/4 encoding/decoding.
DEBUG:img2pdf:encoder error -2 when writing image file
DEBUG:img2pdf:Converting colorspace 1 to L
DEBUG:img2pdf:read_images() encoded an image as PNG

I think the bug may be due to the colorspace used. I tried forcing the colorspace using the `--colorspace` option. Surprisingly forcing the Black and White colorspace results in a PDF thats too large (the input PDF is a 1 bit Black and White image). But forcing a Grayscale colorspace results in a correclty sized PDF!? (The input PNG has metadata) ``` 128,075 1bit_force_colorspace_black_white.pdf 74,065 1bit.png ``` ``` 77,618 1bit_force_colorspace_grayscale.pdf 74,065 1bit.png ``` * Why does forcing the correct Black and White colorspace casue a larger PDF? * `img2pdf` seems to be using a `TiffImagePlugin` (on a PNG) is that part of the problem? * These errors are at the end of the Black and White colorspace conversion log: `Fax3SetupState: Bits/sample must be 1 for Group 3/4 encoding/decoding. DEBUG:img2pdf:encoder error -2 when writing image file DEBUG:img2pdf:Converting colorspace 1 to L` ### Full conversion logs #### Force Grayscale colorspace (result is correct sized PDF) ``` $ img2pdf -v -o 1bit_force_colorspace_grayscale.pdf --colorspace L 1bit.png DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13 DEBUG:PIL.PngImagePlugin:STREAM b'zTXt' 41 203 DEBUG:PIL.PngImagePlugin:STREAM b'iCCP' 256 388 DEBUG:PIL.PngImagePlugin:iCCP profile name b'ICC profile' DEBUG:PIL.PngImagePlugin:Compression method 0 DEBUG:PIL.PngImagePlugin:STREAM b'iTXt' 656 3448 DEBUG:PIL.PngImagePlugin:STREAM b'PLTE' 4116 6 DEBUG:PIL.PngImagePlugin:STREAM b'pHYs' 4134 9 DEBUG:PIL.PngImagePlugin:STREAM b'tIME' 4155 7 DEBUG:PIL.PngImagePlugin:b'tIME' 4155 7 (unknown) DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 4174 8192 DEBUG:img2pdf:PIL format = PNG DEBUG:img2pdf:imgformat = PNG DEBUG:img2pdf:input dpi = 300 x 300 DEBUG:PIL.TiffImagePlugin:tag: ImageWidth (256) - type: long (4) - value: b'\xb0\t\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: ImageLength (257) - type: long (4) - value: b'\xb4\r\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: BitsPerSample (258) - type: short (3) Tag Location: 46 - Data Location: 134 - value: b'\x08\x00\x08\x00\x08\x00' DEBUG:PIL.TiffImagePlugin:tag: Orientation (274) - type: short (3) - value: b'\x01\x00' DEBUG:PIL.TiffImagePlugin:tag: XResolution (282) - type: rational (5) Tag Location: 70 - Data Location: 140 - value: b'\xfc)\x00\x00[\x00\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: YResolution (283) - type: rational (5) Tag Location: 82 - Data Location: 148 - value: b'\xfc)\x00\x00[\x00\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: ResolutionUnit (296) - type: short (3) - value: b'\x03\x00' DEBUG:PIL.TiffImagePlugin:tag: Software (305) - type: string (2) Tag Location: 106 - Data Location: 156 - value: b'GIMP 2.10.34\x00' DEBUG:PIL.TiffImagePlugin:tag: DateTime (306) - type: string (2) Tag Location: 118 - Data Location: 170 - value: b'2023:03:29 11:59:41\x00' DEBUG:PIL.TiffImagePlugin:tag: ExifIFD (34665) - type: long (4) - value: b'\xbe\x00\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00' DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00' DEBUG:img2pdf:rotation = 0° DEBUG:img2pdf:input colorspace (forced) = Colorspace.L DEBUG:img2pdf:width x height = 2480px x 3508px DEBUG:img2pdf:Converting frame: 0 DEBUG:img2pdf:input dpi = 300 x 300 DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00' DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00' DEBUG:img2pdf:rotation = 0° DEBUG:img2pdf:input colorspace (forced) = Colorspace.L DEBUG:img2pdf:width x height = 2480px x 3508px DEBUG:img2pdf:Colorspace is OK: Colorspace.L DEBUG:img2pdf:read_images() encoded an image as PNG ``` #### Force Black and White colorspace (result is a too large sized PDF) ``` $ img2pdf -v -o 1bit_force_colorspace_black_white.pdf --colorspace 1 1bit.png DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13 DEBUG:PIL.PngImagePlugin:STREAM b'zTXt' 41 203 DEBUG:PIL.PngImagePlugin:STREAM b'iCCP' 256 388 DEBUG:PIL.PngImagePlugin:iCCP profile name b'ICC profile' DEBUG:PIL.PngImagePlugin:Compression method 0 DEBUG:PIL.PngImagePlugin:STREAM b'iTXt' 656 3448 DEBUG:PIL.PngImagePlugin:STREAM b'PLTE' 4116 6 DEBUG:PIL.PngImagePlugin:STREAM b'pHYs' 4134 9 DEBUG:PIL.PngImagePlugin:STREAM b'tIME' 4155 7 DEBUG:PIL.PngImagePlugin:b'tIME' 4155 7 (unknown) DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 4174 8192 DEBUG:img2pdf:PIL format = PNG DEBUG:img2pdf:imgformat = PNG DEBUG:img2pdf:input dpi = 300 x 300 DEBUG:PIL.TiffImagePlugin:tag: ImageWidth (256) - type: long (4) - value: b'\xb0\t\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: ImageLength (257) - type: long (4) - value: b'\xb4\r\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: BitsPerSample (258) - type: short (3) Tag Location: 46 - Data Location: 134 - value: b'\x08\x00\x08\x00\x08\x00' DEBUG:PIL.TiffImagePlugin:tag: Orientation (274) - type: short (3) - value: b'\x01\x00' DEBUG:PIL.TiffImagePlugin:tag: XResolution (282) - type: rational (5) Tag Location: 70 - Data Location: 140 - value: b'\xfc)\x00\x00[\x00\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: YResolution (283) - type: rational (5) Tag Location: 82 - Data Location: 148 - value: b'\xfc)\x00\x00[\x00\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: ResolutionUnit (296) - type: short (3) - value: b'\x03\x00' DEBUG:PIL.TiffImagePlugin:tag: Software (305) - type: string (2) Tag Location: 106 - Data Location: 156 - value: b'GIMP 2.10.34\x00' DEBUG:PIL.TiffImagePlugin:tag: DateTime (306) - type: string (2) Tag Location: 118 - Data Location: 170 - value: b'2023:03:29 11:59:41\x00' DEBUG:PIL.TiffImagePlugin:tag: ExifIFD (34665) - type: long (4) - value: b'\xbe\x00\x00\x00' DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00' DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00' DEBUG:img2pdf:rotation = 0° DEBUG:img2pdf:input colorspace (forced) = Colorspace.1 DEBUG:img2pdf:width x height = 2480px x 3508px DEBUG:img2pdf:Converting frame: 0 DEBUG:img2pdf:input dpi = 300 x 300 DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00' DEBUG:PIL.TiffImagePlugin:tag: ColorSpace (40961) - type: short (3) - value: b'\x01\x00' DEBUG:img2pdf:rotation = 0° DEBUG:img2pdf:input colorspace (forced) = Colorspace.1 DEBUG:img2pdf:width x height = 2480px x 3508px DEBUG:img2pdf:Converting monochrome to CCITT Group4 setting <module 'PIL.TiffImagePlugin' from '/usr/lib/python3/dist-packages/PIL/TiffImagePlugin.py'>.STRIP_SIZE = 1087480 DEBUG:PIL.TiffImagePlugin:Tiffinfo Keys: [] DEBUG:PIL.TiffImagePlugin:Saving using libtiff encoder DEBUG:PIL.TiffImagePlugin:Items: [(256, 2480), (257, 3508), (258, (8,)), (259, 4), (262, 3), (273, (0, 1086240, 2172480, 3258720, 4344960, 5431200, 6517440, 7603680, 8689920)), (278, 438), (279, (1086240, 1086240, 1086240, 1086240, 1086240, 1086240, 1086240, 1086240, 9920)), (284, 1), (320, (0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280, 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280, 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280))] DEBUG:PIL.TiffImagePlugin:Converted items: [(256, 2480), (257, 3508), (258, 8), (259, 4), (262, 3), (278, 438), (284, 1), (320, (0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280, 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280, 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280))] Fax3SetupState: Bits/sample must be 1 for Group 3/4 encoding/decoding. DEBUG:img2pdf:encoder error -2 when writing image file DEBUG:img2pdf:Converting colorspace 1 to L DEBUG:img2pdf:read_images() encoded an image as PNG ```
monobot commented 1 year ago
Poster

I did some more tesing and the metadata that is triggering the bug seems to be GIMP's "Save color profile" option. GIMP's default PNG options don't have that selected, but including a color profile is part of the offical PNG spec.

I did some more tesing and the metadata that is triggering the bug seems to be GIMP's "Save color profile" option. GIMP's default PNG options don't have that selected, but including a color profile is part of the offical [PNG spec](https://www.w3.org/TR/PNG/#11iCCP).
Owner

Hi, could you share a png image that causes this so that I can reproduce this problem? Thanks!

Is your bilevel png image just a palette png with only 2 palette entries? The png format itself does not support bilevel images natively.

The reason the TIFF plugin comes into play is because the CCITT fax encoding compresses bilevel image data much better than the png paeth filter does.

Hi, could you share a png image that causes this so that I can reproduce this problem? Thanks! Is your bilevel png image just a palette png with only 2 palette entries? The png format itself does not support bilevel images natively. The reason the TIFF plugin comes into play is because the CCITT fax encoding compresses bilevel image data much better than the png paeth filter does.
Owner

I've pushed some commits which will not include the ICC profile saved by GIMP for bilevel images. See #164 and https://gitlab.gnome.org/GNOME/gimp/-/issues/3438 for details.

Can you confirm that fixes this issue and then close it if it does?

I've pushed some commits which will not include the ICC profile saved by GIMP for bilevel images. See #164 and https://gitlab.gnome.org/GNOME/gimp/-/issues/3438 for details. Can you confirm that fixes this issue and then close it if it does?
josch added the
question
label 11 months ago
Poster

Hi, could you share a png image that causes this so that I can reproduce this problem? Thanks!

Sorry for the delay.
I've attached the files that I used in my first comment.
(Attached as zip file, not sure if that will work here?)

PNG with metadata
1bit.png 74,0651 bytes

Converted to PDF (much larger):
1bit.pdf 172,594 bytes

PNG but no metadata
1bit_no_metadata.png 69,971 bytes

Converted (correct size):
1bit_no_metadata.pdf 71,352 bytes

(Note that 1bit_no_metadata.png actually has resolution metadata.)

Is your bilevel png image just a palette png with only 2 palette entries? The png format itself does not support bilevel images natively.

I think so. To create a bilevel image in GIMP I use

Image > Mode > Indexed
Colormap > Use Black and White (1-bit) palette

I also have this selected Remove unused and duplicate colors from colormap in the same dialog.

I've pushed some commits ...>
Can you confirm that fixes this issue and then close it if it does?

I'm not very competent with compiling apps. I just run with what's in my distribution's repository. But I'll take a look into it.

> Hi, could you share a png image that causes this so that I can reproduce this problem? Thanks! > Sorry for the delay. I've attached the files that I used in my first comment. (Attached as zip file, ~~not sure if that will work here?~~) PNG with metadata `1bit.png 74,0651 bytes` Converted to PDF (much larger): `1bit.pdf 172,594 bytes` PNG but no metadata `1bit_no_metadata.png 69,971 bytes` Converted (correct size): `1bit_no_metadata.pdf 71,352 bytes` (Note that `1bit_no_metadata.png` actually has resolution metadata.) > Is your bilevel png image just a palette png with only 2 palette entries? The png format itself does not support bilevel images natively. I think so. To create a bilevel image in GIMP I use `Image > Mode > Indexed` `Colormap > Use Black and White (1-bit) palette` I also have this selected `Remove unused and duplicate colors from colormap` in the same dialog. > I've pushed some commits ...> > Can you confirm that fixes this issue and then close it if it does? I'm not very competent with compiling apps. I just run with what's in my distribution's repository. But I'll take a look into it.
342 KiB
josch commented 9 months ago
Owner

Aha, whoops, sorry, when you wrote:

But I'll take a look into it.

I thought that I had to wait for a follow-up. Sorry for the misunderstanding! :)

I now looked into it a bit more and I'm able to confirm your observation and understand the problem. This is indeed another instance of the GIMP bug https://gitlab.gnome.org/GNOME/gimp/-/issues/3438 but this time for PNG images and not for TIFF images. My solution for issue #164 only covered TIFF images and thus this issue is not fixed.

The PNG format supports 1-bit (bilevel) grayscale images but that's not the kind of image produced by GIMP. Instead what you showed here are palette images but with only two colors in the palette: black and white.

So the solution to this problem would be to add some code similar to what was added for TIFF which auto-detects palette PNG images with only two colors as they are created by GIMP and drop the ICC profile for those.

Aha, whoops, sorry, when you wrote: > But I'll take a look into it. I thought that I had to wait for a follow-up. Sorry for the misunderstanding! :) I now looked into it a bit more and I'm able to confirm your observation and understand the problem. This is indeed another instance of the GIMP bug https://gitlab.gnome.org/GNOME/gimp/-/issues/3438 but this time for PNG images and not for TIFF images. My solution for issue #164 only covered TIFF images and thus this issue is not fixed. The PNG format supports 1-bit (bilevel) grayscale images but that's not the kind of image produced by GIMP. Instead what you showed here are palette images but with only two colors in the palette: black and white. So the solution to this problem would be to add some code similar to what was added for TIFF which auto-detects palette PNG images with only two colors as they are created by GIMP and drop the ICC profile for those.
josch commented 9 months ago
Owner

Here is a patch that should work:

diff --git a/src/img2pdf.py b/src/img2pdf.py
index 06f2e7b..ee7acfc 100755
--- a/src/img2pdf.py
+++ b/src/img2pdf.py
@@ -1436,11 +1445,22 @@ def get_imgmetadata(
     iccp = None
     if "icc_profile" in imgdata.info:
         iccp = imgdata.info.get("icc_profile")
-    # GIMP saves bilevel tiff images with an RGB ICC profile which is useless
+    # GIMP saves bilevel TIFF images and palette PNG images with only black and
+    # white in the palette with an RGB ICC profile which is useless
+    # https://gitlab.gnome.org/GNOME/gimp/-/issues/3438
     # and produces an error in Adobe Acrobat, so we ignore it with a warning.
     # imagemagick also used to (wrongly) include an RGB ICC profile for bilevel
     # images: https://github.com/ImageMagick/ImageMagick/issues/2070
-    if iccp is not None and color == Colorspace["1"] and imgformat == ImageFormat.TIFF:
+    if iccp is not None and (
+        (color == Colorspace["1"] and imgformat == ImageFormat.TIFF)
+        or (
+            imgformat == ImageFormat.PNG
+            and color == Colorspace.P
+            and rawdata is not None
+            and parse_png(rawdata)[1]
+            in [b"\x00\x00\x00\xff\xff\xff", b"\xff\xff\xff\x00\x00\x00"]
+        )
+    ):
         with io.BytesIO(iccp) as f:
             prf = ImageCms.ImageCmsProfile(f)
         if (
@@ -1448,7 +1468,14 @@ def get_imgmetadata(
             and prf.profile.manufacturer == "GIMP"
             and prf.profile.profile_description == "GIMP built-in sRGB"
         ):
-            logger.warning("Ignoring RGB ICC profile in bilevel TIFF produced by GIMP.")
+            if imgformat == ImageFormat.TIFF:
+                logger.warning(
+                    "Ignoring RGB ICC profile in bilevel TIFF produced by GIMP."
+                )
+            elif imgformat == ImageFormat.PNG:
+                logger.warning(
+                    "Ignoring RGB ICC profile in 2-color palette PNG produced by GIMP."
+                )
             logger.warning("https://gitlab.gnome.org/GNOME/gimp/-/issues/3438")
             iccp = None
 
Here is a patch that should work: ```patch diff --git a/src/img2pdf.py b/src/img2pdf.py index 06f2e7b..ee7acfc 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -1436,11 +1445,22 @@ def get_imgmetadata( iccp = None if "icc_profile" in imgdata.info: iccp = imgdata.info.get("icc_profile") - # GIMP saves bilevel tiff images with an RGB ICC profile which is useless + # GIMP saves bilevel TIFF images and palette PNG images with only black and + # white in the palette with an RGB ICC profile which is useless + # https://gitlab.gnome.org/GNOME/gimp/-/issues/3438 # and produces an error in Adobe Acrobat, so we ignore it with a warning. # imagemagick also used to (wrongly) include an RGB ICC profile for bilevel # images: https://github.com/ImageMagick/ImageMagick/issues/2070 - if iccp is not None and color == Colorspace["1"] and imgformat == ImageFormat.TIFF: + if iccp is not None and ( + (color == Colorspace["1"] and imgformat == ImageFormat.TIFF) + or ( + imgformat == ImageFormat.PNG + and color == Colorspace.P + and rawdata is not None + and parse_png(rawdata)[1] + in [b"\x00\x00\x00\xff\xff\xff", b"\xff\xff\xff\x00\x00\x00"] + ) + ): with io.BytesIO(iccp) as f: prf = ImageCms.ImageCmsProfile(f) if ( @@ -1448,7 +1468,14 @@ def get_imgmetadata( and prf.profile.manufacturer == "GIMP" and prf.profile.profile_description == "GIMP built-in sRGB" ): - logger.warning("Ignoring RGB ICC profile in bilevel TIFF produced by GIMP.") + if imgformat == ImageFormat.TIFF: + logger.warning( + "Ignoring RGB ICC profile in bilevel TIFF produced by GIMP." + ) + elif imgformat == ImageFormat.PNG: + logger.warning( + "Ignoring RGB ICC profile in 2-color palette PNG produced by GIMP." + ) logger.warning("https://gitlab.gnome.org/GNOME/gimp/-/issues/3438") iccp = None ```
Poster

tl;dr I'm not comfortable using PIP.

I should have replied back. I don't understand PIP and what it does and what changes it makes to a system.
I've seen a few warnings about PIP being risky and a potential source of malware (obviously not your code).

I'm short of time at the moment (who isn't?!) so I can't dig into PIP some more and try to figure out what it does and what its risks are (and if I'm honest I'm not sure I want to!)
I'm not a developer, just a Linux user who has had to look under the hood a few times.

tl;dr I'm not comfortable using PIP. I should have replied back. I don't understand PIP and what it does and what changes it makes to a system. I've seen a few warnings about PIP being risky and a potential source of malware (obviously not your code). I'm short of time at the moment (who isn't?!) so I can't dig into PIP some more and try to figure out what it does and what its risks are (and if I'm honest I'm not sure I want to!) I'm not a developer, just a Linux user who has had to look under the hood a few times.
josch commented 9 months ago
Owner

@monobot no problem! I just wanted to give you the opportunity to test if those changes do what you expect. That way we reduce the chance of me fixing something that is not really the problem you observed. If you don't want to try out the diff I posted for whatever reason that's totally fine. I think and hope the diff fixes what I understood as your problem. It is obviously not your responsibility to test any diff.

I'm happy you reported this problem and hope that this is fixed with the next release!

If you don't use pip, where do you obtain img2pdf from?

@monobot no problem! I just wanted to give you the opportunity to test if those changes do what you expect. That way we reduce the chance of me fixing something that is not really the problem you observed. If you don't want to try out the diff I posted for whatever reason that's totally fine. I think and hope the diff fixes what I understood as your problem. It is obviously not your responsibility to test any diff. I'm happy you reported this problem and hope that this is fixed with the next release! If you don't use pip, where do you obtain img2pdf from?
josch closed this issue 9 months ago
Poster

@josch. Thanks for giving me the chance to test the changes.
It's not that I don't want to test the changes, it's I'm not able to!
PIP is totally new to me and I've no idea what it does (and more importantly what it does behind the scenes).

I install img2pdf using Debian's apt installer.
I just run sudo apt install img2pdf and it gets installed.

Is pip a common way for regular users to install apps? I thought it was more for developers.

Thanks for working on this issue. And thanks for img2pdf, it's an extremely useful app.

@josch. Thanks for giving me the chance to test the changes. It's not that I don't *want* to test the changes, it's I'm *not able* to! PIP is totally new to me and I've no idea what it does (and more importantly what it does behind the scenes). I install img2pdf using Debian's `apt` installer. I just run `sudo apt install img2pdf` and it gets installed. Is pip a common way for regular users to install apps? I thought it was more for developers. Thanks for working on this issue. And thanks for img2pdf, it's an extremely useful app.
josch commented 9 months ago
Owner

Then you are in luck: I'm also maintaining the img2pdf package in Debian so once I release a new version here I'll also upload that version to Debian unstable. :)

Then you are in luck: I'm also maintaining the img2pdf package in Debian so once I release a new version here I'll also upload that version to Debian unstable. :)
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: josch/img2pdf#159
Loading…
There is no content yet.