|
8 | 8 | from fontTools import subset
|
9 | 9 | from fontTools.ttLib import TTFont, TTLibError, ttFont
|
10 | 10 | from fontTools.varLib.mutator import instantiateVariableFont
|
| 11 | +from PIL import Image |
11 | 12 |
|
12 | 13 | from ..logger import LOGGER
|
13 | 14 | from ..matrix import Matrix
|
@@ -361,13 +362,24 @@ def add_group(self, x, y, width, height):
|
361 | 362 | self._x_objects[group.id] = group
|
362 | 363 | return group
|
363 | 364 |
|
364 |
| - def add_image(self, image, width, height, interpolate): |
365 |
| - image_name = f'i{image.id}{width}{height}{interpolate}' |
| 365 | + def add_image(self, image, width, height, interpolate, ratio): |
| 366 | + image_name = f'i{image.id}{width}{height}{interpolate}{ratio}' |
366 | 367 | self._x_objects[image_name] = None # Set by write_pdf
|
367 | 368 | if image_name in self._images:
|
368 | 369 | # Reuse image already stored in document
|
369 | 370 | return image_name
|
370 | 371 |
|
| 372 | + if ratio != 1: |
| 373 | + thumbnail = Image.open(io.BytesIO(image.image_data.data)) |
| 374 | + width = int(round(image.width * ratio)) |
| 375 | + height = int(round(image.height * ratio)) |
| 376 | + thumbnail.thumbnail((max(1, width), max(1, height))) |
| 377 | + image_file = io.BytesIO() |
| 378 | + thumbnail.save( |
| 379 | + image_file, format=thumbnail.format, optimize=image.optimize) |
| 380 | + width, height = thumbnail.width, thumbnail.height |
| 381 | + image.image_data = image.cache_image_data(image_file.getvalue()) |
| 382 | + |
371 | 383 | xobject = image.get_xobject(width, height, interpolate)
|
372 | 384 | self._images[image_name] = xobject
|
373 | 385 | return image_name
|
|
0 commit comments