Design system.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

code.py 3.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import os
  2. import random
  3. from PIL import Image, ImageDraw, ImageFont
  4. # set up the image folder and font file
  5. image_folder = 'dogetemplates/'
  6. font_file = 'impact.ttf'
  7. # get a list of all the image files in the folder
  8. 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))]
  9. # set up the text parameters
  10. text_color = (255, 255, 255)
  11. text_outline_color = (0, 0, 0)
  12. text_outline_width = 2
  13. # load the text files
  14. with open('strings.txt', 'r') as f:
  15. string_lines = f.readlines()
  16. with open('strings_toptext.txt', 'r') as f:
  17. string_toptext_lines = f.readlines()
  18. with open('strings_bottomtext.txt', 'r') as f:
  19. string_bottomtext_lines = f.readlines()
  20. # create the output folder if it doesn't exist
  21. output_folder = 'randomdoges'
  22. if not os.path.exists(output_folder):
  23. os.makedirs(output_folder)
  24. # generate a random meme
  25. image_file = random.choice(image_files)
  26. image = Image.open(image_file)
  27. draw = ImageDraw.Draw(image)
  28. # scale the font size based on a 500x500 image
  29. if image.width < 500 and image.height < 500:
  30. font_size = int(40)
  31. else:
  32. font_size = int(40 * (min(image.width, image.height) / 500))
  33. font = ImageFont.truetype(font_file, font_size)
  34. top_text = random.choice(string_lines + string_toptext_lines).strip().upper()
  35. bottom_text = random.choice(string_lines + string_bottomtext_lines).strip().upper()
  36. text_width, text_height = draw.textsize(top_text, font)
  37. while text_width > image.width:
  38. font_size -= 1
  39. font = ImageFont.truetype(font_file, font_size)
  40. text_width, text_height = draw.textsize(top_text, font)
  41. x = (image.width - text_width) / 2
  42. y = 10
  43. draw.text((x, y), top_text, font=font, fill=text_color, stroke_width=text_outline_width, stroke_fill=text_outline_color)
  44. text_width, text_height = draw.textsize(bottom_text, font)
  45. while text_width > image.width:
  46. font_size -= 1
  47. font = ImageFont.truetype(font_file, font_size)
  48. text_width, text_height = draw.textsize(bottom_text, font)
  49. x = (image.width - text_width) / 2
  50. y = image.height - text_height - 10
  51. draw.text((x, y), bottom_text, font=font, fill=text_color, stroke_width=text_outline_width, stroke_fill=text_outline_color)
  52. # set the image size
  53. min_size = 225
  54. max_size = 960
  55. new_size = random.randint(min_size, max_size)
  56. image = image.resize((new_size, new_size))
  57. # wrap text to new lines if it goes out of bounds
  58. max_width = int(image.width * 0.8)
  59. top_text_lines = []
  60. bottom_text_lines = []
  61. for text, lines in [(top_text, top_text_lines), (bottom_text, bottom_text_lines)]:
  62. words = text.split()
  63. current_line = words[0]
  64. for word in words[1:]:
  65. if draw.textsize(current_line + ' ' + word, font=font)[0] < max_width:
  66. current_line += ' ' + word
  67. else:
  68. lines.append(current_line)
  69. current_line = word
  70. lines.append(current_line)
  71. # draw the text on the image
  72. text_y = 10
  73. for line in top_text_lines:
  74. text_width, text_height = draw.textsize(line, font)
  75. x = (image.width - text_width) / 2
  76. draw.text((x, text_y), line, font=font, fill=text_color, stroke_width=text_outline_width, stroke_fill=text_outline_color)
  77. text_y += text_height
  78. text_y = image.height - 10
  79. for line in reversed(bottom_text_lines):
  80. text_width, text_height = draw.textsize(line, font)
  81. x = (image.width - text_width) / 2
  82. text_y -= text_height
  83. draw.text((x, text_y), line, font=font, fill=text_color, stroke_width=text_outline_width, stroke_fill=text_outline_color)
  84. # save the meme with an incrementing file name in the output folder
  85. file_num = len(os.listdir(output_folder))
  86. file_name = f"randomdoge{file_num:04d}.jpg" # 4-digit padded file number
  87. output_path = os.path.join(output_folder, file_name)
  88. image.save(output_path)