Training SDXL to Generate Text Using IA3 LoRA

Resources:

Test IA3 Text-Image Adapters:
https://huggingface.co/AOLCDROM/iA3-adapters

More about the Prodigy optimizer:
https://github.com/konstmish/prodigy

More about the IA3 PEFT adapter:
https://huggingface.co/docs/peft/conceptual_guides/ia3

T-shirts dataset:
https://www.kaggle.com/datasets/sunnykusawa/tshirts

More on Pangrams:
https://www.prdaily.com/16-clever-pangrams-for-word-lovers/

Font File to .PNG Image Script:

import glob
import random
from random import randrange
import os
from PIL import Image, ImageDraw, ImageFont
from string import ascii_letters
import textwrap
# Output image resolution
width = 768
height = 768

phraselist = ["the five boxing wizards jump quickly","THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG","0123456789"]

# Directory with .ttf fonts
searchdir = "c:\\fonts2\\"
# Directory for .png and .caption output
outdir = "c:\\fonts-symbols-numbers\\"

fonts = glob.glob(searchdir + "*.ttf")

# Colors list from:
# https://matplotlib.org/stable/gallery/color/named_colors.html
bgcolors = ['black','white','beige','whitesmoke','azure','dimgrey','ghostwhite','linen','lightsteelblue','aliceblue','seashell']
textcolors = ['gold','crimson','steelblue','darkgoldenrod','black','orange','darkslateblue','seagreen','white','whitesmoke',]

for fontfile in fonts:
# Get font base filename for output filename
    onlyfile=os.path.basename(fontfile)
    filebase = os.path.splitext(onlyfile)[0]
    for phrase in phraselist:
        print(fontfile)
        text = phrase
        bgcolor = random.choice(bgcolors)
        tcolor = random.choice(textcolors)

        # Text wrapping method from:
        # https://www.alpharithms.com/fit-custom-font-wrapped-text-image-python-pillow-552321/
        font = ImageFont.truetype(fontfile, size=randrange(12,60))
        img = Image.new('RGB', (width, height), color=bgcolor)
        draw = ImageDraw.Draw(im=img)
        avg_char_width = sum(font.getsize(char)[0] for char in ascii_letters) / len(ascii_letters)
        # Translate this average length into a character count
        max_char_count = int(img.size[0] * .618 / avg_char_width)
        # Create a wrapped text object using scaled character count
        text = textwrap.fill(text=text, width=max_char_count)
        # Add text to the image
        #draw.text(xy=(img.size[0]/2, img.size[1] / 2), text=text, font=font, fill=(randrange(0,255), randrange(0,255), randrange(0,255),randrange(0,255)), anchor='mm')
        draw.text(xy=(img.size[0]/2, img.size[1] / 2), text=text, font=font, fill=tcolor, anchor='mm')
        phrasesan = phrase.replace(" ","-")
        fullnamepng = filebase + "-" + bgcolor + ".png"
        textout = filebase + "-" + bgcolor + ".caption"
        fulltext = "text ***"+phrase+"***,"
        # Rotate text portion of image with random factor, fill empty space with same background color as earlier
        new_img = img.rotate(randrange(-15,15), expand = 1,resample=Image.BICUBIC,fillcolor=bgcolor)
        new2 = new_img.resize((768, 768), Image.Resampling.LANCZOS)
        new2.save(outdir+fullnamepng)
        with open(outdir+textout, mode="w", encoding="UTF-8") as f:
            f.writelines(fulltext)

Leave a Reply

Your email address will not be published. Required fields are marked *