🖼 人工智能学习总目录

🖼 《Python王者归来》小结及目录

17、Python处理图像文件

分辨率(resolution)、矩阵(matrix)

盒子元组(box tuple) left左上到右上 top左上到左下 right 左下到右下 bottom 右下到右上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from PIL import ImageColor
# R(red)、G(green)、B(blue)、A(alpha)

print(ImageColor.getrgb("#0000ff"))
print(ImageColor.getrgb("rgb(0, 0, 255)"))
print(ImageColor.getrgb("rgb(0%, 0%, 100%)"))
print(ImageColor.getrgb("Blue"))
print(ImageColor.getrgb("blue"))

print(ImageColor.getcolor("#0000ff", "RGB"))
print(ImageColor.getcolor("rgb(0, 0, 255)", "RGB"))
print(ImageColor.getcolor("Blue", "RGB"))
print(ImageColor.getcolor("#0000ff", "RGBA"))
print(ImageColor.getcolor("rgb(0, 0, 255)", "RGBA"))
print(ImageColor.getcolor("Blue", "RGBA"))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from PIL import Image,ImageDraw,ImageFont
a = Image.open('xxx.png') # 建立Pillow对象
print("列出对象型态 : ", type(a))
width, height = a.size # 获得影像宽度和高度
print("宽度 = ", width)
print("高度 = ", height)
print("列出物件文件名 : ", a.filename)
print("列出物件扩展名 : ", a.format)
print("列出对象描述 : ", a.format_description)

pictObj = Image.new("RGB", (300, 180), "aqua")
pictObj.save("xxxx.jpg")

a.show()
# 改大小,三次方取样
b = a.resize((400, 400),Image.BISCUBIC)
# 旋转45度,图像扩充,后保存
a.rotate(45, expand=True).save("out17_10_3.jpg")
# 翻转Image.FLIP_TOP_BOTTOM、Image.FLIP_LEFT_RIGHT
a.transpose(Image.FLIP_TOP_BOTTOM).save("out2.jpg")
# 像素点色彩
print(newImage.getpixel((150, 50)))
# 某位置填入色彩
newImage = Image.new('RGBA', (300, 300), "Yellow")
for x in range(50, 251): # x轴区间在50-250
for y in range(50, 151): # y轴区间在50-150
newImage.putpixel((x, y), (0, 255, 255, 255)) # 填青色
newImage.save("out17_14_1.png") # 第一阶段存档
for x in range(50, 251): # x轴区间在50-250
for y in range(151, 251): # y轴区间在151-250
newImage.putpixel((x, y), ImageColor.getcolor("Blue", "RGBA"))
newImage.save("out3.png")
# 复制图像
copyPict = a.copy()
# 剪切图像
cropPict = a.crop((80, 30, 150, 100))
# 图像合成
a.paste(b,(20,20))
# 图像滤镜 BLUR模糊、CONTOUR轮廓、EMBOSS浮雕效果、FIND_EDGES边缘信息
filterPict = a.filter(ImageFilter.BLUR)

点drawObj.point([(x,y),…],fill=’GREEN’)

线drawObj.line([(x,y),…],width,fill=’RED’)

矩形drawObj.rectangle((l,t,r,d),fill=’red’,outline=’white’)

椭圆drawObj.ellipses((l,t,r,d),fill=’red’,outline=’white’)

多边形drawObj.polygons([(x,y),…],fill=’red’,outline=’white’)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 绘图
from PIL import Image, ImageDraw

newImage = Image.new('RGBA', (300, 300), "Yellow") # 建立300*300黄色底的影像
drawObj = ImageDraw.Draw(newImage)

# 绘制点
for x in range(100, 200, 3):
for y in range(100, 200, 3):
drawObj.point([(x,y)], fill='Green')

# 绘制线条, 绘外框线
drawObj.line([(0,0), (299,0), (299,299), (0,299), (0,0)], fill="Black")
# 绘制右上角美工线
for x in range(150, 300, 10):
drawObj.line([(x,0), (300,x-150)], fill="Blue")
# 绘制左下角美工线
for y in range(150, 300, 10):
drawObj.line([(0,y), (y-150,300)], fill="Blue")
newImage.save("out17_20.png")

out17_20

1
2
3
4
5
6
7
8
9
10
11
12
13
from PIL import Image, ImageDraw

newImage = Image.new('RGBA', (300, 300), 'Yellow') # 建立300*300黄色底的影像
drawObj = ImageDraw.Draw(newImage)

drawObj.rectangle((0,0,299,299), outline='Black') # 影像外框线
drawObj.ellipse((30,60,130,100),outline='Black') # 左眼外框
drawObj.ellipse((65,65,95,95),fill='Blue') # 左眼
drawObj.ellipse((170,60,270,100),outline='Black') # 右眼外框
drawObj.ellipse((205,65,235,95),fill='Blue') # 右眼
drawObj.polygon([(150,120),(180,180),(120,180),(150,120)],fill='Aqua') # 鼻子
drawObj.rectangle((100,210,200,240), fill='Red') # 嘴
newImage.save("out17_21.png")

out17_21

18、使用tkinter开发GUI

19、动画与游戏

这两章换成Qt,有时间再写