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
还没有评论,来说两句吧...