Last week, my mom suddenly asked me, “Son, do you remember tomorrow is your aunt’s birthday?”
I slapped my forehead and completely forgot about it! My aunt lives in another city, and we don’t keep in touch often, but every year on her birthday, she sends me red envelopes. I can’t afford to be rude this time.
But, where do I have time to pick out a greeting card? Suddenly, an idea struck me: Python! That’s right, let’s use Python to create a unique electronic greeting card!
Pillow Library: Your Image Processing Helper
We need to install the Pillow library. It is the Swiss Army knife for image processing in Python, powerful enough to leave you in awe.
pip install Pillow
Once installed, let’s start our greeting card creation journey!
Creating the Canvas: Starting with a Blank Sheet
Imagine we have a blank white canvas in front of us; now is the time to unleash our creativity:
from PIL import Image, ImageDraw, ImageFont
# Create an image with a white background
width, height = 800, 600
img = Image.new('RGB', (width, height), color='white')
Here, we created a white canvas of 800×600 pixels. It feels just like when you got a blank sheet of paper as a child, ready to draw, doesn’t it?
Adding Text: Writing Blessings with Code
Next, we want to write our blessings on the greeting card. Imagine picking up a colorful pen and writing “Happy Birthday” on the paper:
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("path/to/your/font.ttf", 36)
text = "Dear Aunt, Happy Birthday!"
text_width = draw.textlength(text, font=font)
x = (width - text_width) / 2
y = height / 2
draw.text((x, y), text, font=font, fill='black')
This code is like carefully choosing the color and size of your pen and writing the blessings right in the center of the card.
Friendly reminder: Don’t forget to replace “path/to/your/font.ttf” with the actual font file path on your computer! Can’t find a suitable font? The system fonts can work too!
Adding Decorations: Making the Card More Festive
A card with only text is a bit dull. Let’s draw a few simple shapes to add some festive cheer:
# Draw a few colorful balloons
colors = ['red', 'blue', 'green', 'yellow']
for i in range(5):
x = width * i / 5 + 50
y = height - 100
draw.ellipse([x, y, x+60, y+60], fill=colors[i % len(colors)])
draw.line([x+30, y+60, x+30, y+120], fill=colors[i % len(colors)], width=2)
# Draw some confetti
import random
for _ in range(100):
x = random.randint(0, width)
y = random.randint(0, height)
size = random.randint(2, 5)
color = random.choice(colors)
draw.rectangle([x, y, x+size, y+size], fill=color)
Look, our greeting card has suddenly come to life! The colorful balloons and confetti seem to celebrate my aunt’s birthday.
Saving the Result: Sending Out Love
Let’s save our masterpiece:
img.save('birthday_card.png')
Look, a heartfelt birthday greeting card has been created! You can send it to someone you want to bless, or print it out as a physical card.
This little program can be used for much more than this. You can use it to create various holiday cards, invitations, or even funny memes. Just unleash your imagination, and Python + Pillow can help you achieve it.
The next time someone asks if you can edit images, you can proudly say: I not only can edit images, but I can also code to edit images!
Remember, the charm of coding lies not only in its functionality but also in how it allows us to solve little problems in life with creativity. Maybe one day, you’ll be glad you learned this little skill; on some special day, it will become a unique way for you to express your feelings.