現(xiàn)在主流的web框架東莞seo軟件
.dat文件的命名規(guī)則沒有統(tǒng)一的規(guī)定,但通常以.dat為擴(kuò)展名。
目錄
- 一、 .dat格式數(shù)據(jù)
- 1.1 .dat數(shù)據(jù)用途
- 1.2 常見的.dat文件格式
- 1.3 .dat文件示例
- 二、讀取.dat格式數(shù)據(jù)
- 2.1 單個.dat文件讀取并轉(zhuǎn)換
- 2.1.1 代碼
- 2.1.2 查看數(shù)據(jù)
- 2.1.3 輸出查看8Bit圖片
- 2.2 批量.dat文件讀取并轉(zhuǎn)換
- 2.2.1 代碼參數(shù)修改
- 2.2.2 代碼
- 2.2.3 批量轉(zhuǎn)換結(jié)果
- 三、總結(jié)
一、 .dat格式數(shù)據(jù)
.dat格式數(shù)據(jù)是一種通用的二進(jìn)制文件格式,可以用于存儲各種類型的數(shù)據(jù)。.dat文件的格式可以是任意的,因此需要根據(jù)數(shù)據(jù)的具體格式來編寫代碼來讀取數(shù)據(jù)。
1.1 .dat數(shù)據(jù)用途
存儲文本數(shù)據(jù),例如用戶配置信息或日志數(shù)據(jù)。
存儲二進(jìn)制數(shù)據(jù),例如圖像數(shù)據(jù)或音頻數(shù)據(jù)。
存儲結(jié)構(gòu)化數(shù)據(jù),例如數(shù)據(jù)庫表格。
1.2 常見的.dat文件格式
文本格式:數(shù)據(jù)以字符串的形式存儲。
二進(jìn)制格式:數(shù)據(jù)以二進(jìn)制數(shù)據(jù)的形式存儲。
結(jié)構(gòu)化格式:數(shù)據(jù)以結(jié)構(gòu)化的形式存儲。
注:如果不確定.dat文件的格式,您以嘗試使用hex編輯器來查看數(shù)據(jù)的二進(jìn)制格式。
1.3 .dat文件示例
用戶配置文件:通常用于存儲用戶的個人偏好或設(shè)置。
日志文件:通常用于記錄系統(tǒng)或應(yīng)用程序的運(yùn)行情況。
圖像文件:通常用于存儲圖像數(shù)據(jù)。
音頻文件:通常用于存儲音頻數(shù)據(jù)。
數(shù)據(jù)庫文件:通常用于存儲數(shù)據(jù)庫表格的數(shù)據(jù)。
二、讀取.dat格式數(shù)據(jù)
2.1 單個.dat文件讀取并轉(zhuǎn)換
這里主要是使用numpy讀取,我自己的.dat文件存放著圖像數(shù)據(jù),且圖像數(shù)據(jù)的尺寸為256*192,學(xué)者在使用該教程中代碼時,得提前明確自己的.dat文件是否存儲的是圖像數(shù)據(jù),且明確圖像尺寸。
使用代碼修改的地方如下:
2.1.1 代碼
import numpy as np
import cv2def float32_to_unit8(img):max_val = img.max()min_val = img.min()img = (img - min_val)/(max_val - min_val) * 255img = img.astype(np.uint8)return imgdat_f = open("data/INF_AiRui/groundtruth/nuc_1879538664.dat")
# 讀取數(shù)據(jù)
data = np.fromfile(dat_f,dtype=np.uint16)# 定義圖片的列數(shù)和函數(shù)
col = 256
row = 192# 將一維data數(shù)組轉(zhuǎn)換為二位數(shù)組
dataReshape = data.reshape(row,col)# 調(diào)用float32_to_unit8函數(shù)將16bit數(shù)據(jù)轉(zhuǎn)為uint8數(shù)據(jù)
imgUint8 = float32_to_unit8(dataReshape)# 顯示uint8圖像
cv2.imshow("result_image",imgUint8)
cv2.waitKey(0)
cv2.destroyAllWindows()
2.1.2 查看數(shù)據(jù)
這里查看數(shù)據(jù)主要是通過DeBug解析每一步查看,如下:
2.1.3 輸出查看8Bit圖片
直接運(yùn)行上面代碼后就可以輸出單個.dat數(shù)據(jù)轉(zhuǎn)換后的uint8圖片,如下:
2.2 批量.dat文件讀取并轉(zhuǎn)換
2.2.1 代碼參數(shù)修改
主體代碼和上面一樣,就加了遍歷文件夾的代碼,需要修改的地方如下:
2.2.2 代碼
import numpy as np
import cv2
import os
import sysdef float32_to_unit8(img):max_val = img.max()min_val = img.min()img = (img - min_val)/(max_val - min_val) * 255img = img.astype(np.uint8)return imgdat_path = "data/INF_AiRui/groundtruth"
save_path = "data/INF_AiRui_bmp/groundtruth"dat_file = os.listdir(path = dat_path)for file_name in dat_file:dat_f = open(os.path.join(dat_path,file_name))# 讀取數(shù)據(jù)data = np.fromfile(dat_f,dtype=np.uint16)# 定義圖片的列數(shù)和函數(shù)col = 256row = 192# 將一維data數(shù)組轉(zhuǎn)換為二位數(shù)組dataReshape = data.reshape(row,col)# 調(diào)用float32_to_unit8函數(shù)將16bit數(shù)據(jù)轉(zhuǎn)為uint8數(shù)據(jù)imgUint8 = float32_to_unit8(dataReshape)out_dat_name = file_name[:-4]+".bmp"print("圖片{}轉(zhuǎn)換成功!".format(file_name[:-4]))cv2.imwrite(os.path.join(save_path,out_dat_name),imgUint8)key = cv2.waitKey(30) & 0xffif key == 27:sys.exit(0)
2.2.3 批量轉(zhuǎn)換結(jié)果
三、總結(jié)
以上就是使用Python讀取.dat格式數(shù)據(jù)并轉(zhuǎn)為.png,.jpg,.bmp等可視化格式的過程及詳細(xì)代碼,本代碼轉(zhuǎn)換只適用于.dat存儲的是圖像數(shù)據(jù),其它數(shù)據(jù)不適用。
總結(jié)不易,多多支持,謝謝!