Last active
May 6, 2020 13:55
-
-
Save be1993/ffc66835c0e2ec4abb25f06f5d25b20a to your computer and use it in GitHub Desktop.
Python script to parse a text file and create a nice image for social media post. My second version it now accepts font sizes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""This script prompts for text file and attaches it's contents to an image | |
Writen By Atasa Rossios""" | |
import sys | |
import traceback | |
from pathlib import Path, PurePath | |
import textwrap | |
from PIL import Image, ImageDraw, ImageFont | |
# | |
BACKGROUND = "/some/path/background.png" | |
FONT_FILE = '/some/path//IBM Plex Serif_regular.ttf' | |
FONT_SIZE = 22 | |
#textfile = sys.argv[1] | |
print("\n Welcome to text2img python by Atasa Rossios! \n") | |
def main(): | |
try: | |
# Ask for the text file | |
textfile = input("PROVIDE TEXT FILE: >> ") | |
# Check if file exists | |
if Path(textfile).exists() is False: | |
sys.exit( | |
"\n ... Please make sure the file [" + textfile + "] exists! ") | |
# Save the image with the same filename | |
OUTFILE = str(PurePath(textfile).with_suffix('.png')) | |
# Ask fo font size just in case default is 22 | |
fnt_size = input("ENTER FONT SIZE (22) : ") or FONT_SIZE | |
try: | |
fnt_size = int(fnt_size) | |
except ValueError: | |
print("\n no font size given using default 22 \n") | |
fnt_size = FONT_SIZE | |
# MAKE SURE FONT EXISTS | |
if Path(FONT_FILE).exists is False: | |
sys.exit("\n ... Couln't find the font file...") | |
# MAKE SURE BACKGOUND IMAGE EXISTS | |
if Path(BACKGROUND).exists() is False: | |
sys.exit("\n ... background image is missing!") | |
# Read the File | |
File = open(textfile) | |
QUOTE = File.read() | |
File.close() | |
# Wrap this text. | |
text = textwrap.dedent(QUOTE) | |
# create Image object with the input image | |
image = Image.open(BACKGROUND) | |
# initialise the drawing context with the image object as background | |
draw = ImageDraw.Draw(image) | |
# create font object with the font file and specify desired size | |
font = ImageFont.truetype(FONT_FILE, size=fnt_size) | |
# starting position of the message | |
(x, y) = (15, 20) | |
COLOR = 'rgb(255, 255, 255)' # black color | |
# draw the message on the background | |
draw.text((x, y), text, fill=COLOR, font=font) | |
#(x, y) = (30, 495) | |
#name = 'Atasa' | |
# color = 'rgb(220, 220, 220)' # white color | |
#draw.text((x, y), name, fill=color, font=font) | |
# save the edited image | |
image.save(OUTFILE, compress_level=5) | |
print("YOUR IMAGE IS SAVED AS "+OUTFILE) | |
except KeyboardInterrupt: | |
print("\n .....Shutdown requested...exiting! \n") | |
except Exception: | |
traceback.print_exc(file=sys.stdout) | |
sys.exit(0) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment