python + opencv实现提取png图像的像素信息并存储到txt文件中(附安装指导)

相关库安装指导:

这里我们需要 opencv_python,numpy,matplotlib库,另外我用的是python3.6.1版本。

一般库大家都是用pip install命令安装的,不过不知道为啥这里的opencv_python库总是抽风,就是安装不了(起码我周围都是这样)。

所以以上哪个库如果下载不动啥的可以去这里下载:海克斯科技传送门

如果不知道下载哪个版本可以通过

import pip; print(pip.pep425tags.get_supported())
这条命令来查看 你的python所支持的whl 文件类型(否则容易发生: * is not a supported wheel on this platform错误)

如图:

之后可以通过“import”对应的库来验证是否安装成功。


python + opencv实现提取png图像的像素信息并存储到txt文件中代码:

注意这里的文件路径要根据个人情况修改。

import cv2
import numpy
import pylab
import matplotlib.pyplot as plt

imgfile = input("请输入图片名:")
txtfile = input("请输入存储文本文件名:")
img = cv2.imread("C:/Users/Jake/Desktop/test01/"+imgfile,cv2.IMREAD_GRAYSCALE)
print("图像的形状,返回一个图像的(行数,列数,通道数):",img.shape)
print("图像的像素数目:",img.size)
print("图像的数据类型:",img.dtype)
#----------------------------------------------------------------------------
"""
In windows the COLOR->GRAYSCALE: Y = 0.299R+0.587G+0.114B 测试是否三个通道的值是相同的。
某些图像三通道值相同,可以直接读灰度图来代替读单一通道。
"""
# sum = 0
# ans = 0
# for i in range(562):
#     for j in range(715):
#         if not(img[i][j][0] == img[i][j][1] and img[i][j][1] == img[i][j][2]):
#             sum += 1
#         else:
#             ans += 1
# print(ans)
# print(sum)
#-----------------------------------------------------------------------------
"""
将图片数据写入txt文件
格式:
    基础信息
    行号:
        像素值
    行号:
        像素值
    ......
"""
fname = open("C:/Users/Jake/Desktop/test01/"+txtfile,'w')
fname.write("图像的形状,返回一个图像的(行数,列数,通道数):"+str(img.shape)+'\n')#----1
fname.write("图像的像素数目:"+str(img.size)+'\n')#----2
fname.write("图像的数据类型:"+str(img.dtype)+'\n')#----3
Xlenth = img.shape[1]#图片列数
Ylenth = img.shape[0]#图片行数
a = 1#----4
for i in range(Ylenth):
    fname.write(str(a) + ':'+'\n')#----5
    for j in range(Xlenth):
        fname.write(str(img[i][j])+' ')
    a += 1#----6
    fname.write('\n')
fname.close()
#---------------------------------------------------------------------------
"""
将txt文件中的数据读取进blist
并显示为"test"图片框进行测试。
注意进行测试前需要注释掉数据写入模块
中标记的六行代码,要不然读取会出错误。
"""
# blist = []
# split_char = ' '
# with open('C:/Users/Jake/Desktop/test01/'+txtfile, 'r') as bf:
#     blist = [b.strip().split(split_char) for b in bf]
#
##从txt文件读入进来的值类型是char需要转换为int
# for i in range(Ylenth):
#     for j in range(Xlenth):
#         blist[i][j] = int(blist[i][j])
#
# tlist = numpy.array(blist)
# plt.figure()
# plt.imshow(tlist)
# plt.axis('off') # 不显示坐标轴
# pylab.show()
#------------------------------------------------------------------------------
"""
将图片显示在'image'图片框
"""
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#----------------------------------------------------------------------




猜你喜欢

转载自blog.csdn.net/vocaloid01/article/details/80046421