From df7070eca22278b25c921ef72d3f97a221d66242 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 11 May 2023 10:06:19 +0300 Subject: [PATCH] Deduplicate get_font code --- modules/images.py | 13 +++++++------ modules/textual_inversion/image_embedding.py | 9 ++------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/modules/images.py b/modules/images.py index c4e98c75..d8527179 100644 --- a/modules/images.py +++ b/modules/images.py @@ -24,6 +24,13 @@ from modules.shared import opts LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) +def get_font(fontsize: int): + try: + return ImageFont.truetype(opts.font or Roboto, fontsize) + except Exception: + return ImageFont.truetype(Roboto, fontsize) + + def image_grid(imgs, batch_size=1, rows=None): if rows is None: if opts.n_rows > 0: @@ -142,12 +149,6 @@ def draw_grid_annotations(im, width, height, hor_texts, ver_texts, margin=0): lines.append(word) return lines - def get_font(fontsize): - try: - return ImageFont.truetype(opts.font or Roboto, fontsize) - except Exception: - return ImageFont.truetype(Roboto, fontsize) - def draw_texts(drawing, draw_x, draw_y, lines, initial_fnt, initial_fontsize): for line in lines: fnt = initial_fnt diff --git a/modules/textual_inversion/image_embedding.py b/modules/textual_inversion/image_embedding.py index d85a4888..5858a55f 100644 --- a/modules/textual_inversion/image_embedding.py +++ b/modules/textual_inversion/image_embedding.py @@ -3,9 +3,7 @@ import json import numpy as np import zlib from PIL import Image, ImageDraw, ImageFont -from fonts.ttf import Roboto import torch -from modules.shared import opts class EmbeddingEncoder(json.JSONEncoder): @@ -136,11 +134,8 @@ def caption_image_overlay(srcimage, title, footerLeft, footerMid, footerRight, t image = srcimage.copy() fontsize = 32 if textfont is None: - try: - textfont = ImageFont.truetype(opts.font or Roboto, fontsize) - textfont = opts.font or Roboto - except Exception: - textfont = Roboto + from modules.images import get_font + textfont = get_font(fontsize) factor = 1.5 gradient = Image.new('RGBA', (1, image.size[1]), color=(0, 0, 0, 0))