Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
NotCookey authored Dec 11, 2021
1 parent 2ade6f4 commit 6a9654a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Quote2Image.py
Original file line number Diff line number Diff line change
@@ -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
Binary file added __pycache__/Quote2Image.cpython-37.opt-2.pyc
Binary file not shown.
11 changes: 11 additions & 0 deletions examples/quote to image.py
Original file line number Diff line number Diff line change
@@ -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
Binary file added fonts/Coves Bold.otf
Binary file not shown.

0 comments on commit 6a9654a

Please sign in to comment.