Add a digital source code to a Python image

Source: Internet
Author: User
Tags add numbers

Recently due to work needs, need to add numbers on the image, query the data, wrote a method, and tested, because the code used in the PIL library, need to download the installation,: http://www.pythonware.com/products/pil/, Download imaging-1.1.7.tar.gz after extracting, Imaging-1.1.7, run setup.py under command line to install

The specific implementation code is as follows:

#-*-Coding:utf-8-*-
Import PIL
From PIL import Imagefont
From PIL import Image
From PIL import Imagedraw

"' Add a number to the picture, imagefile the picture file to add the number, fontfile the font file,
Targetimagefile a picture file to be saved after adding a number, txtnum the added number "
def drawimagetxt (Imagefile,fontfile,targetimagefile,txtnum):
#设置字体大小
Font = Imagefont.truetype (Fontfile, 46)
#打开文件
im = Image.open (imagefile)

#字体坐标
x = IM.SIZE[0]/2
y = IM.SIZE[1]/2
p = (x, y)

Draw = Imagedraw.draw (IM)
Draw.text (P, Txtnum, (255,255,255), Font=font)

#保存
Im.save (Targetimagefile)
#关闭
Im.close ()

#测试代码
ImageFile = r "D:\test.png"
Fontfile = r "D:\arialuni.ttf"
Targetimagefile = r "D:\target.jpg"
Txtnum= ' 34 '
Drawimagetxt (Imagefile,fontfile,targetimagefile,txtnum)

Add a digital source code to a Python image

Related Article

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.