qpython pillow_图像识别的前期工作——使用pillow进行图像处理

pillow是个很好用的python图像处理库,可以到官方网站下载最新的文件。如果官网的任何PIL版本都不能与自己的python版本对应,或安装成功后发现运行出错,可以尝试从一个非官方的whl网站下载:http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy 这个网站的内容相当丰富,而且版本齐全。

打开图片

from PIL importImageimportmatplotlib.pyplot as plt

img= Image.open('girl.png')

img.show()

1203675-20170812231658663-672338926.png

控制台显示:size=(461, 603), mode=RGBA, format=PNG

代码很简单,但PIL使用操作系统的默认方式打开图片,我们需要用一些更牛叉的方式打开:

1 from PIL importImage2 importmatplotlib.pyplot as plt3

4 img = Image.open('girl0.png')5 model = img.convert('L')6 plt.figure("girl")7 #the argument comp is Colormap

8 plt.imshow(model, cmap='pink')9 plt.show()

其中img.convert指定一种色彩模式:

1 (1-bit pixels, black and white, stored with one pixel per byte)

L (8-bit pixels, black and white)

P (8-bit pixels, mapped to any other mode using a colour palette)

RGB (3x8-bit pixels, true colour)

RGBA (4x8-bit pixels, true colour with transparency mask)

CMYK (4x8-bit pixels, colour separation)

YCbCr (3x8-bit pixels, colour video format)

I (32-bit signed integer pixels)

F (32-bit floating point pixels)

1203675-20170812225615788-1496474004.png

分离rgba

rgb指红绿蓝光色三原色,a指alpha通道,一般用作不透明度参数

1203675-20170812231255695-1856948936.png

img = Image.open('girl0.png')#分离rgba

r, g, b, a =img.split()

plt.figure("girl0")

plt.imshow(r)

plt.show()

1203675-20170812231543210-1913684244.png

需要注意的是,并非所有图片都有alpha通道,此时 img.split()仅能返回r,g,b

显示多个图片

from PIL importImageimportmatplotlib.pyplot as plt

img= Image.open('girl0.png')

gray= img.convert('L')#分离rgba

r, g, b, a =img.split()

plt.figure("girl")defsetPlot(num, title):#subplot(nrows, ncols, plot_number)

#图表的整个绘图区域被等分为numRows行和numCols列,然后按照从左到右、从上到下的顺序对每个区域进行编号,左上区域的编号为1

plt.subplot(2, 3, num)

plt.title(title)

plt.axis('off')

setPlot(1, 'origin')

plt.imshow(img)

setPlot(2, 'gray')

plt.imshow(gray, cmap='gray')

setPlot(3, 'rgba')#合并rgba

plt.imshow(Image.merge('RGBA', (r, g, b, a)))

setPlot(4, 'r')

plt.imshow(r)

setPlot(5, 'g')

plt.imshow(g)

setPlot(6, 'b')

plt.imshow(b)

1203675-20170812233336554-892391128.png

二值化处理

到了关键时刻

from PIL importImageimportmatplotlib.pyplot as plt#二值化处理

img = Image.open('girl0.png')

gray= img.convert('L')

WHITE, BLACK= 1, 0

img_new= gray.point(lambda x: WHITE if x > 128 elseBLACK)

plt.imshow(img_new, cmap='gray')

plt.show()

1203675-20170812232833663-1222501567.png

图片由像素组成,每个像素对应着rgb值,整个图片可以看成一个矩阵。我们将大于128的像素点转换为1,其它转换为0。如果有一张背景色是彩色的手写文字,经过二值化处理后得到这样的图片:

1203675-20170812234034742-1818893948.png

图片压缩

如果图片大小不一,不利于下一步工作,在此需要将图片压缩成统一大小,对于手写数字,可将其压缩为32*32

1 #等比例压缩图片

2 #参考 http://fc-lamp.blog.163.com/blog/static/174566687201282424018946/

3 def resizeImg(**args):4 #dst_w,dst_h 目标图片大小, save_q 图片质量

5 args_key = {'ori_img':'', 'dst_img':'', 'dst_w':'', 'dst_h':'', 'save_q':75}6 arg ={}7 for key inargs_key:8 if key inargs:9 arg[key] =args[key]10

11 im = Image.open(arg['ori_img'])12 ori_w, ori_h =im.size13 widthRatio = heightRatio =None14 ratio = 1

15 if (ori_w and ori_w > arg['dst_w']) or (ori_h and ori_h > arg['dst_h']):16 if arg['dst_w'] and ori_w > arg['dst_w']:17 widthRatio = float(arg['dst_w']) /ori_w18 if arg['dst_h'] and ori_h > arg['dst_h']:19 heightRatio = float(arg['dst_h']) /ori_h20

21 if widthRatio andheightRatio:22 if widthRatio

1203675-20170814170641271-1061286720.png

1203675-20180307175238882-951227739.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值