import os import random from PIL import Image, ImageDraw, ImageFont # set up the image folder and font file image_folder = 'dogetemplates/' font_file = 'impact.ttf' # get a list of all the image files in the folder image_files = [os.path.join(image_folder, f) for f in os.listdir(image_folder) if os.path.isfile(os.path.join(image_folder, f))] # set up the text parameters text_color = (255, 255, 255) text_outline_color = (0, 0, 0) text_outline_width = 2 # load the text files with open('strings.txt', 'r') as f: string_lines = f.readlines() with open('strings_toptext.txt', 'r') as f: string_toptext_lines = f.readlines() with open('strings_bottomtext.txt', 'r') as f: string_bottomtext_lines = f.readlines() # create the output folder if it doesn't exist output_folder = 'randomdoges' if not os.path.exists(output_folder): os.makedirs(output_folder) # generate a random meme image_file = random.choice(image_files) image = Image.open(image_file) draw = ImageDraw.Draw(image) # scale the font size based on a 500x500 image if image.width < 500 and image.height < 500: font_size = int(40) else: font_size = int(40 * (min(image.width, image.height) / 500)) font = ImageFont.truetype(font_file, font_size) top_text = random.choice(string_lines + string_toptext_lines).strip().upper() bottom_text = random.choice(string_lines + string_bottomtext_lines).strip().upper() text_width, text_height = draw.textsize(top_text, font) while text_width > image.width: font_size -= 1 font = ImageFont.truetype(font_file, font_size) text_width, text_height = draw.textsize(top_text, font) x = (image.width - text_width) / 2 y = 10 draw.text((x, y), top_text, font=font, fill=text_color, stroke_width=text_outline_width, stroke_fill=text_outline_color) text_width, text_height = draw.textsize(bottom_text, font) while text_width > image.width: font_size -= 1 font = ImageFont.truetype(font_file, font_size) text_width, text_height = draw.textsize(bottom_text, font) x = (image.width - text_width) / 2 y = image.height - text_height - 10 draw.text((x, y), bottom_text, font=font, fill=text_color, stroke_width=text_outline_width, stroke_fill=text_outline_color) # set the image size min_size = 225 max_size = 960 new_size = random.randint(min_size, max_size) image = image.resize((new_size, new_size)) # wrap text to new lines if it goes out of bounds max_width = int(image.width * 0.8) top_text_lines = [] bottom_text_lines = [] for text, lines in [(top_text, top_text_lines), (bottom_text, bottom_text_lines)]: words = text.split() current_line = words[0] for word in words[1:]: if draw.textsize(current_line + ' ' + word, font=font)[0] < max_width: current_line += ' ' + word else: lines.append(current_line) current_line = word lines.append(current_line) # draw the text on the image text_y = 10 for line in top_text_lines: text_width, text_height = draw.textsize(line, font) x = (image.width - text_width) / 2 draw.text((x, text_y), line, font=font, fill=text_color, stroke_width=text_outline_width, stroke_fill=text_outline_color) text_y += text_height text_y = image.height - 10 for line in reversed(bottom_text_lines): text_width, text_height = draw.textsize(line, font) x = (image.width - text_width) / 2 text_y -= text_height draw.text((x, text_y), line, font=font, fill=text_color, stroke_width=text_outline_width, stroke_fill=text_outline_color) # save the meme with an incrementing file name in the output folder file_num = len(os.listdir(output_folder)) file_name = f"randomdoge{file_num:04d}.jpg" # 4-digit padded file number output_path = os.path.join(output_folder, file_name) image.save(output_path)