Skip to content

Commit

Permalink
Run color inversion logic code when image instance is created
Browse files Browse the repository at this point in the history
The attribute name is more readable this way.
  • Loading branch information
liZe committed Jun 8, 2024
1 parent 4ce48a4 commit 1efe1b6
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions weasyprint/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ def __init__(self, pillow_image, image_id, image_data, filename=None,
self.height = pillow_image.height
self.ratio = (self.width / self.height) if self.height != 0 else inf
self.optimize = optimize = options['optimize_images']
self.app14 = getattr(original_pillow_image, 'app', {}).get('APP14')

# The presence of the APP14 segment indicates an Adobe image with
# inverted CMYK data. Specify a Decode Array to invert it again back to
# normal. See https://github.com/Kozea/WeasyPrint/pull/2179.
app14 = getattr(original_pillow_image, 'app', {}).get('APP14')
self.invert_colors = self.mode == 'CMYK' and app14 is not None

if pillow_image.format in ('JPEG', 'MPO'):
self.format = 'JPEG'
Expand Down Expand Up @@ -151,11 +156,8 @@ def get_x_object(self, interpolate, dpi_ratio):
})

if self.format == 'JPEG':
if self.mode == 'CMYK' and self.app14 is not None:
# The presence of the APP14 segment indicates an Adobe image
# with inverted CMYK data. Specify a Decode Array to invert
# it again back to normal.
extra['Decode'] = '[1 0 1 0 1 0 1 0]'
if self.invert_colors:
extra['Decode'] = pydyf.Array((1, 0) * 4)
extra['Filter'] = '/DCTDecode'
return pydyf.Stream([self.image_data], extra)

Expand Down

0 comments on commit 1efe1b6

Please sign in to comment.