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)