Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatible with Pillow 10 and bug fix #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 17 additions & 2 deletions thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
IMAGE_ROWS = 7
PADDING = 5
FONT_SIZE = 16
IMAGE_WIDTH = 1536
IMAGE_WIDTH = 1920
FONT_NAME = "HelveticaNeue.ttc"
BACKGROUND_COLOR = "#fff"
TEXT_COLOR = "#000"
Expand All @@ -28,6 +28,15 @@ def get_random_filename(ext):
return ''.join([random.choice(string.ascii_lowercase) for _ in range(20)]) + ext


def get_text_height(font: ImageFont.FreeTypeFont, text: str) -> int:
'''Compatible operation of function ImageDraw.textsize() which has deprecated in Piilow 10.'''

ascent, descent = font.getmetrics()
text_height = (ascent + descent) * (text.count("\n") + 1)

return text_height


def create_thumbnail(filename):
print('Processing:', filename)

Expand Down Expand Up @@ -75,7 +84,7 @@ def create_thumbnail(filename):
img = Image.new("RGB", (IMAGE_WIDTH, IMAGE_WIDTH), BACKGROUND_COLOR)
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(FONT_NAME, FONT_SIZE)
_, min_text_height = draw.textsize("\n".join(metadata), font=font)
min_text_height = get_text_height(font, "\n".join(metadata))
image_width_per_img = int(round((IMAGE_WIDTH - PADDING) / IMAGE_PER_ROW)) - PADDING
image_height_per_img = int(round(image_width_per_img / width * height))
image_start_y = PADDING * 2 + min_text_height
Expand All @@ -99,6 +108,12 @@ def create_thumbnail(filename):
except Exception as e:
traceback.print_exc()
finally:
# Close PyAV object
try:
container.close()
except NameError:
pass

os.rename(random_filename, filename)
if os.path.exists(random_filename_2):
os.remove(random_filename_2)
Expand Down