1

im processing measured data and in a following process i create plots of them. (To be more specific its about to display orbits of a spinning shaft.) Thus i will create many plots and i want to safe them to a hdf5 file. The "workaround" i know is:

  1. saving the file to a .jgp
  2. reading it with opencv
  3. writing that array to the hdf5

This works well, but will create a mess in my working dictionary.

Here is a Code example if someone wants to just safe one ore two plots:

    fig.savefig(some_plot.jpg)  # saves the plot to working discretionary as 'some_plot.jpg'
    image = cv2.imread(some_plot.jpg)  # reads the created .jpg as array 
    g = f.create_dataset(some_path, data=image)  # creates dataset in the hdf5 and safes the 
    image array to it
    # add !!important!! image attributes , i got these from another forum but if you convert a 
    # image in hdf5 and read the attributes you can create them by yourself
    # futher information is in the hdf5 documentation (hdfgroup.org --> HDF5 Image and Palette 
    Specification)
    g.attrs.create('CLASS', 'IMAGE', dtype='S6')                                        
    g.attrs.create('IMAGE_MINMAXRANGE', [0, 255], dtype=np.uint8)
    g.attrs.create('IMAGE_SUBCLASS', 'IMAGE_TRUECOLOR', dtype='S16')
    g.attrs.create('IMAGE_VERSION', '1.2', dtype='S4')
    g.attrs.create('INTERLACE_MODE', 'INTERLACE_PIXEL', dtype='S16')

So now my problem is that this code creates a .jpg image for every plot, which will make a mess. Is there a way of converting the picture to a numpy arrray without the need of saving it as a image. (I could delete the picture.jpg after it got saved to the hdf5, but i want to avoid that.) matplotlib.pyplot has a inbuild imread()-function that i will try in the future to safe the use of opencv. I hope someone knows a solution to save plot images directly to hdf5.

Best regards

Marius

m.ariuz
  • 11
  • 1
  • HDF5 mimics a file structure, why can you not carry that on within a single file? See [Hdf5 file for large image datasets](https://blade6570.github.io/soumyatripathy/hdf5_blog.html) which has some snippets. – Alex Oct 01 '21 at 10:11
  • Thanks for your reply. When i got it right the link you send is for reading images to hdf5 and is mainly focused on AI Training. Therefore existing images are read into hdf5. In my project im creating these images and then i could use the information in your link. But im talking about like 2000pics which is noting compared to the size needed for ai training, so storage management wont be a problem in my case. The main thing is that i want to avoid the step creating these 2000images and then reading them back to hdf5. – m.ariuz Oct 01 '21 at 10:31
  • You can save matplotlib figures to `bytesIO` which you can [read into numpy as in this answer](https://stackoverflow.com/a/61443397/3279716) or [convert the figure canvas to a string and use that](https://stackoverflow.com/a/7821917/3279716). – Alex Oct 01 '21 at 10:51
  • Thank you very much, thanks exactly what i was searching for. I will implement that in my code and then write a comment if it works. During my search i focused on hdf5 and totally forgot the step in between (plot-->numpy). – m.ariuz Oct 01 '21 at 11:34
  • That's fantastic! Once you've got a working solution make sure to add the answer here. – Alex Oct 01 '21 at 11:50

0 Answers0