标签:blog ima nbsp 字母 point https 使用 win 字体库
1.安装
pip install Pillow
2.使用
1)图片缩放
from PIL import Image
im = Image.open(‘dog.jpg‘) w,h = im.size #获取图像的尺寸 im.thumbnail((w/2,h/2)) #将图像缩放50% #im.show() #显示图片 im.save(‘dog_thumbnail.jpg‘) #保存图片
效果图:
2.图片模糊
im = Image.open(‘dog.jpg‘) im2 = im.filter(ImageFilter.BLUR) im2.save(‘dog_blur.jpg‘)
效果图:
3.生成验证码
from PIL import ImageDraw,ImageFont,ImageFilter import random #返回一个A-Z的随机字母 def randomChar(): return chr(random.randint(65,90)) #随机颜色 def randomColor1(): return (random.randint(64,255),random.randint(64,255),random.randint(64,255)) #随机颜色2 def randomColor2(): return (random.randint(32,127),random.randint(32,127),random.randint(32,127)) width = 60 * 4 height = 60 #创建白色图片 image = Image.new(‘RGB‘,(width,height),(255,255,255)) #创建font对象 #OSError: cannot open resource 需要指定字体库的系统路径 #font = ImageFont.truetype("Arial.ttf",36) font = ImageFont.truetype("C:/windows/fonts/Arial.ttf",36) #创建Draw对象,用于向白色背景图片上绘图 draw = ImageDraw.Draw(image) #填充每一个像素 for x in range(width): for y in range(height): draw.point((x,y),fill=randomColor1()) #输出文字 for i in range(4): draw.text((60*i+10,10),randomChar(),font=font,fill=randomColor2()) #模糊 image = image.filter(ImageFilter.BLUR) image.save("captcha.jpg")
效果:
4.裁图,旋转,粘贴
from PIL import Image im = Image.open(‘dog.jpg‘) box = (100,50,350,250) #要裁剪的矩形区域,坐标以左上角为原点 region = im.crop(box) #返回裁剪到的图片 #region.show() region = region.transpose(Image.ROTATE_180) #对图像进行旋转 im.paste(region,box) #将旋转后的图像粘回原图 im.show()
效果图:
5.添加图片水印:
from PIL import Image image = Image.open(‘dog.jpg‘) logo = Image.open(‘logo.png‘) logoW,logoH= logo.size imageW,imageH = image.size image.paste(logo,(imageW - logoW, imageH - logoH)) image.show()
效果:
6.添加透明文字水印:
参考: http://pythoncentral.io/watermark-images-python-2x/
def add_watermark(in_file, text,font,out_file=‘watermark.jpg‘, angle=23, opacity=0.25): ‘‘‘ :param in_file: 要添加水印的图片 :param text: 文字水印内容 :param out_file: 添加水印后的图片 :param font:水印字体 :param angle: 水印旋转角度 :param opacity: 水印透明度 ‘‘‘ img = Image.open(in_file).convert(‘RGB‘) watermark = Image.new(‘RGBA‘, img.size, (0, 0, 0, 0)) size = 2 n_font = ImageFont.truetype(font, size) #getsize返回水印文字对应字体大小的宽度和高度 n_width, n_height = n_font.getsize(text) #找到使得水印文字宽度最接近图片宽度的字体大小 while n_width + n_height < watermark.size[0]: size += 2 n_font = ImageFont.truetype(font, size) n_width, n_height = n_font.getsize(text) draw = ImageDraw.Draw(watermark, ‘RGBA‘) draw.text(((watermark.size[0] - n_width) / 2, (watermark.size[1] - n_height) / 2), text, font=n_font) watermark = watermark.rotate(angle, Image.BICUBIC) alpha = watermark.split()[3] #通过降低亮度和对比度来降低水印的透明度 alpha = ImageEnhance.Brightness(alpha).enhance(opacity) #设置透明度 watermark.putalpha(alpha) #添加水印 Image.composite(watermark, img, watermark).save(out_file, ‘JPEG‘) FONT = ‘C:/Windows/fonts/Arial.ttf‘ add_watermark(‘dog.jpg‘,‘python‘,font=FONT)
效果图:
相关资料:
https://pillow.readthedocs.io/en/latest/handbook/tutorial.html
http://pillow-cn.readthedocs.io/zh_CN/latest/handbook/tutorial.html
标签:blog ima nbsp 字母 point https 使用 win 字体库
原文地址:http://www.cnblogs.com/hupeng1234/p/6679545.html