diff --git a/Quote2Image.py b/Quote2Image.py new file mode 100644 index 0000000..5e58620 --- /dev/null +++ b/Quote2Image.py @@ -0,0 +1,70 @@ +from PIL import Image, ImageDraw, ImageFont +import math, random + +x1 = 612 +y1 = 612 + +def auto_color(): + d = 0 + rgbGenerator = lambda: ( + random.randint(0, 255), + random.randint(0, 255), + random.randint(0, 255), + 100, + ) + color = rgbGenerator() + luminance = (0.299 * color[0] + 0.587 * color[1] + 0.114 * color[2]) / 255 + if luminance > 0.5: + d = 0 + else: + d = 255 + return [(d, d, d), color] + + +def convert(quote, author, fg=None, bg=None): + if bg: + bg = bg + else: + bg = (0, 0, 0) + if fg: + fg = fg + else: + fg = (255, 255, 255) + sentence = f"{quote} - {author}" + + quote = ImageFont.truetype("fonts/Coves Bold.otf", 27) + + img = Image.new("RGB", (x1, y1), color=bg) + d = ImageDraw.Draw(img) + + sum = 0 + for letter in sentence: + sum += d.textsize(letter, font=quote)[0] + average_length_of_letter = sum / len(sentence) + + number_of_letters_for_each_line = (x1 / 1.618) / average_length_of_letter + incrementer = 0 + fresh_sentence = "" + + for letter in sentence: + if letter == "-": + fresh_sentence += "\n\n" + letter + elif incrementer < number_of_letters_for_each_line: + fresh_sentence += letter + else: + if letter == " ": + fresh_sentence += "\n" + incrementer = 0 + else: + fresh_sentence += letter + incrementer += 1 + dim = d.textsize(fresh_sentence, font=quote) + x2 = dim[0] + y2 = dim[1] + + qx = x1 / 2 - x2 / 2 + qy = y1 / 2 - y2 / 2 + + d.text((qx, qy), fresh_sentence, align="center", font=quote, fill=fg) + + return img diff --git a/__pycache__/Quote2Image.cpython-37.opt-2.pyc b/__pycache__/Quote2Image.cpython-37.opt-2.pyc new file mode 100644 index 0000000..cbce3b6 Binary files /dev/null and b/__pycache__/Quote2Image.cpython-37.opt-2.pyc differ diff --git a/examples/quote to image.py b/examples/quote to image.py new file mode 100644 index 0000000..f3f657e --- /dev/null +++ b/examples/quote to image.py @@ -0,0 +1,11 @@ +from Quote2Image import auto_color, convert + +# Automatically Generates a Perfect Match of Colors for Fore and Back +color=auto_color() + +img=convert(quote="Pooing keeps you healthy",author="Pee",fg=color[0],bg=color[1]) + +# Save The Image as a Png file +img.save("quote.png") + +# Everytime you run this code it will generate a quote.png file with a different background and foreground \ No newline at end of file diff --git a/fonts/Coves Bold.otf b/fonts/Coves Bold.otf new file mode 100644 index 0000000..1e064ec Binary files /dev/null and b/fonts/Coves Bold.otf differ