1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-04 16:07:57 +03:00

Use image id instead of increasing index for image keys

Pillow images are stored in a cache, to be sure that we use the same image when
we have the same URL (and don’t try to retrieve it twice).

Using the image id as a key prevents WeasyPrint to store the image twice in the
PDF, with a large gain of time and size.

Related to #1392.
This commit is contained in:
Guillaume Ayoub 2021-07-12 21:40:33 +02:00
parent fa07d36fb0
commit 04985cb5ab

View File

@ -262,6 +262,11 @@ class Stream(pydyf.Stream):
return image_file
def add_image(self, pillow_image, image_rendering, optimize_size):
image_name = f'i{id(pillow_image)}'
if image_name in self._x_objects:
# Reuse image already stored in stream
return image_name
if 'transparency' in pillow_image.info:
pillow_image = pillow_image.convert('RGBA')
elif pillow_image.mode in ('1', 'P'):
@ -313,7 +318,6 @@ class Stream(pydyf.Stream):
stream = [image_file.getvalue()]
xobject = pydyf.Stream(stream, extra=extra)
image_name = f'i{len(self._x_objects)}'
self._x_objects[image_name] = xobject
return image_name