zoukankan      html  css  js  c++  java
  • python 图片上添加数字源代码

    最近因工作需要,需要在图片上添加数字,查询了资料,自己写了一个方法,并进行了测试,由于代码用到了PIL库,需要下载安装,下载地址:http://www.pythonware.com/products/pil/,下载Imaging-1.1.7.tar.gz后解压得到,Imaging-1.1.7,在命令行下运行setup.py进行安装

    具体实现代码如下:

    # -*- coding: utf-8 -*-
    import PIL
    from PIL import ImageFont
    from PIL import Image
    from PIL import ImageDraw

    '''在图片上添加数字,imageFile为要添加数字的图片文件,fontFile为字体文件,
    targetImageFile为添加数字后保存的图片文件,txtnum为添加的数字'''
    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: est.png"
    fontFile = r"D:arialuni.ttf"
    targetImageFile = r"D: arget.jpg"
    txtnum= '34'
    DrawImageTxt(imageFile,fontFile,targetImageFile,txtnum)

  • 相关阅读:
    002Python和JS的不同进制之间的转换实现
    001JS中的非严格相等 ==
    028_同步本地git项目到github和码云
    015你所常见的日常英语
    001CSS三大特性
    014国家地区语言缩写码
    013常用的英语词典Share
    012_犯人的夏日的蚊虫叮咬词汇
    011_中文"上火"的英文怎么说
    我的IT之路这样走过
  • 原文地址:https://www.cnblogs.com/shaosks/p/5611577.html
Copyright © 2011-2022 走看看