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
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') 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)
a.rotate(45, expand=True).save("out17_10_3.jpg")
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): for y in range(50, 151): newImage.putpixel((x, y), (0, 255, 255, 255)) newImage.save("out17_14_1.png") for x in range(50, 251): for y in range(151, 251): 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))
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") 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")
|
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') 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")
|
18、使用tkinter开发GUI
19、动画与游戏
这两章换成Qt,有时间再写