A python module to convert text quotes into graphical images
++ +## Installation +**To install Quote2Image, you can use `pip`:** +```bash +pip install Quote2Image +``` + +## Usage +**The convert function takes the following arguments:** + +- **`quote` : The quote to convert.** +- **`author` : The author of the quote.** +- **`fg` : The foreground color of the text.** +- **`bg` : The background color of the image.** +- **`font_type` : The font to use for the text.** +- **`font_size` : The font size to use for the text.** +- **`width` : The width of the image.** +- **`height` : The height of the image.** + +**Generating an image using RGB background and foreground, The package comes with a builtin `GenerateColors` function that generates a fg and bg color with the correct amount of luminosity and returns them in tuples.** + +```python +from Quote2Image import Convert, GenerateColors + +# Generate Fg and Bg Color +fg, bg = GenerateColors() + +img=Convert( + quote="Pooing keeps you healthy", + author="Pee", + fg=fg, + bg=bg, + font_size=32, + font_type="arial.ttf", + width=1080, + height=450) + +# Save The Image as a Png file +img.save("hello.png") +``` +**Generating an image using a custom background image. We can do that using the `ImgObject` that gives us alot of flexibility on how we want our background Image to be.** + +**The `ImgObject` class takes the following arguments:** + +- **`image` : The link to the background image (required).** +- **`brightness` : The brightness of the image (optional, default is 100).** +- **`blur` : The blur of the image (optional, default is 0).** + +**You can then use the `ImgObject` instance as the bg argument in the convert function:** + +```py +from Quote2Image import Convert, ImgObject + +bg=ImgObject(image="IMAGE FILE LOCATION", brightness=80, blur=80) + +img=Convert( + quote="Pooing keeps you healthy", + author="Pee", + fg=(21, 21, 21), + bg=bg, + font_size=32, + font_type="arial.ttf", + width=1080, + height=450) + +# Save The Image as a Png file +img.save("hello.png") +``` + +## Permissions + +- **You are allowed to use, modify, and distribute the module.** +- **You are allowed to distribute modified versions of the module, as long as you follow the terms of the license.** + +## Obligations + +- **You must include a copy of the GPL-3.0 license with the module.** +- **You must provide a copy of the source code of the module, either along with the modified version of the module or through a written offer to provide the source code.** +- **You must provide a prominent notice stating that you have modified the module, and the date of the modification.** +- **If you distribute the module, you must do so under the terms of the GPL-3.0 license.** + + +# That's It! +> **Thank You! Hope this was useful to you <3** diff --git a/Quote2Image.egg-info/SOURCES.txt b/Quote2Image.egg-info/SOURCES.txt new file mode 100644 index 0000000..0f4ee14 --- /dev/null +++ b/Quote2Image.egg-info/SOURCES.txt @@ -0,0 +1,11 @@ +LICENSE +README.md +setup.cfg +setup.py +Quote2Image/Quote2Image.py +Quote2Image/__init__.py +Quote2Image.egg-info/PKG-INFO +Quote2Image.egg-info/SOURCES.txt +Quote2Image.egg-info/dependency_links.txt +Quote2Image.egg-info/requires.txt +Quote2Image.egg-info/top_level.txt \ No newline at end of file diff --git a/Quote2Image.egg-info/dependency_links.txt b/Quote2Image.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Quote2Image.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/Quote2Image.egg-info/requires.txt b/Quote2Image.egg-info/requires.txt new file mode 100644 index 0000000..a6d4d60 --- /dev/null +++ b/Quote2Image.egg-info/requires.txt @@ -0,0 +1 @@ +Pillow==9.2.0 diff --git a/Quote2Image.egg-info/top_level.txt b/Quote2Image.egg-info/top_level.txt new file mode 100644 index 0000000..32ea903 --- /dev/null +++ b/Quote2Image.egg-info/top_level.txt @@ -0,0 +1 @@ +Quote2Image diff --git a/Quote2Image.py b/Quote2Image.py deleted file mode 100644 index 02e1494..0000000 --- a/Quote2Image.py +++ /dev/null @@ -1,58 +0,0 @@ -from PIL import Image, ImageDraw, ImageFont, ImageFilter -import random, math - -def convert(quote, author, fg, image, border_color, font_file=None, font_size=None,width=None,height=None): - x1 = width if width else 612 - y1 = height if height else 612 - - sentence = f"{quote} - {author}" - - quote = ImageFont.truetype(font_file if font_file else "fonts/Coves Bold.otf", font_size if font_size else 32) - - img = Image.new("RGB", (x1, y1), color=(255,255,255)) - - back = Image.open(image, 'r') - img_w, img_h = back.size - bg_w, bg_h = img.size - offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2) - bback=back.filter(ImageFilter.BLUR) - img.paste(bback, offset) - - 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-1, qy-1), fresh_sentence, align="center", font=quote, fill=border_color) - d.text((qx+1, qy-1), fresh_sentence, align="center", font=quote, fill=border_color) - d.text((qx-1, qy+1), fresh_sentence, align="center", font=quote, fill=border_color) - d.text((qx+1, qy+1), fresh_sentence, align="center", font=quote, fill=border_color) - - d.text((qx, qy), fresh_sentence, align="center", font=quote, fill=fg) - - return img diff --git a/Quote2Image/Quote2Image.py b/Quote2Image/Quote2Image.py new file mode 100644 index 0000000..4381ad9 --- /dev/null +++ b/Quote2Image/Quote2Image.py @@ -0,0 +1,82 @@ +from PIL import Image, ImageDraw, ImageFont, ImageEnhance, ImageFilter +import random + + +class ImgObject: + def __init__(self, image, brightness=100, blur=0): + self.image = image + self.brightness = brightness + self.blur = blur + + def __repr__(self): + return f"ImgObject(image='{self.image}', brightness={self.brightness}, blur={self.blur})" + + +def GenerateColors(): + foreground_color = ( + random.randint(0, 255), + random.randint(0, 255), + random.randint(0, 255), + ) + + background_color = ( + random.randint(0, 255), + random.randint(0, 255), + random.randint(0, 255), + ) + + while abs(sum(foreground_color) - sum(background_color)) < (255 * 3) / 2: + background_color = ( + random.randint(0, 255), + random.randint(0, 255), + random.randint(0, 255), + ) + + return foreground_color, background_color + + +def Convert(quote, author, fg, bg, font_type, font_size, width, height): + if isinstance(bg, ImgObject): + image = Image.open(bg.image).resize((width, height)) + enhancer = ImageEnhance.Brightness(image) + image = enhancer.enhance(bg.brightness / 100) + if bg.blur != 0: + image = image.filter(ImageFilter.BoxBlur(bg.blur)) + else: + image = Image.new("RGB", (width, height), bg) + + draw = ImageDraw.Draw(image) + + font = ImageFont.truetype(font_type, font_size) + + lines = [] + line = "" + for word in quote.split(): + line_width = draw.textsize(line + " " + word, font)[0] + if line_width > width-40: + lines.append(line) + line = word + else: + line += " " + word + lines.append(line) + + quote_height = sum([draw.textsize(line, font)[1] for line in lines]) + y = (height - quote_height - font_size) // 2 + + for line in lines: + line_width = draw.textsize(line, font)[0] + x = (width - line_width) // 2 + draw.text((x, y), line, fg, font=font) + y += draw.textsize(line, font)[1] + + dash_width = draw.textsize(" - ", font)[0] + x = (width - dash_width) // 2 + y += font_size // 2 + draw.text((x, y), " - ", fg, font=font) + y += font_size // 2 + + author_width = draw.textsize(author, font)[0] + x = (width - author_width) // 2 + draw.text((x, y+15), author, fg, font=font) + + return image diff --git a/Quote2Image/__init__.py b/Quote2Image/__init__.py new file mode 100644 index 0000000..d9543fc --- /dev/null +++ b/Quote2Image/__init__.py @@ -0,0 +1,3 @@ +from .Quote2Image import ImgObject +from .Quote2Image import GenerateColors +from .Quote2Image import Convert \ No newline at end of file diff --git a/README.md b/README.md index 231c09c..12d9efd 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,87 @@
A Python Library to Make Quote Images
-+
A python module to convert text quotes into graphical images
+-# How To Use? -- **Download The Latest Package From [Releases](https://github.com/SecretsX/Quote2Image/releases)** -- **Extract The Zip File And Place Every File In It To Your Current Code Folder** +## Installation +**To install Quote2Image, you can use `pip`:** +```bash +pip install Quote2Image +``` + +## Usage +**The convert function takes the following arguments:** + +- **`quote` : The quote to convert.** +- **`author` : The author of the quote.** +- **`fg` : The foreground color of the text.** +- **`bg` : The background color of the image.** +- **`font_type` : The font to use for the text.** +- **`font_size` : The font size to use for the text.** +- **`width` : The width of the image.** +- **`height` : The height of the image.** - +**Generating an image using RGB background and foreground, The package comes with a builtin `GenerateColors` function that generates a fg and bg color with the correct amount of luminosity and returns them in tuples.** -- **Code Instructions** ```python -from Quote2Image import convert +from Quote2Image import Convert, GenerateColors -# Font Size Default to 32, Height and Width by default is 612 -img=convert( +# Generate Fg and Bg Color +fg, bg = GenerateColors() + +img=Convert( quote="Pooing keeps you healthy", author="Pee", - fg="white", - image="background_img.jpg", - border_color="black", + fg=fg, + bg=bg, font_size=32, - font_file=None, + font_type="arial.ttf", width=1080, height=450) # Save The Image as a Png file -img.save("quote.png") +img.save("hello.png") ``` +**Generating an image using a custom background image. We can do that using the `ImgObject` that gives us alot of flexibility on how we want our background Image to be.** + +**The `ImgObject` class takes the following arguments:** + +- **`image` : The link to the background image (required).** +- **`brightness` : The brightness of the image (optional, default is 100).** +- **`blur` : The blur of the image (optional, default is 0).** + +**You can then use the `ImgObject` instance as the bg argument in the convert function:** + +```py +from Quote2Image import Convert, ImgObject + +bg=ImgObject(image="IMAGE FILE LOCATION", brightness=80, blur=80) + +img=Convert( + quote="Pooing keeps you healthy", + author="Pee", + fg=(21, 21, 21), + bg=bg, + font_size=32, + font_type="arial.ttf", + width=1080, + height=450) + +# Save The Image as a Png file +img.save("hello.png") +``` + +## Permissions + +- **You are allowed to use, modify, and distribute the module.** +- **You are allowed to distribute modified versions of the module, as long as you follow the terms of the license.** + +## Obligations + +- **You must include a copy of the GPL-3.0 license with the module.** +- **You must provide a copy of the source code of the module, either along with the modified version of the module or through a written offer to provide the source code.** +- **You must provide a prominent notice stating that you have modified the module, and the date of the modification.** +- **If you distribute the module, you must do so under the terms of the GPL-3.0 license.** + # That's It! -> Thank You! Hope this was useful to you <3 +> **Thank You! Hope this was useful to you <3** diff --git a/__pycache__/Quote2Image.cpython-37.opt-2.pyc b/__pycache__/Quote2Image.cpython-37.opt-2.pyc deleted file mode 100644 index cbce3b6..0000000 Binary files a/__pycache__/Quote2Image.cpython-37.opt-2.pyc and /dev/null differ diff --git a/build/lib/Quote2Image/Quote2Image.py b/build/lib/Quote2Image/Quote2Image.py new file mode 100644 index 0000000..4381ad9 --- /dev/null +++ b/build/lib/Quote2Image/Quote2Image.py @@ -0,0 +1,82 @@ +from PIL import Image, ImageDraw, ImageFont, ImageEnhance, ImageFilter +import random + + +class ImgObject: + def __init__(self, image, brightness=100, blur=0): + self.image = image + self.brightness = brightness + self.blur = blur + + def __repr__(self): + return f"ImgObject(image='{self.image}', brightness={self.brightness}, blur={self.blur})" + + +def GenerateColors(): + foreground_color = ( + random.randint(0, 255), + random.randint(0, 255), + random.randint(0, 255), + ) + + background_color = ( + random.randint(0, 255), + random.randint(0, 255), + random.randint(0, 255), + ) + + while abs(sum(foreground_color) - sum(background_color)) < (255 * 3) / 2: + background_color = ( + random.randint(0, 255), + random.randint(0, 255), + random.randint(0, 255), + ) + + return foreground_color, background_color + + +def Convert(quote, author, fg, bg, font_type, font_size, width, height): + if isinstance(bg, ImgObject): + image = Image.open(bg.image).resize((width, height)) + enhancer = ImageEnhance.Brightness(image) + image = enhancer.enhance(bg.brightness / 100) + if bg.blur != 0: + image = image.filter(ImageFilter.BoxBlur(bg.blur)) + else: + image = Image.new("RGB", (width, height), bg) + + draw = ImageDraw.Draw(image) + + font = ImageFont.truetype(font_type, font_size) + + lines = [] + line = "" + for word in quote.split(): + line_width = draw.textsize(line + " " + word, font)[0] + if line_width > width-40: + lines.append(line) + line = word + else: + line += " " + word + lines.append(line) + + quote_height = sum([draw.textsize(line, font)[1] for line in lines]) + y = (height - quote_height - font_size) // 2 + + for line in lines: + line_width = draw.textsize(line, font)[0] + x = (width - line_width) // 2 + draw.text((x, y), line, fg, font=font) + y += draw.textsize(line, font)[1] + + dash_width = draw.textsize(" - ", font)[0] + x = (width - dash_width) // 2 + y += font_size // 2 + draw.text((x, y), " - ", fg, font=font) + y += font_size // 2 + + author_width = draw.textsize(author, font)[0] + x = (width - author_width) // 2 + draw.text((x, y+15), author, fg, font=font) + + return image diff --git a/build/lib/Quote2Image/__init__.py b/build/lib/Quote2Image/__init__.py new file mode 100644 index 0000000..d9543fc --- /dev/null +++ b/build/lib/Quote2Image/__init__.py @@ -0,0 +1,3 @@ +from .Quote2Image import ImgObject +from .Quote2Image import GenerateColors +from .Quote2Image import Convert \ No newline at end of file diff --git a/dist/Quote2Image-0.0.4-py3-none-any.whl b/dist/Quote2Image-0.0.4-py3-none-any.whl new file mode 100644 index 0000000..0a50235 Binary files /dev/null and b/dist/Quote2Image-0.0.4-py3-none-any.whl differ diff --git a/dist/Quote2Image-0.0.4.tar.gz b/dist/Quote2Image-0.0.4.tar.gz new file mode 100644 index 0000000..6253835 Binary files /dev/null and b/dist/Quote2Image-0.0.4.tar.gz differ diff --git a/dist/Quote2Image-0.0.4.win-amd64.zip b/dist/Quote2Image-0.0.4.win-amd64.zip new file mode 100644 index 0000000..1115fe4 Binary files /dev/null and b/dist/Quote2Image-0.0.4.win-amd64.zip differ diff --git a/examples/background_img.jpg b/examples/background_img.jpg deleted file mode 100644 index 9077015..0000000 Binary files a/examples/background_img.jpg and /dev/null differ diff --git a/examples/quote to image.py b/examples/quote to image.py deleted file mode 100644 index ed49ea3..0000000 --- a/examples/quote to image.py +++ /dev/null @@ -1,16 +0,0 @@ -from Quote2Image import convert - -# Font Size Default to 32, Height and Width by default is 612 -img=convert( - quote="Pooing keeps you healthy", - author="Pee", - fg="white", - image="background_img.jpg", - border_color="black", - font_size=32, - font_file=None, - width=1080, - height=450) - -# Save The Image as a Png file -img.save("quote.png") diff --git a/fonts/Coves Bold.otf b/fonts/Coves Bold.otf deleted file mode 100644 index 1e064ec..0000000 Binary files a/fonts/Coves Bold.otf and /dev/null differ diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..224a779 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[metadata] +description-file = README.md \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..b6655d3 --- /dev/null +++ b/setup.py @@ -0,0 +1,40 @@ +from setuptools import setup, find_packages +import codecs +import os + +here = os.path.abspath(os.path.dirname(__file__)) + +with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh: + long_description = "\n" + fh.read() + +VERSION = "0.0.4" +DESCRIPTION = "A python module to convert text quotes into graphical images" +LONG_DESCRIPTION = DESCRIPTION + +setup( + name="Quote2Image", + version=VERSION, + author="NotCookey", + url="https://github.com/NotCookey/Quote2Image", + author_email="kanao.nishimiya@gmail.com", + description=DESCRIPTION, + long_description_content_type="text/markdown", + long_description=long_description, + install_requires=["Pillow==9.2.0"], + keywords=[ + "quotes", + "images", + "text", + "conversion", + "quote2image", + "quote to image", + "quote text to image", + ], + classifiers=[ + "Programming Language :: Python :: 3", + "Operating System :: Unix", + "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", + "Operating System :: MacOS :: MacOS X", + "Operating System :: Microsoft :: Windows", + ], +)