您的位置:首页 > 编程语言 > Python开发

【python学习记录】--1--对图片的基本操作

2017-11-19 11:23 465 查看
安装好PyCharm 软件,配置好环境Python 2.7和Python 3.6,安装两个版本的python,方便验证网上的代码。

1、打开图片

代码里打开图片的方式很多,这里记录两个

1.1 使用opencv(cv2)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cv2
# image read
img = cv2.imread("1.jpg")
#image show
cv2.namedWindow("Image")
cv2.imshow("Image", img)
cv2.waitKey (0)
cv2.destroyAllWindows()


另外,imread(filename[, flags])是可以加参数,flags=0 显示为灰度图像,有兴趣的朋友可以去爬源码

1.2使用Python Imaging Library(PIL)+ matplotlib

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import PIL.Image as Image
import matplotlib.pyplot as plot
#image read
image = Image.open("1.jpg")
#image show
plot.imshow(img) # using matplotlib
plot.show()

2、图片转换为数据

打开一张图片,提取出图片中的像素数据

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import PIL.Image as Image

def image_to_array(imagefile):
# 图片转化为数组并存为二进制文件;
print("开始将图片转为数组")
image = Image.open(imagefile)
result = np.array(image)
# print ("image shape = %d x %d x %d" % img.shape)
# print(result)
return result

if __name__ == "__main__":
images_array = image_to_array("2.jpg")

为方便测试,制作了一张5x2像素的图片2.jpg 

 (图片文件在这里  图片很小),图片RGB数据为

{[255 0 0 ,0 200 0 ,0 0 100, 10 20 30 ,111 222 123] 

 [255 0 0 ,0 200 0 ,0 0 100, 10 20 30 ,111 222 123] }

程序输出为:

img shape = 2 x 5 x 4

[[[255   0   0 255]

  [  0 200   0 255]

  [  0   0 100 255]

  [ 10  20  30 255]

  [111 222 123 255]]

 [[255   0   0 255]

  [  0 200   0 255]

  [  0   0 100 255]

  [ 10  20  30 255]

  [111 222 123 255]]]

提取出的数组数据是RGB顺序排列的,最后一个数据255可能是表示意义暂不明确,后面再实验测试

下面实现分离出RGB单一色数据的功能def image_to_RGBarray(imagefile):
image = Image.open(imagefile)
img = image.split() # RGB通道分离
r = img[0]
g = img[1]
b = img[2]
r_arr = np.array(r) # 把分离出的R色转换成颜色数组,数组行列和像素数一致
g_arr = np.array(g)
b_arr = np.array(b) # 分离出来的数据是二维的

result = np.zeros((3, r_arr.shape[0], r_arr.shape[1])) # 创建一个三维数组
result[0] = r_arr # 把颜色数据分别按R数据、G数据、B数据的顺序存放在三维数组中
result[1] = g_arr
result[2] = b_arr
# print(result)
return result调用执行一下看看结果
if __name__ == "__main__":
imagearr = images_array = image_to_array("2.jpg")
print(imagearr)
RGBarr = image_to_RGBarray("2.jpg")
print (RGBarr)运行结果如下:
[[[255   0   0 255]

  [  0 200   0 255]

  [  0   0 100 255]

  [ 10  20  30 255]

  [111 222 123 255]]

 [[255   0   0 255]

  [  0 200   0 255]

  [  0   0 100 255]

  [ 10  20  30 255]

  [111 222 123 255]]]

[[[ 255.    0.    0.   10.  111.]

  [ 255.    0.    0.   10.  111.]]

 [[   0.  200.    0.   20.  222.]

  [   0.  200.    0.   20.  222.]]

 [[   0.    0.  100.   30.  123.]

  [   0.    0.  100.   30.  123.]]

 [[ 255.  255.  255.  255.  255.]

  [ 255.  255.  255.  255.  255.]]]

3、把数据保存成图像文件

好了,还需要把处理过的数据恢复成图像文件。
def RGBarray_to_image(imagearray, filename):
# 从image数据恢复为图片
r = Image.fromarray(imagearray[0]).convert('L')
g = Image.fromarray(imagearray[1]).convert('L')
b = Image.fromarray(imagearray[2]).convert('L')
image = Image.merge("RGB", (r, g, b))
# 显示图片
pyplot.imshow(image)
pyplot.show()
# 保存图片
print ()
image.save(filename)
if __name__ == "__main__":
# imagearr = images_array = image_to_array("3.jpg")
RGBarr = image_to_RGBarray("3.jpg")
RGBarray_to_image(RGBarr, "copy3.jpg")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 图片