A tutorial that uses the PIL library to add serial numbers to a picture in Python

Source: Internet
Author: User
My girlfriend let me give her the picture of the paper with the letter serial number, originally thought is a very simple thing, but that white black word circle serial number But baffled me, tried a few commonly used software, can not.

Later with PS + action, it can be made out, but it is not easy, just did not finish the day, so take home to do, but my computer does not have PS, so it is implemented with Python.


The pictures here are all 240X240, according to the first letter of the file name as the serial number, PIL although the size of the text can be calculated, but similar to the D character still cannot be in the center of the circle, so also to the individual character offset settings, would like to use Aggdraw to draw circles, can smooth some, But installed several times, all failed, eventually give up.

#!/usr/bin/env python#-*-coding:utf-8-*-import os, sys, Fnmatchimport Image, Imagedraw, Imagefontdef process_picture (f Ilename): seq = os.path.split (filename) [ -1][0].upper () img = Image.open (os.path.join (input_dir, filename)) draw = Image Draw.draw (IMG) # Draw white bottom black box circle Draw.ellipse ((215, 215, 235, 235) in lower right corner (outline= ' black ', fill= ' Bai ') # write alpha number into circle Font = Im Agefont.truetype (' Fonts/times New Roman.ttf ', 20) # Calculates the center position of the text 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.ge T (seq, 0) Draw.text ((215 + x + offset, 215 + y), seq, Font=font, fill= ' Black ') # Save Image Img.save (Os.path.join (OUTPU  T_dir, filename), ' JPEG ') if __name__ = = ' __main__ ': If Len (SYS.ARGV) < 3:print ' Usage:python drawseq.py
 
  
   
    ' Sys.exit (1) input_dir, Output_dir = Sys.argv[1:3] Os.path.exists (output_dir) or os.makedirs (output_dir) fo R filename in Os.listdir (input_dir): If Fnmatch.fnmatch (Filename.lower (), ' *.jpg '): process_picture (filename) 
    
 

  
 
  • 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.