Python图像处理PIL库的操作总结
Matlab确实是一个强大的图像处理工具,不过对于一些简单的图像处理,如图像增强、图像锐化等等,调用python的第三方包PIL(Python Image Library)也可以解决问题,这里把python调用PIL做一些简单的图像处理操作总结下。
主要用到PIL库里的三个模块:Image、ImageDraw、ImageEnhance。
1.调整图像大小
import Imageimg = Image.open("1.jpg")
new_img = img.resize((128,128),Image.BILINEAR)
new_img.save("new_img.jpg")
将1.jpg的大小转为128*128,Image.BILINEAR 指定采用双线性法对像素点插值。
两幅图对比(左:原图 右:resize后)
2.旋转图像
import Imageimg = Image.open("1.jpg")
rot_img = img.rotate(45)
rot_img.save("rot_img.jpg")
把图像旋转45度。
旋转45度
相关推荐:《Python入门教程》
3.格式转换
rot_img.save("con_img.bmp")
要把上面生成的 rot_img.jpg 转换成 bmp 图像,加这一行即可。如果不指定保存格式,PIL 将自动根据文件名后缀完成格式之间的转换。
4.统计直方图
import Imageimg = Image.open("1.jpg")
new_img = img.resize((128,128),Image.BILINEAR)
rot_img = new_img.rotate(45)
print (rot_img.histogram())
Image 类实例的 histogram()方法能够对直方图数据进行统计,并将结果做为一个列表(list)返回,将打印出所有 256 个灰度级像素点个数的统计值:
[2812, 7, 18, 18, 16, 25, 18, 22, 27, 41, 24, 34, 24, 22, 23, 20, 33, 32, 22, 20, 22, 21, 21, 24, 21, 27, 3, 8, 19,18, 16, 14, 11, 12, 18, 17, 12, 7, 10, 13, 12, 14, 12, 11, 10, 20, 15, 6, 17, 15, 19, 7, 12, 11, 14, 11, 17, 10, 13,
8, 9, 10, 9, 10, 12, 8, 10, 12, 16, 7, 15, 6, 6, 15, 8, 11, 9, 10, 8, 11, 6, 8, 12, 7, 10, 16, 16, 7, 13, 5, 15, 15,
13, 8, 8, 13, 10, 19, 9, 13, 11, 8, 7, 18, 17, 9, 13, 10, 16, 22, 7, 17, 8, 14, 10, 11, 5, 13, 9, 17, 14, 14, 16, 15,
8, 22, 10, 16, 13, 12, 6, 5, 15, 9, 14, 9, 12, 8, 11, 9, 8, 7, 9, 16, 12, 9, 7, 14, 7, 6, 11, 16, 18, 10, 3, 16, 10,
14, 14, 14, 11, 9, 11, 12, 16, 11, 15, 9, 10, 5, 8, 8, 12, 11, 7, 15, 8, 20, 8, 9, 11, 14, 7, 19, 13, 9, 13, 7, 8, 8,
16, 20, 2, 16, 6, 13, 8, 11, 14, 14, 9, 14, 8, 14, 15, 14, 8, 14, 25, 14, 16, 21, 12, 10, 12, 17, 21, 15, 12, 34, 16,
18, 32, 34, 25, 33, 34, 37, 53, 60, 85, 97, 76, 90, 150, 155, 156, 186, 142, 188, 205, 178, 231, 203, 217, 214, 193,
342, 218, 218, 272, 439, 935, 429, 2787, 1935, 2812, 7, 29, 14, 13, 22, 19, 30, 33, 31, 30, 42, 24, 17, 22, 24, 23,
37, 18, 19, 21, 30, 20, 22, 14, 20, 4, 10, 23, 16, 18, 7, 14, 15, 15, 17, 12, 6, 23, 11, 4, 11, 13, 15, 15, 14, 17,
6, 19, 12, 19, 11, 13, 7, 12, 15, 12, 10, 11, 10, 2, 13, 11, 8, 17, 6, 15, 13, 11, 5, 9, 12, 10, 16, 16, 10, 6, 5, 6,
8, 11, 7, 14, 18, 11, 11, 15, 2, 10, 7, 16, 9, 11, 20, 4, 11, 12, 22, 4, 15, 8, 12, 9, 10, 12, 12, 14, 15, 15, 19,
11, 17, 6, 13, 6, 13, 6, 9, 12, 22, 13, 13, 11, 18, 13, 17, 8, 14, 14, 10, 14, 8, 11, 17, 10, 8, 11, 10, 11, 2, 6,
15, 4, 11, 17, 13, 12, 10, 3, 7, 17, 10, 12, 9, 11, 13, 11, 15, 14, 5, 8, 14, 8, 18, 15, 10, 8, 13, 16, 5, 10, 9, 10,
11, 10, 14, 10, 14, 7, 12, 12, 13, 7, 14, 14, 5, 18, 6, 12, 5, 13, 18, 5, 13, 10, 17, 6, 13, 20, 8, 9, 14, 14, 13, 21,
11, 12, 17, 14, 12, 8, 15, 13, 15, 12, 14, 19, 18, 17, 19, 27, 18, 23, 31, 27, 16, 27, 30, 28, 34, 37, 45, 40, 46,
64, 60, 83, 87, 113, 109, 127, 153, 192, 187, 233, 315, 362, 346, 359, 392, 920, 776, 514, 2183, 2670, 24, 2812, 1,
5, 7, 18, 30, 14, 33, 23, 33, 24, 43, 41, 27, 29, 22, 17, 29, 19, 26, 30, 22, 15, 24, 21, 22, 10, 11, 9, 19, 15, 18,
11, 16, 14, 11, 15, 7, 16, 7, 20, 11, 17, 10, 12, 16, 10, 9, 18, 12, 14, 13, 11, 12, 13, 13, 14, 16, 5, 13, 5, 7, 10,
10, 11, 15, 9, 18, 8, 12, 9, 9, 8, 9, 5, 13, 15, 7, 12, 8, 14, 4, 7, 10, 12, 22, 15, 9, 11, 5, 11, 8, 11, 14, 12, 9,
11, 19, 12, 20, 8, 4, 16, 12, 8, 15, 12, 13, 12, 16, 22, 11, 13, 10, 15, 14, 12, 12, 13, 8, 13, 16, 9, 14, 18, 14,
14, 15, 11, 13, 9, 12, 11, 11, 9, 9, 13, 12, 12, 10, 10, 9, 12, 4, 6, 7, 6, 15, 11, 11, 13, 10, 12, 12, 11, 12, 9,
15, 13, 9, 16, 9, 12, 14, 13, 9, 14, 13, 7, 7, 14, 7, 13, 6, 17, 9, 13, 10, 8, 13, 13, 6, 19, 9, 14, 9, 14, 10, 7, 9,
13, 12, 17, 11, 7, 5, 10, 23, 9, 18, 9, 15, 9, 15, 22, 7, 12, 21, 10, 20, 15, 15, 18, 20, 18, 12, 21, 11, 14, 21, 34,
22, 42, 44, 63, 48, 55, 56, 62, 87, 91, 106, 115, 164, 174, 183, 144, 159, 144, 156, 172, 190, 164, 177, 258, 229,
259, 243, 217, 288, 179, 846, 326, 316, 3907, 941]
下面的操作加入 ImageDraw 模块,因为绘图操作是在图像上进行的,因此实例化 Draw 类的时候要把 Image对象 img 通过参数传递给 Draw 类的构造函数。
5.绘制直线
import Image,ImageDrawimg = Image.open("1.jpg")
draw = ImageDraw.Draw(img)
width,height = img.size # 得到img的大小
draw.line(((0,0),(width-1,height-1)),fill=255) # 对角线方向绘制直线
draw.line(((0,height-1),(width-1,0)),fill=255)
img.save("cross_line.jpg")
绘制对角线
6.绘制圆
import Image,ImageDrawimg = Image.open("1.jpg")
width,height = img.size
draw = ImageDraw.Draw(img)
draw.arc((0,0,width-1,height-1),0,360,fill=255)
img.save("circle.jpg")
(0,0,width-1,height-1)指定了所画弧线的界限;0,360 是所画弧线的起始角度和终止角度; fill=255 指定了所画线的颜色。
绘制圆
下面的操作加入ImageEnhance 模块,这个模块提供了一个常用的图像增强工具箱。可以用来进行色彩增强、亮度增强、对比度增强、图像尖锐化等等增强操作。所有操作都有相同形式的接口——通过相应类的 enhance 方法实现。
7.亮度增强
import Image,ImageEnhanceimg = Image.open("1.jpg")
brightness = ImageEnhance.Brightness(img)
bright_img = brightness.enhance(2.0)
bright_img.save("bright.jpg")
brightness = ImageEnhance.Brightness(img) 这一行把 img 传给 Brightness 类,得到一个 Brightness 类实例;
bright_img = brightness.enhance(2.0) 这一行调用 brightness 实例的 enhance 方法,传入的参数指定将亮度增强 2 倍;
两幅图对比(左:原图 右:增强后)
8.图像锐化
import Image,ImageEnhanceimg = Image.open("1.jpg")
sharpness = ImageEnhance.Sharpness(img)
sharp_img = sharpness.enhance(7.0)
sharp_img.save("sharp.jpg")
两幅图对比(左:原图 右:锐化后)
9.对比度增强
import Image,ImageEnhanceimg = Image.open("1.jpg")
contrast = ImageEnhance.Contrast(img)
contrast_img = contrast.enhance(2.0)
contrast_img.save("contrast.jpg")
两幅图对比(左:原图 右:增强后)
以上是 Python图像处理PIL库的操作总结 的全部内容, 来源链接: utcz.com/z/540641.html