Python下將陣列 矩陣轉換成Image類

2021-07-30 23:00:46 字數 1048 閱讀 9158

先說明一下為什麼要將陣列轉換成image類。我處理的影象是fits (flexible image transport system)檔案,是一種灰度影象檔案,也就是單通道影象。fits影象的特點是灰度值取值為0~65535,這類影象在python下讀成陣列首先是不能直接轉換成位圖,也就不能用opencv、image等方法了。如果是普通的jpg影象,用自帶的image庫就能實現很多功能。

下方的這幅圖就是通過python下的image庫中的rotate函式實現的

接下來貼上**。

import image

import numpy as np

#生成乙個陣列,維度為100*100,灰度值一定比255大

narray=np.array([range(10000)],dtype='int')

narray=narray.reshape([100,100])

#呼叫image庫,陣列歸一化

img=image.fromarray(narry*255.0/9999)

#轉換成灰度圖

img=img.covert('l')

#可以呼叫image庫下的函式了,比如show()

img.show()

#image類返回矩陣的操作

imgdata=np.matrix(img.getdata(),dtype='float')

imgdata=imgdata.reshape(narry.shape[0],narry.shape[1])

#影象歸一化,生成矩陣

這是剛才**生成的,是不是很簡單?

Python 將拼音轉換成漢字

利用pinyin2hanzi 可以將拼音轉換為漢字,可以作為拼音輸入法的轉換引擎 示例 def pinyin 2 hanzi pinyinlist from pinyin2hanzi import defaultdagparams from pinyin2hanzi import dag dagpa...

python 將繁體轉換成簡體

usr bin env python coding utf 8 from util.langconv import converter defcat to chs sentence 傳入引數為列表 將繁體轉換成簡體 param line return sentence join sentence s...

將陣列轉換成字串

toarray 摘要 從 system.collections.generic.ienumerable建立乙個陣列。引數 source 要從其建立陣列的 system.collections.generic.ienumerable。型別引數 tsource source 中的元素的型別。返回結果 乙...