Fix error in image padding logic

This commit is contained in:
Matthias Bisping 2023-01-23 14:38:36 +01:00
parent 52776494cb
commit 70802d6341

View File

@ -350,6 +350,7 @@ def superimpose_texture_with_transparency(page: Image, texture: Image) -> Image:
if page.size != texture.size:
logger.trace(f"Padding image before pasting to fit size {page.size}")
texture = pad_image_to_size(texture, page.size)
assert page.size == texture.size
assert texture.mode == "RGBA"
page.paste(texture, (0, 0), texture)
@ -360,12 +361,14 @@ def pad_image_to_size(image: Image, size: Tuple[int, int]) -> Image:
"""Pads an image to a given size."""
if image.size == size:
return image
if image.size[0] > size[0] or image.size[1] > size[1]:
raise ValueError(f"Image size {image.size} is larger than target size {size}.")
padded = Image.new(image.mode, size, color=255)
pasting_coords = compute_pasting_coordinates(image, padded)
assert image.mode == "RGBA"
padded.paste(image, pasting_coords, image)
padded.paste(image, pasting_coords)
return padded
@ -413,11 +416,11 @@ class ContentGenerator:
figure_boxes = lfilter(is_square_like, figure_boxes)
text_boxes = merge_related_rectangles(text_boxes)
text_boxes = lmap(generate_random_text_block, every_nth(2, text_boxes))
text_boxes = lmap(generate_recursive_random_table, every_nth(2, text_boxes))
tables_1, tables_1_captions = zipmap(generate_recursive_random_table_with_caption, every_nth(2, text_boxes[1:]))
# TODO: Refactor: Figures should be their own class
plots, plot_captions = zipmap(generate_random_plot_with_caption, every_nth(2, figure_boxes))
plots, plot_captions = zipmap(generate_recursive_random_table_with_caption, every_nth(2, figure_boxes))
tables_2, tables_2_captions = zipmap(
generate_recursive_random_table_with_caption, every_nth(2, figure_boxes[1:])
@ -558,7 +561,7 @@ class RecursiveRandomTable(RandomContentRectangle):
size = get_size(inner_region)
if size <= Size.SMALL.value:
words = generate_random_words(4, 6)
words = generate_random_words(1, 3)
return generate_text_block(cell, " ".join(words))
elif size <= Size.MEDIUM.value:
@ -770,7 +773,7 @@ class RandomFontPicker:
mask = lmap(lambda f: includes_pattern(f) and excludes_pattern(f), self.fonts_lower)
fonts = itertools.compress(self.fonts, mask)
fonts = keep(map(self.load_font, fonts))
fonts = filter(self.font_is_renderable, fonts) # FIXME: this does not work
# fonts = filter(self.font_is_renderable, fonts) # FIXME: this does not work
font = first(fonts)
logger.info(f"Using font: {font.getname()}")
@ -819,20 +822,10 @@ def pick_random_mono_space_font_available_on_system(**kwargs):
@lru_cache(maxsize=None)
def pick_random_font_available_on_system(**kwargs):
# kwargs["excludes"] = (
# *kwargs.get(
# "excludes",
# ),
# "hebrew",
# "armenian", # TODO: De-hardcode / find a better way to filter out fonts that can not be rendered
# "arabic",
# "cyrillic",
# "georgian",
# "greek",
# "telugu",
# "tamil",
# "thai",
# "vietnamese",
# "ethiopic",
# *kwargs.get(
# "excludes",
# ),
# "padauk",
# )
font_picker = get_font_picker(**omit(kwargs, ["includes", "excludes"]))
return font_picker.pick_random_font_available_on_system(**project(kwargs, ["includes", "excludes"]))
@ -912,11 +905,7 @@ class RandomPlot(RandomContentRectangle):
fig.set_size_inches(rectangle.width / 100, rectangle.height / 100)
fig.tight_layout(pad=0)
plot_fn(
x,
y,
**plot_kwargs,
)
plot_fn(x, y, **plot_kwargs)
ax.set_facecolor("none")
maybe() and ax.set_title("Figure Title")
@ -971,7 +960,7 @@ def generate_random_caption(rectangle: Rectangle, caption_start, n_sentences=100
text_generator=CaptionGenerator(caption_start=caption_start),
font=pick_random_font_available_on_system(
size=8,
includes=("italic"),
includes=("italic",),
excludes=("bold", "mono"),
),
font_size=5, # TODO: De-hardcode font size... Seems to have no effect on top of that
@ -985,9 +974,9 @@ def generate_text_block(rectangle: Rectangle, text) -> ContentRectangle:
block = TextBlock(
*rectangle.coords,
font=pick_random_font_available_on_system(
size=11, # TODO: De-hardcode font size... Seems to have no effect on top of that
includes=("serif", "sans-serif"),
excludes=("bold", "mono", "italic", "oblique", "cursive"),
size=30, # TODO: De-hardcode font size... Seems to have no effect on top of that
includes=("serif", "sans-serif", "bold"),
excludes=("mono", "italic", "oblique", "cursive"),
),
)
block.content = rectangle.content if isinstance(rectangle, ContentRectangle) else None # TODO: Refactor
@ -1178,8 +1167,8 @@ class PagePartitioner:
return boxes
def draw_boxes(self, page: Image, boxes: Iterable[Rectangle]):
image = draw_rectangles(page, boxes, filled=False, annotate=True)
show_image(image)
# page = draw_rectangles(page, boxes, filled=False, annotate=True)
show_image(page)
def generate_content_boxes(self, box: Rectangle, depth=0):
if depth >= self.max_depth: