在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())) 工作。 –