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('运行结束')

    转载仅为学习,不会商用。
    欢迎转载原创,附文链接。
  • 相关阅读:
    DB2基本语句操作
    makefile简析与例子
    io多路复用epoll归纳
    Selenium 3----设置元素等待
    Selenium 3----获取断言信息
    Selenium 3----鼠标、键盘操作常用方法
    Selenium 3----WebDriver常用方法
    算法时间复杂度求解
    Selenium 3----控制浏览器操作
    操作系统基础知识学习
  • 原文地址:https://www.cnblogs.com/xdd1997/p/11982938.html
Copyright © 2011-2022 走看看