【python学习记录】--1--对图片的基本操作
2017-11-19 11:23
465 查看
安装好PyCharm 软件,配置好环境Python 2.7和Python 3.6,安装两个版本的python,方便验证网上的代码。
1、打开图片
代码里打开图片的方式很多,这里记录两个
1.1 使用opencv(cv2)
另外,imread(filename[, flags])是可以加参数,flags=0 显示为灰度图像,有兴趣的朋友可以去爬源码
1.2使用Python Imaging Library(PIL)+ matplotlib
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)
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 OpenCV学习笔记(六):图片的基本操作
- Python学习第2节(基本数据类型操作和执行流程语句)
- Linux学习记录(3)认识终端以及一些基本的操作命令
- Python学习笔记一:数据类型与基本操作
- Linux学习记录(11)LVM的基本概念和操作
- 【从零学习Python】PIL安装与基本图像操作
- Delphi7学习基本数据格式和开发操作记录
- 我的Python学习之路之基本语法--参数传递、文件操作
- django学习教程(一)如何安装python django和基本的操作
- Linux学习记录(6)磁盘分区的基本操作
- python学习总结 基本操作
- Python学习记录——基本概念
- javascript学习记录——canvas元素的基本操作
- Linux学习记录(12)ACL基本概念及基础操作
- python 学习记录(3)—文件操作
- Maven学习记录之maven基本操作命令,maven本地工厂的创建,maven骨架的生成,以及在eclipse中创建maven工程:
- python学习 变量的操作 与 基本数据类型
- ps图片基本操作记录
- Linux学习记录(13)RAID基本概念及操作
- python文件操作+字符串操作 总结(学习记录)