Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions border.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import math
from enum import Enum
from dataclasses import dataclass
from PIL import Image
from PIL import Image, ImageFont, ImageDraw
import text as tm

class BorderType(Enum):
Expand Down Expand Up @@ -123,11 +123,29 @@ def draw_border(img: Image, border: Border) -> Image:

return canvas

def draw_exif(img: Image, exif: dict, border: Border, fontpath: str, boldfontpath: str) -> Image:
def draw_exif(img: Image, exif: dict, border: Border, fontpath: str, boldfontpath: str, max_width: int = None) -> Image:
centered = border.border_type in (BorderType.POLAROID, BorderType.LARGE, BorderType.INSTAGRAM)
multiplier = 0.2 if centered else 0.5
font_size = tm.get_optimal_font_size("Test string", border.bottom * multiplier, fontpath)
heading_font_size = tm.get_optimal_font_size("Test string", border.bottom * (multiplier + 0.02), boldfontpath)

if max_width and not centered:
exif_lines = [
f"{exif['Make']} {exif['Model']}",
f"{exif['LensMake']} {exif['LensModel']}",
f"{exif['FocalLength']} {exif['FNumber']} {exif['ISOSpeedRatings']} {exif['ExposureTime']}",
]
dummy = ImageDraw.Draw(img)
while font_size > 1:
test_heading = ImageFont.truetype(boldfontpath, heading_font_size)
test_font = ImageFont.truetype(fontpath, font_size)
widths = [dummy.textlength(exif_lines[0], font=test_heading)] + \
[dummy.textlength(line, font=test_font) for line in exif_lines[1:]]
if max(widths) <= max_width:
break
font_size -= 1
heading_font_size = max(1, heading_font_size - 1)

font = tm.create_font(font_size, fontpath)
heading_font = tm.create_font(heading_font_size, boldfontpath)

Expand Down
17 changes: 11 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ def process_image(path: str, add_exif: bool, add_palette: bool, border_type: Bor
img_with_border = draw_border(img, border)
save_as = f'{filename}_border-{border.border_type}'

exif_max_width = None

if add_palette:
palette_size = round(border.bottom / 3)
color_palette = load_image_color_palette(img, palette_size)
# Position palette on right side of bottom border
palette_x = img_with_border.width - border.right - color_palette.width
palette_y = img_with_border.height - round(border.bottom / 2) - round(color_palette.height / 2)
exif_max_width = palette_x - border.left

if add_exif:
exif = get_exif(img)
if exif:
Expand All @@ -89,15 +99,10 @@ def process_image(path: str, add_exif: bool, add_palette: bool, border_type: Bor
if len(error_messages) > 0:
raise ValueError(error_messages)

img_with_border = draw_exif(img_with_border, exif, border, font_path, bold_font_path)
img_with_border = draw_exif(img_with_border, exif, border, font_path, bold_font_path, max_width=exif_max_width)
save_as = f'{save_as}_exif'

if add_palette:
palette_size = round(border.bottom / 3)
color_palette = load_image_color_palette(img, palette_size)
# Position palette on right side of bottom border
palette_x = img_with_border.width - border.right - color_palette.width
palette_y = img_with_border.height - round(border.bottom / 2) - round(color_palette.height / 2)
img_with_border = overlay_palette(img=img_with_border,
color_palette=color_palette,
offset=(palette_x, palette_y))
Expand Down