Tutorial on batch adding serial numbers to images using the PIL library in Python

Source: Internet
Author: User
Tags image processing library

Tutorial on batch adding serial numbers to images using the PIL library in Python

This article describes how to add serial numbers to images in batches using the PIL library in Python. The PIL library is a very powerful image processing library in Python. For more information, see

My girlfriend asked me to add a letter serial number to the picture of her paper. I thought it was a very simple thing, but the circle serial number with white background and black characters was hard for me and I tried some common software, none.

After that, I was able to use PS + actions, but it was not easy. I did not finish the work that day, so I took it back to my house, but I didn't have PS on my computer, so it is implemented in python.

  

The images used here are all 240X240 characters. PIL can be used as the serial number based on the first letter of the file name. Although PIL can be used to calculate the size of the text, characters such as D cannot be in the center of the circle, therefore, offset settings are also made for individual characters. Originally, we wanted to use aggdraw to draw a circle to make it smoother. However, after installing it several times, they all ended up failing and finally gave up.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

#! /Usr/bin/env python

#-*-Coding: UTF-8 -*-

Import OS, sys, fnmatch

Import Image, ImageDraw, ImageFont

 

Def process_picture (filename ):

Seq = OS. path. split (filename) [-1] [0]. upper ()

Img = Image. open (OS. path. join (input_dir, filename ))

 

Draw = ImageDraw. Draw (img)

 

# Draw a black circle in the bottom right corner

Draw. ellipse (215,215,235,235), outline = 'black', fill = 'white ')

 

# Write the letter number into the circle

Font = ImageFont. truetype ('fonts/Times New Roman. ttf', 20)

 

# Calculate the text Center Position

Text_size = draw. textsize (seq, font)

X = (20/2)-(text_size [0]/2)

Y = (20/2)-(text_size [1]/2)

 

# Letter offset

Offsets = {'A': 1, 'B': 1, 'E': 1, 'D': 2}

Offset = offsets. get (seq, 0)

Draw. text (215 + x + offset, 215 + y), seq, font = font, fill = 'black ')

 

# Save image

Img. save (OS. path. join (output_dir, filename), 'jpeg ')

 

If _ name _ = '_ main __':

If len (sys. argv) <3:

Print 'usage: python drawseq. py <input_dir> <output_dir>'

Sys. exit (1)

 

Input_dir, output_dir = sys. argv [1: 3]

OS. path. exists (output_dir) or OS. makedirs (output_dir)

 

For filename in OS. listdir (input_dir ):

If fnmatch. fnmatch (filename. lower (), '*. jpg '):

Process_picture (filename)

Note: For more exciting tutorials, please pay attention to the graphic tutorial channel of the customer's house,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.