在 RI 中,使用以下测试代码创建一个包含 PNG 图像的原始矢量。(格式 = ARGB (640 x 480))
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 中重新加载会容易得多,但我需要它而不先保存文件。甚至可能吗?