在RI創建包含具有以下測試代碼PNG圖像的原始載體(格式= ARGB(640×480))使用Python從包含PNG圖像的原始矢量生成圖像。
library(Cairo)
library(png)
Cairo(type='raster')
x=seq(0,3,by=0.01)
y=2*sin(2*pi*(x-1/4))
plot(x,y) #Test plot
rawdata = writePNG(dev.capture(native=TRUE), raw())
assign("rawpng", rawdata, envir = .GlobalEnv)
得到的載體是這樣的:c(89, 50, 4e, 47, 0d, 0a, 1a, 0a, 00, 00, 00, 0d, 49, 48, 44, 52, 00, 00, 02, 80, 00, 00, 01, e0, 08, 06, 00, 00, 00, 35, d1, dc, e4, 00, 00, 20, 00, 49, 44, 41, 54, 78, 9c, ec, dd, 77, af, 5d, ee, cf, 75, 71, 41, d6, 90, 99, e7, ec, b3, 66, 9f, b5, d7, 7a, d6, b3, c0, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc, cc,.................................. b3, b0, fc, 1f, fe, 86, bd, ef, 71, 27, 9e, 88, 00, 00, 00, 00, 49, 45, 4e, 44, ae, 42, 60, 82)
現在我想在使用原始矢量的Python畫布中顯示圖像,但我似乎無法做到正確。使用Rpy2和Pil,我以某種方式設法生成一個圖像,但它完全搞砸了。
rawdataVec = rpy2.robjects.globalenv['rawpng']
rawdataArray = numpy.asarray(rawdataVec)
newshape = (640,480,4)
newstrides = (rawdataArray.itemsize, rawdataArray.itemsize,rawdataArray.itemsize)
data = numpy.lib.stride_tricks.as_strided(rawdataArray, shape=newshape, strides=newstrides)
img = Image.fromstring('RGBA', (640,480), data.tostring(),'raw','RGBA',0,1)
photo = ImageTk.PhotoImage(image=img)
我知道節約的情節爲圖像文件,並重新加載它在Python就好辦多了,但我需要它沒有保存的文件。 它甚至有可能嗎?
感謝,IMG = Image.open(StringIO.StringIO(rawdataArray.tostring())) 工作。 –