Pillow概况

  • PIL是Python的一种图像处理工具。
  • PIL支持大部分的图像格式,高效并强大。
  • 核心库设计用来高速访问基于基于像素的数据存储,给这个通用的图像处理工具提供了坚实的基础。

一.读、写、显示、屏幕截图

from PIL import Image

img1 = Image.open('wali.jpg')  # 读
img1.show()                     # 显示
img1.save('wali_test1.png')    # 写  : 写入时,可以更换格式

w, h = img1.size                # 利用内置方法获取图像的宽度高的
print(w, h, img1.size)          # 1920 1080 (1920, 1080)
  • 显示 :是调用系统自带的图片查看器,以临时文件的方式查看

  • :写的时候可以更改图像格式

  • 屏幕截图

from PIL import ImageGrab
ImageGrab.grab().save(r'D:\wali\grab.jpg')
#     截图             保存图像

1. 读取图像

from PIL import Image
# 从文件读
img1 = Image.open('wali.jpg')  # 读
# 从array数组读
img2 = Image.fromarray(image)
# 从内存读

二.尺寸变换、旋转

1.尺寸变换 - 缩小- resize

from PIL import Image
img1 = Image.open('wali.jpg')       # 读
print(img1.size)                       #打印图片原始的尺寸
img1 = img1.resize((80, 60))          # 缩小尺寸
print(img1.size)                    #打印打印缩小后的图片尺寸
img1.save('wali_test2.png')  # 保存图片为'wali_test2.png'

#(1920, 1080)
#(80, 60)

2.尺寸变换 - 放大- resize

from PIL import Image

img1 = Image.open('wali_test2.png') #使用80*60 的瓦力作为放大的原图像
print(img1.size)
img1 = img1.resize((400, 300))
print(img1.size)
img1.save('wali_test3.png')

#(80, 60)
#(400, 300)

3.1尺寸变换 - 旋转90度- transpose

from PIL import Image
img1 = Image.open('800X600.png')
for x in range(7):
    img1.transpose(x).save('transpose%d.png'% x)
img1.close()    #关闭图像

# # transpose
# FLIP_LEFT_RIGHT = 0
# FLIP_TOP_BOTTOM = 1
# ROTATE_90 = 2
# ROTATE_180 = 3
# ROTATE_270 = 4
# TRANSPOSE = 5
# TRANSVERSE = 6

3.2尺寸变换 - 旋转(任意角度)- rotate

from PIL import Image
img1 = Image.open(r'D:\wali\400X300.jpg')
for x in range(30,361,30):
    img1.rotate(x).save(r'D:\wali\rotate%d.png'% x )

三.绘制图形

from PIL import Image, ImageDraw,ImageFont

Image1 = Image.new('RGB', (500, 400), (255, 255, 255))
draw =ImageDraw.Draw(Image1)

# 画直线
draw.line((20, 20, 150, 150), 'cyan', width=4)
draw.line((20, 150, 150, 20), 'red', width=2)

# 画矩形
draw.rectangle((170, 20, 300, 150), 'black', 'red', width=2)

# 画圆弧
draw.arc((340, 20, 470, 150), 0, 270, 'yellow', width=3)

# 画椭圆
draw.ellipse((20, 170, 150, 235), 'yellowgreen', 'wheat')

Image1.show()

四.绘制文字

1.英文

from PIL import Image, ImageDraw,ImageFont

Image1 = Image.new('RGB', (500, 400), (255, 255, 255))
draw =ImageDraw.Draw(Image1)

# 画文字_英文
draw.text((20, 250), u'Hello World', 'fuchsia')
draw.text((120, 250), u'Hello World', 'red')

# 画文字_中文
path_to_ttf = r'font/simfang.ttf'
font = ImageFont.truetype(path_to_ttf, size=25) # 设置字体
draw.text(xy=(20,280),text='Hello,中国!',fill='red',font=font)

Image1.show()

2.中文

from PIL import Image, ImageDraw,ImageFont

Image1 = Image.new('RGB', (500, 400), (255, 255, 255))
draw =ImageDraw.Draw(Image1)

# 画文字_中文
path_to_ttf = r'font/simfang.ttf'
font = ImageFont.truetype(path_to_ttf, size=25) # 设置字体
draw.text(xy=(20,280),text='Hello,中国!',fill='red',font=font)

Image1.show()

五.区域截图(抠图,覆盖)

from PIL import Image, ImageDraw

im = Image.open("t3.jpeg")

#   设置抠图区域
box = (350, 40, 507, 90)
#   从图片上抠下此区域
region = im.crop(box)
#   将此区域旋转180度
region = region.transpose(Image.ROTATE_180)
#   查看抠出来的区域
region.show()
#   将此区域粘回去
im.paste(region, box)
im.show()

六.滤镜

from PIL import Image
from PIL import ImageFilter
img1 = Image.open(r'D:\wali\z400X300.jpg')

img1.filter(ImageFilter.BLUR).save(r'D:\wali\1BLUR.png')
img1.filter(ImageFilter.CONTOUR).save(r'D:\wali\2CONTOUR.png')
img1.filter(ImageFilter.DETAIL).save(r'D:\wali\3DETAIL.png')
img1.filter(ImageFilter.EDGE_ENHANCE).save(r'D:\wali\4EDGE_ENHANCE.png')
img1.filter(ImageFilter.EDGE_ENHANCE_MORE).save(r'D:\wali\5EDGE_ENHANCE_MORE.png')
img1.filter(ImageFilter.EMBOSS).save(r'D:\wali\6EMBOSS.png')
img1.filter(ImageFilter.FIND_EDGES).save(r'D:\wali\7FIND_EDGES.png')
img1.filter(ImageFilter.SMOOTH).save(r'D:\wali\8SMOOTH.png')
img1.filter(ImageFilter.SMOOTH_MORE).save(r'D:\wali\9SMOOTH_MORE.png')
img1.filter(ImageFilter.SHARPEN).save(r'D:\wali\10SHARPEN.png')

七.透明叠加

from PIL import Image, ImageDraw,ImageFont

# 准备图像
img1 = Image.new('RGB', (500, 400), (255,255,255))
draw =ImageDraw.Draw(img1)
draw.line((20, 20, 150, 150), 'cyan', width=4)
draw.line((20, 150, 150, 20), 'red', width=2)
draw.rectangle((170, 20, 300, 150), 'black', 'red', width=2)
draw.arc((340, 20, 470, 150), 0, 270, 'yellow', width=3)
draw.ellipse((20, 170, 150, 235), 'yellowgreen', 'wheat')
draw.text((20, 250), u'Hello World', 'fuchsia')
draw.text((120, 250), u'Hello World', 'red')
path_to_ttf = r'font/simfang.ttf'
font = ImageFont.truetype(path_to_ttf, size=25) # 设置字体
draw.text(xy=(20,280),text='Hello,中国!',fill='red',font=font)

# a通道图像叠加
draw2 = ImageDraw.Draw(img1, 'RGBA')
draw2.rectangle((100, 100, 300, 300), fill=(0, 255, 0, 128))

img1.show()
02-10 19:06