New meme generator
A new meme generator! Let's create a simple one using Python and the Pillow library.
Meme Generator Code
import os
from PIL import Image, ImageDraw, ImageFont
class MemeGenerator:
def __init__(self):
self.font = ImageFont.truetype("arial.ttf", 24)
self.image_width = 500
self.image_height = 500
def generate_meme(self, top_text, bottom_text, image_path):
# Open the image
img = Image.open(image_path)
img = img.resize((self.image_width, self.image_height))
# Create a drawing context
draw = ImageDraw.Draw(img)
# Add the top text
draw.text((10, 10), top_text, font=self.font, fill=(255, 255, 255))
# Add the bottom text
draw.text((10, self.image_height - 30), bottom_text, font=self.font, fill=(255, 255, 255))
# Save the meme
meme_path = f"meme_{os.urandom(6).hex()}.png"
img.save(meme_path)
return meme_path
# Example usage
generator = MemeGenerator()
meme_path = generator.generate_meme("This is the top text", "And this is the bottom text", "path/to/image.jpg")
print(meme_path)
How to use
- Install the Pillow library by running
pip install pillow
in your terminal. - Create a new Python file and copy the code above into it.
- Replace
"path/to/image.jpg"
with the path to the image you want to use as the base for your meme. - Run the script by executing the Python file.
- The script will generate a new meme image and save it to a file with a random name (e.g.,
meme_abcdefg.png
). - You can then share the meme image with others!
Customization
You can customize the meme generator by modifying the following variables:
font
: Change the font used for the text by specifying a different font file path.image_width
andimage_height
: Adjust the size of the output image.top_text
andbottom_text
: Change the text displayed on the meme.image_path
: Specify a different image path to use as the base for your meme.
I hope you enjoy using this meme generator!