[gdal-dev] [GDAL Python] Save a dataset to an in memory Python Bytes object

Even Rouault even.rouault at spatialys.com
Wed Aug 10 07:11:40 PDT 2016


Le mercredi 10 août 2016 15:35:19, domlysz a écrit :
> Hi all,
> 
> I'm trying to use GDAL Python API to save a Numpy array directly to a
> Python bytes object in PNG format. Then, bytes data will be pushed as BLOB
> in a SQlite database, so I want to avoid using an intermediate file on
> disk.
> 
> I wish to reproduce something like this snippet with PIL/PILLOW:
> 
> 
> b = io.BytesIO()
> img = Image.fromarray(data)
> img.save(b, format='PNG')
> data = b.getvalue() #convert bytesio to bytes
> 
> 
> For now I tried to write my Numpy array in a memory dataset:
> 
> img_h, img_w, nbBands = data.shape
> mem = gdal.GetDriverByName('MEM').Create('', img_w, img_h, nbBands,
> gdal.GDT_Byte)
> for bandIdx in range(nbBands):
> 	bandArray = data[:,:,bandIdx]
> 	mem.GetRasterBand(bandIdx+1).WriteArray(bandArray)
> 
> and then save the dataset to a bytes object using the CreateCopy method:
> 
> b = io.BytesIO()
> out = gdal.GetDriverByName('PNG').CreateCopy(b, mem)
> 
> But CreateCopy only accept a valid string path not a bytes object.
> 
> 
> I know it's possible to read a bytes stream through a virtual memory file
> 
> req = urllib.request.Request(url)
> handle = urllib.request.urlopen(req)
> data = handle.read()
> vsipath = '/vsimem/img'
> gdal.FileFromMemBuffer(vsipath, data)
> ds = gdal.Open(vsipath)
> 
> But I did not find any information on how to do the inverse of this.

gdal.GetDriverByName('PNG').CreateCopy('/vsimem/output.png', mem)

# Read /vsimem/output.png 
f = gdal.VSIFOpenL('/vsimem/output.png', 'rb')
gdal.VSIFSeekL(f, 0, 2) # seek to end
size = gdal.VSIFTellL(f)
gdal.VSIFSeekL(f, 0, 0) # seek to beginning
data = gdal.VSIFReadL(1, size, f)
gdal.VSIFCloseL(f)

# Cleanup
gdal.Unlink('/vsimem/output.png')


> It's
> seems the ReadRaster method return bytes data but if I try to reopen this
> output with PIL, it can't recognize the image format.

Yes, because ReadRaster() return the uncompressed data.

Even

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list