100+ Python Projects Challenge

Jeffrey Hu
4 min readMay 7, 2021

--

Someone told me that Python is very easy to learn but it’s difficult to use it to do something. So I think if we can learn Python programming by do some real projects it’ll helpful. That’s the reason I made the github repo https://github.com/zhiwehu/Python-programming-exercises several years ago and now I’m making a new one: https://github.com/zhiwehu/100_plus_Python_Projects_Challenge

Today I just pushed the challenge 3: add text watermark on pictures.

Add text watermark on pictures

Requirements

  1. Run the code in console using command line.
  2. It’ll ask you what’s the text you want to add as watermark, and the text size, transparency and position. Please use 20, 50% and bottom right as the default values.
  3. It’ll add the text watermark on every png pictures in the ‘images’ dirtionary of the current path, and save these pictures into ‘watermark’ dirtionary.

What will we practice in this project?

  • for loop
  • input text
  • if conditions
  • functions
  • tuple
  • Open/save files
  • Image RGBA conception
  • os package
  • PIL package (need to install by pip install pillow)

A reference code

import osfrom PIL import Image, ImageDraw, ImageFont
def get_position(image_width, image_height, text_width, text_height, position_id=9, margin=10):
'''
Get the position of the text by the position_id
1: top left, 2: top center, 3: top right
4: middle left, 5: middle center, 6: middle right
7: bottom left, 8: bottom center, 9: bottom right
:param image_width: image width
:param image_height: image height
:param text_width: text width
:param text_height: text height
:param position_id: position_id
:param margin: the text position margin value to the image
:return: text position tuple
'''
margin = 10
if position_id == 1:
return (margin, margin)
elif position_id == 2:
return (image_width // 2 - text_width // 2, margin)
elif position_id == 3:
return (image_width - text_width - margin, margin)
elif position_id == 4:
return (margin, image_height // 2 - text_height // 2)
elif position_id == 5:
return (image_width // 2 - text_width // 2, image_height // 2 - text_height // 2)
elif position_id == 6:
return (image_width - text_width - margin, image_height // 2 - text_height // 2)
elif position_id == 7:
return (margin, image_height - text_height - margin)
elif position_id == 8:
return (image_width // 2 - text_width // 2, image_height - text_height - margin)
elif position_id == 9:
return (image_width - text_width - margin, image_height - text_height - margin)
def add_watermark(filename, text, font_name='Roboto-Italic.ttf', font_size=20, font_opacity=50, position_id=9):
'''
Add watermark function
:param filename: origin image filename
:param text: watermark text
:param font_name: Roboto-Italic.ttf, you can use your font, please make sure your program can find it
:param font_size: font size, default is 20
:param font_opacity: font opacity, default is 50
:param position_id: position id, defalut is 9 (bottom right)
:return:
'''
# get an image
with Image.open(filename).convert("RGBA") as base:
# make a blank image for the text, initialized to transparent text color
txt = Image.new("RGBA", base.size, (255, 255, 255, 0))
# get a font
fnt = ImageFont.truetype(font_name, font_size)
# get a drawing context
d = ImageDraw.Draw(txt)
# get the text widht and height
text_width, text_height = d.textsize(text, font=fnt)
# get the text position of the image
pos = get_position(base.size[0], base.size[1], text_width, text_height, position_id=position_id)
# draw text with opacity
d.text(pos, text, font=fnt, fill=(255, 255, 255, 256 * font_opacity // 100))
out = Image.alpha_composite(base, txt)
# save the image file
out_filename = 'watermark/{}'.format(os.path.basename(filename))
if not os.path.exists('watermark'):
os.makedirs('watermark')
out.save(out_filename, 'PNG')
if __name__ == '__main__':
text = input('Please input a watermark text: ').strip()
font_size = int(input('Please input the font size: [20]') or '20')
font_opacity = int(input('Please input the font opacity: [50]') or '50')
# 1: top left, 2: top center, 3: top right
# 4: middle left, 5: middle center, 6: middle right
# 7: bottom left, 8: bottom center, 9: bottom right
position_id = int(input('Please input the position: [9]') or '9')
for f in os.listdir('images'):
if f.endswith('.png'):
filename = 'images/{}'.format(f)
print('add watermark for {}'.format(filename))
add_watermark(filename=filename, text=text, font_size=font_size, font_opacity=font_opacity,
position_id=position_id)

Run the demo

  • make sure you have a image dirtionary and put some png files in this dirtionary
  • use pip install requirements.txt to install packages
  • run it in console
python 3.py
  • it’ll add watermark on every picture in images dirtionary and save them to watermark dirtionary.

Please feel free to let me know if you have any ideas so we can put your ideas into a real Python project.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response