Python每天一题 0000

开学了,也继续写博客了,暑假期间也写了很多笔记。有空再复习下写在博客上。
很喜欢这个每天一题的~~~
如有错误,请大牛指出,谢谢。
第一天是对图片的操作,那么就安装PIL
easy_install Pillow
检测的话可以python交互下写import PIL

#-*- coding: utf-8 -*-
from PIL import Image
im = Image.open("c:/1.png")
print im.show()

使用Image模块,I是大写的,要写from PIL 不能直接写import

使用dir查看im有的属性和方法。

先试了size
im.size
返回一个元组
sizeout = im.size
返回一个元组
也可以
x,y = im.size

然后是使用两个模块

ImageFont
ImageDraw

设置字体和在图片上进行画

ImageFont 用于ImageDraw类的text()
ImageFont的truetype

ImageFont.truetype(file,size, encoding=value)

使用Arial字体
arial.ttf

font = ImageFont.truetype(“arial.ttf”,3)

创建完font在创建一个draw
draw = ImageDraw.Draw(im)
然后使用draw的text方法在图片上写字

PIL.ImageDraw.Draw.text(xy, text, fill=None, font=None, anchor=None)

参数:
xy – Top left corner of the text.
text – Text to be drawn.
font – An ImageFont instance.
fill – Color to use for the text.
也就是说第一个参数是位置以左上角为初始位置,第二个参数是写的文本内容,第三个参数是颜色。如(255,0,0)就是红色。然后是字体
代码

#-*- coding: utf-8 -*-
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
im = Image.open("c:/1.png")
#print im.show()
#print dir(im)
x,y = im.size
print x,y
font = ImageFont.truetype("arial.ttf",int(y/5))
draw = ImageDraw.Draw(im)
draw.text((0.9*x,0.1*y),"3",(255,0,0),font)
print im.show()

参考:

http://www.2cto.com/kf/201603/491448.html
http://pillow-cn.readthedocs.io/zh_CN/latest/
http://blog.csdn.net/scdn892147045/article/details/50996629
https://github.com/Ciyfly/show-me-the-code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值