# This script assumes that all of your files are in PNG format and# that there is a single unweighted 'by artist' in the entire prompt.# CONFIGFONT_PATH="C:\Windows\Fonts\Arial.ttf"FONT_SIZE=60TEXT_COLOR="white"TEXT_STROKE_WIDTH=6TEXT_STROKE_COLOR="black"TEXT_ORIGIN=(8,8)OUTPUT_DIR="overlay"KEEP_METADATA=True# SCRIPTimportsys,os,refromPILimportImage,ImageDraw,ImageFontfromPIL.PngImagePluginimportPngInfoartistRegex=re.compile("by (.+?)(?:,|$)")font=ImageFont.truetype(FONT_PATH,FONT_SIZE)# make sure the output directory exists and is a directoryifos.path.exists(OUTPUT_DIR):ifnotos.path.isdir(OUTPUT_DIR):raiseValueError(f"OUTPUT_DIR ({OUTPUT_DIR}) exists and is not a directory.")else:os.mkdir(OUTPUT_DIR)# get a list of all of the pngs in the current directoryfilenames=[xforxinos.listdir('.')ifos.path.isfile(x)andx.endswith('.png')]# create all of the overlayed imagesforfilenameinfilenames:try:print(filename,end=" ",flush=True)image=Image.open(filename)# get the artist name from the metadataparams=image.text["parameters"]match=re.search(artistRegex,params)ifnotmatch:print("!!! artist not found !!!")continueartist=match.group(1)# overlay the artist namedraw=ImageDraw.Draw(image)draw.text(TEXT_ORIGIN,artist,fill=TEXT_COLOR,stroke_fill=TEXT_STROKE_COLOR,stroke_width=TEXT_STROKE_WIDTH,font=font)# create a new PngInfo object with the metadata from current filemetadata=PngInfo()ifKEEP_METADATA:forkey,valueinimage.text.items():metadata.add_text(key,value)image.save(f"{OUTPUT_DIR}/{os.path.basename(filename)}",pnginfo=metadata)print(f"by {artist}")exceptExceptionase:print(repr(e))
Warning
LINK
You are about to visit a link which has been flagged with the above content warnings. Do you wish to continue?