Friday, December 28, 2018

Project Py-Meme

Project Py-Meme

I decided to have a crack at a brand new project, a meme creator with, of course, a Shambles GUI.

pymeme-screenshot

A meme is just a funny image, with some amusing text on it, like this example:

geek meme

There is nothing new about this of course, memes have been doing the rounds since the early 1990s, hey I’m catching up!

I got started writing the code with the slimmest of plans, do I ever learn from my past mistakes?  Yes and no, I know I will get in trouble as soon as the project gets complicated and as I realise what features need to be added I will regret not planning for it.  See virtually any of my previous projects on this blog for an example.

But, just going for it with great enthusiasm is just how I do things, it’s how my brain works, I cannot see past the initial concept, no matter how hard I try, and I have tried.

One thing I found when planning too much and trying to pre-empt problems and what future features to add actually scares me off as I have very low coding skill and confidence.

Enough crying already.  Project Py-Meme.

  • create GUI frame
  • Load and display  image
  • User inputs text on image
  • save image.

At the time of writing I have achieved the above four elements and it works.  I wouldn’t say I am happy with it, as it needs a lot of work to make it more useful, user friendly and flexible.

There are many problems with this project, most of which I can’t seem to get past as yet.

  • Some images won’t load or load and crash when try to put text on.
  • I can’t work out yet how to undo the users text, if required.
  • How do I know weather to use black or white text on the background, maybe give the user the option to change text colour?
  • I haven’t looked into fonts yet but font and font size should be offered I guess.  But this will open up a small can of worms on text width etc.
  • Baring in mind that the text is converted to an image before being placed on the background (not my doing, it’s the only way to get it centred) how can I allow for multiple lines of text if the user requires it.

At the moment I am at a quite low ebb after all the Xmas shenanigans that goes on.  I don’t feel particularly good, and the Pyinstaller false positive virus problem is just depressing me to the point that I started looking at other programming languages.

After reading the first chapter on a beginners guide to Ruby I quickly came back to Python knowing that this is the best I can ever hope for.  Don’t get me wrong, I love Python and appreciate all the hard work that goes on behind the scenes by volunteers and that it is free, I just get very frustrated that I don’t really understand how to use it properly yet.

Sob. If only Amos Basic was ported properly to the PC, I could of died a happy man.

Anyway, here is my crap Python code, it works sometimes.  It can act as a reminder on how not to code I guess.

python joke, changed something got a different error

Double click inside the code box to select all

'''
   PyMeMe Test Version
   By Steve Shambles
   Dec 2018
   https://stevepython.wordpress.com/
'''

from tkinter import Tk, Label, LabelFrame, Menu, messagebox, RIDGE,  \
simpledialog, filedialog
from PIL import Image, ImageTk, ImageFont, ImageDraw

# Create the main gui window
ROOT = Tk()
ROOT.title('PyMeMe -test version')
ROOT.geometry('830x630')
ROOT.resizable(False, False)

# Create frame for the image
FRAME0 = LabelFrame(ROOT, relief=RIDGE)
FRAME0.grid(padx=10, pady=10)

def user_load_image():
    global IMAGE
    '''offer file dialog for user to select an image'''
    FILE_SELECTED = filedialog.askopenfilename(title='Select Image')

    # resize and display image
    IMAGE = Image.open(FILE_SELECTED)
    IMAGE = IMAGE.resize((800, 600), Image.ANTIALIAS)
    user_photo = ImageTk.PhotoImage(IMAGE)
    LABEL = Label(FRAME0, image=user_photo)
    LABEL.IMAGE = user_photo
    LABEL.grid()

def add_top_text():
    '''Input dialog for user to enter text for top of image'''

    top_text = simpledialog.askstring(title='PyMeMe',  \
    prompt='Enter Top Text, Max 20 chars:')
    top_text = top_text.upper()

    draw = ImageDraw.Draw(IMAGE, "RGBA")

    impact = ImageFont.truetype("impact.ttf", 80)
    W, H = (800, 80)
    w, h = impact.getsize(top_text)
    draw.text(((W-w)/2, (H-h)), str(top_text), font=impact)

    IMAGE.save("foo.jpg")
    update_image()

def add_bottom_text():
    '''Input dialog for user to enter text for bottom of image'''
    bot_text = simpledialog.askstring(title='PyMeMe', prompt='Enter Bottom Text:')
    bot_text = bot_text.upper()

    draw = ImageDraw.Draw(IMAGE, "RGBA")

    impact = ImageFont.truetype("impact.ttf", 80)
    W, H = (800, 590)
    w, h = impact.getsize(bot_text)
    draw.text(((W-w)/2, (H-h)), str(bot_text), font=impact)

    IMAGE.save("foo.jpg")
    update_image()

def update_image():
    '''Updates image with text on it'''
    global IMAGE
    IMAGE = Image.open('foo.jpg')
    user_photo = ImageTk.PhotoImage(IMAGE)
    LABEL = Label(FRAME0, image=user_photo)
    LABEL.IMAGE = user_photo
    LABEL.grid(row=0, column=0)

# Drop-down menu.
MENU_BAR = Menu(ROOT)
FILE_MENU = Menu(MENU_BAR, tearoff=0)
MENU_BAR.add_cascade(label='Menu', menu=FILE_MENU)
FILE_MENU.add_command(label='Visit Blog', command="")
FILE_MENU.add_command(label='About', command="")
FILE_MENU.add_command(label='Exit', command="")

FILE_MENU2 = Menu(MENU_BAR, tearoff=0)
MENU_BAR.add_cascade(label='Text', menu=FILE_MENU2)
FILE_MENU2.add_command(label='Add Top Text. max 20 chars', command=add_top_text)
FILE_MENU2.add_command(label='Add Bottom Text, max 20 chars', command=add_bottom_text)

ROOT.config(menu=MENU_BAR)

user_load_image()

ROOT.mainloop()

Using Python V3.6.5(32 bit) on Windows 7, (64 bit).

Previous post:  Windows Shutdown Timer

 

Advertisements


from Python Coder
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...