3
我有一個包含超過200 raw
圖像的文件夾,我想他們都轉換爲png
或任何其他形式,在C這是很容易的,但在python我不知道它是如何做如何將原始圖像轉換爲Python中的PNG?
我發現這個片段
#import struct
import numpy, array, PIL, Image
from struct import *
#declarations
arr1D = array.array('H') #H is unsigned short
#------------------------------------
#read 16 bit unsigned raw depth image
#------------------------------------
w = 640
h = 480
fid = open('/home/salman/salman/NiSimpleRead_salman/data/200.raw')
#fid = open('/home/salman/test.raw')
numBytes = w*h
arr1D.read(fid, numBytes)
fid.close()
#----------------------------------------------------
#convert to float numpy array -> scale -> uint8 array
#----------------------------------------------------
numarr = numpy.array(arr1D, dtype='float');
numarr = 255 - (numarr*255.0/numarr.max())
numarr.shape = (h,w)
numarr = numarr.astype('uint8')
#======================
#IMAGES
#======================
#2D numpy array -> image
#-----------------------
img = Image.fromarray(numarr); #print data.dtype.name
#image view and save
#-------------------
#img.show()
img.save('/home/salman/test.png')
這是我能找到的唯一代碼片段,這是正確的方法嗎?
或者,在命令行中使用ImageMagick中的一個:'轉換*。RAW --format png'。 – 2012-03-29 11:25:36
或者,或者交替使用Python的ImageMagick綁定。 – 2012-03-29 11:26:17
「在C中很容易」 - 你能指出一點嗎? – jsbueno 2012-03-29 13:14:58