Python Forum
Convert numpy array to image without loading it into RAM.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert numpy array to image without loading it into RAM.
#8
Some time ago, I used h5py to record pictures into a h5 file (very usefull and powerfull); in the following example:
  1. a basic image is created using matplotlib
  2. the image is saved into the h5 file as a picture (you can see it under hdfview for instance)
  3. the image is saved into the h5 file as an array
  4. the previous picture is recovered an saved locally into a gif image

(you can create a gif animated with ImageMagick for instance, but it's another topic)

Here after 2 screenshots of the picture into hdfview.

Maybe it add some features on your project.

Paul


import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvas
import os, h5py
 
Path = str(os.getcwd())

### 0) to create a fig
plt.style.use('_mpl-gallery')

# make data
x = np.linspace(0, 10, 100)
y = 4 + 2 * np.sin(2 * x)

# plot
fig, ax = plt.subplots(figsize=(16,16))
ax.plot(x, y, linewidth=2.0)

ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
       ylim=(0, 8), yticks=np.arange(1, 8))

plt.show()
 
 
## 1) Convertion  Matplotlib fig into aNumpyArray ---> mandatory to change the picture into a matrix      
canvas = FigureCanvas(fig)
canvas.draw()
FigArray = np.array(canvas.renderer.buffer_rgba())
 
## 2) write directly a picture into the h5 file --> Note dataset is MANDATORY
h5 = h5py.File(Path + '/test_picture.h5', 'w')
ImageDataset = h5.create_dataset(name = "Example_picture", data = FigArray, dtype = 'uint8', compression = 'gzip')
ImageDataset.attrs["CLASS"] = np.string_("IMAGE")
ImageDataset.attrs["IMAGE_VERSION"] = np.string_("1.2")
ImageDataset.attrs["IMAGE_SUBCLASS"] = np.string_("IMAGE_TRUECOLOR")
ImageDataset.attrs["INTERLACE_MODE"] = np.string_("INTERLACE_MODE")
ImageDataset.attrs["IMAGE_MINMAXRANGE"] = np.uint8(0.255)
h5.close()


## 3) Write the fig into a basic array (the h5 file)
h5 = h5py.File(Path + '/test_array.h5', 'w')
NpArrays = h5.create_group('Fig')
DatasetFigs = NpArrays.create_dataset(name = 'Example of array', data = FigArray, dtype='f', compression = 'gzip')
h5.flush()
h5.close()


## 4) array recovering => save into a gif file
from PIL import Image as im
with h5py.File(Path + '/test_picture.h5','r') as pict:
    data = pict.get('/Example_picture')
    Array = np.array(data)
    
    # array is converted int a picture
    Picture = im.fromarray(Array)
    Picture.save(Path + '/export.gif')

Attached Files

Thumbnail(s)
       
Reply


Messages In This Thread
RE: Convert numpy array to image without loading it into RAM. - by paul18fr - Feb-08-2024, 09:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Numpy, array(2d,bool), flipping regions. MvGulik 2 1,087 Oct-27-2024, 11:06 AM
Last Post: MvGulik
  python code to calculate mean of an array of numbers using numpy viren 3 1,316 May-29-2024, 04:49 PM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 1,605 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Convert np Array A to networkx G IanAnderson 2 1,646 Jul-05-2023, 11:42 AM
Last Post: IanAnderson
  Expand the range of a NumPy array? PythonNPC 0 1,833 Jan-31-2023, 02:41 AM
Last Post: PythonNPC
  Change a numpy array to a dataframe Led_Zeppelin 3 2,877 Jan-26-2023, 09:01 PM
Last Post: deanhystad
  from numpy array to csv - rounding SchroedingersLion 6 6,646 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  numpy.array has no attribute head Led_Zeppelin 1 3,417 Jul-13-2022, 12:56 AM
Last Post: Led_Zeppelin
  Seeing al the data in a dataframe or numpy.array Led_Zeppelin 1 1,788 Jul-11-2022, 08:54 PM
Last Post: Larz60+
  go over and search in numpy array faster caro 7 3,354 Jun-20-2022, 04:54 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020