zoukankan      html  css  js  c++  java
  • python 图片上添加文字

    搬运链接https://www.cnblogs.com/tk091/p/4331327.html

    import PIL
    from PIL import ImageFont
    from PIL import Image
    from PIL import ImageDraw
    
    #设置字体,如果没有,也可以不设置
    #font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",13)
    font = ImageFont.truetype("C:\Windows\Fonts\simsun.ttc",60)    #现在是宋体
    
    #打开底版图片
    imageFile = "e:\1.jpg"
    im1=Image.open(imageFile)
    
    # 在图片上添加文字 1
    draw = ImageDraw.Draw(im1)
    #draw.text((0, 0),"1",(255,255,0),font=font)
    #draw.text((500, 50),"1",(255,255,0))
    draw.text((500, 50),"xie",(0,0,0),font=font)
    draw = ImageDraw.Draw(im1)
    
    # 保存
    im1.save("e:\target.png")
    print('运行结束')

     2.改动版本Xdd

    import PIL
    from PIL import ImageFont
    from PIL import Image
    from PIL import ImageDraw
    
    #设置字体,如果没有,也可以不设置
    #font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",13)
    font = ImageFont.truetype("C:\Windows\Fonts\simsun.ttc",60)    #现在是宋体
    
    #打开底版图片,确定书写位置
    imageFile = "e:\1.jpg"
    img=Image.open(imageFile)
    
    #print(width,height)
    #print(type(width))
    
    
    # 在图片上添加文字 
    str1 = 'xdd'
    str2 = ''
    width=img.width
    height=img.height
    position1 = (width/2,height/10)
    position2 = (width/4,height/5)
    color = (255,255,0)
    draw = ImageDraw.Draw(img)
    draw.text(position1,str1,color,font=font)
    draw.text(position2,str2,color,font=font)
    #draw = ImageDraw.Draw(img)
    
    # 保存图片
    img.save("e:\1_xdd.jpg")
    print('运行结束')

    转载仅为学习,不会商用。
    欢迎转载原创,附文链接。
  • 相关阅读:
    linux 文件系统基本结构
    linux bash命令行基本操作
    U盘安装Centos6.2
    linux安装JDK
    linux重启和关闭系统命令
    eclipse安装反编译工具JadClipse
    Linux系统 Centos6 安装
    Linux 发展史
    计算机硬件
    网络 、osi 七层模型、tcp/ip 五层参考
  • 原文地址:https://www.cnblogs.com/xdd1997/p/11982938.html
Copyright © 2011-2022 走看看