1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-05 08:27:22 +03:00

Handle images less than 32×32

This commit is contained in:
Guillaume Ayoub 2020-06-07 17:05:12 +02:00
parent 2a1c33d0ba
commit a673ad113c

View File

@ -232,6 +232,8 @@ class Context(pydyf.Stream):
elif image_mode == 'CMYK': elif image_mode == 'CMYK':
color_space = '/DeviceCMYK' color_space = '/DeviceCMYK'
# TODO: Handle alpha layers
# TODO: Handle different modes for different output formats
if image_mode in ('RGBA', 'P'): if image_mode in ('RGBA', 'P'):
pillow_image = pillow_image.convert('RGB') pillow_image = pillow_image.convert('RGB')
@ -241,14 +243,25 @@ class Context(pydyf.Stream):
'Width': pillow_image.width, 'Width': pillow_image.width,
'Height': pillow_image.height, 'Height': pillow_image.height,
'ColorSpace': color_space, 'ColorSpace': color_space,
'BitsPerComponent': 8,
'Filter': '/DCTDecode' if image_format == 'JPEG' else '/JPXDecode',
}) })
save_format = 'jpeg' if image_format == 'JPEG' else 'jpeg2000'
image_file = io.BytesIO() image_file = io.BytesIO()
pillow_image.save(image_file, format=save_format) if image_format == 'JPEG':
xobject = pydyf.Stream([image_file.getvalue()], extra=extra) extra['Filter'] = '/DCTDecode'
pillow_image.save(image_file, format='JPEG')
stream = [image_file.getvalue()]
else:
if pillow_image.width >= 32 and pillow_image.height >= 32:
# JPEG2000 files cant be less than 32×32
extra['Filter'] = '/JPXDecode',
pillow_image.save(image_file, format='JPEG2000')
stream = [image_file.getvalue()]
else:
extra['BitsPerComponent'] = 8
stream = [bytes([
i for pixel in pillow_image.getdata() for i in pixel])]
xobject = pydyf.Stream(stream, extra=extra)
image_name = f'Im{len(self._x_objects)}' image_name = f'Im{len(self._x_objects)}'
self._x_objects[image_name] = xobject self._x_objects[image_name] = xobject
return image_name return image_name