日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時間:2023/12/10 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python图片保存为txt文件_python + opencv实现提取png图像的像素信息并存储到txt文件中(附安装指导)... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

相關(guān)庫安裝指導:

這里我們需要 opencv_python,numpy,matplotlib庫,另外我用的是python3.6.1版本。

一般庫大家都是用pip install命令安裝的,不過不知道為啥這里的opencv_python庫總是抽風,就是安裝不了(起碼我周圍都是這樣)。

所以以上哪個庫如果下載不動啥的可以去這里下載:海克斯科技傳送門

如果不知道下載哪個版本可以通過import pip; print(pip.pep425tags.get_supported())這條命令來查看你的python所支持的whl 文件類型(否則容易發(fā)生:* is not a supported wheel on this platform錯誤)

如圖:

之后可以通過“import”對應(yīng)的庫來驗證是否安裝成功。

python + opencv實現(xiàn)提取png圖像的像素信息并存儲到txt文件中代碼:

注意這里的文件路徑要根據(jù)個人情況修改。

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("圖像的形狀,返回一個圖像的(行數(shù),列數(shù),通道數(shù)):",img.shape)

print("圖像的像素數(shù)目:",img.size)

print("圖像的數(shù)據(jù)類型:",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)

#-----------------------------------------------------------------------------

"""

將圖片數(shù)據(jù)寫入txt文件

格式:

基礎(chǔ)信息

行號:

像素值

行號:

像素值

......

"""

fname = open("C:/Users/Jake/Desktop/test01/"+txtfile,'w')

fname.write("圖像的形狀,返回一個圖像的(行數(shù),列數(shù),通道數(shù)):"+str(img.shape)+'\n')#----1

fname.write("圖像的像素數(shù)目:"+str(img.size)+'\n')#----2

fname.write("圖像的數(shù)據(jù)類型:"+str(img.dtype)+'\n')#----3

Xlenth = img.shape[1]#圖片列數(shù)

Ylenth = img.shape[0]#圖片行數(shù)

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文件中的數(shù)據(jù)讀取進blist

并顯示為"test"圖片框進行測試。

注意進行測試前需要注釋掉數(shù)據(jù)寫入模塊

中標記的六行代碼,要不然讀取會出錯誤。

"""

# 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需要轉(zhuǎn)換為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()

#----------------------------------------------------------------------

總結(jié)

以上是生活随笔為你收集整理的python图片保存为txt文件_python + opencv实现提取png图像的像素信息并存储到txt文件中(附安装指导)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。