How to save matplotlib plot to a hdf5 file? - python

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:
saving the file to a .jgp
reading it with opencv
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

Related

How to get the image torchvision.utils.save_image saves, without reading it back from disk?

from torchvision.utils import save_image
...
save_image(im, f'im_name.png')
In my case (standard mnist), using code from here, im is a Tensor:96, and save_image works.
I want that image in memory to show it in other plots, and I don't want to read it back after saving it, which seems kind of stupid.
Is there a way to separate the functionality of generating the image and of saving it?
Edit
clarification:
I want an equivalent to
save_image(im, f'im_name.png')
reread = plt.imread(f'im_name.png')
without saving the image and reading it back.
I just want the image, and I want to save it later.
the save_image function does some work, like stacking multiple images into one, converting the tensor to images of correct sizes and so on. I want only that part without the saving to disk.
About 2 weeks later, I stumbled upon the solution by accident.
grid = torchvision.utils.make_grid(im)
grid will be the image save_image was just saving.

3D visualization of .dicom files with ipyvolume

i'm trying to visualize a set of .dicom files using pydicom and ipyvolume.
I used pydicom to read files and then sorted them by their location and turned the slices into a 3D array. I could draw a 3D model of the data using ipyvolume.pylab.plot_isosurface() although I'm not sure if this is the right way of visualizing medical images (it's all solid pixels with the same opacity and color). I've also tried ipyvolume.pylab.volshow() but that did not work.
Is there a right way to visualize medical images with ipyvolume? or this is just not the right library for that?
DICOM file does not have 'voxel' data so you can't simply plot a dicom in 3D view. You should estimate voxel data using slices of a dicom series. after that, using a 3D modeling algorithm such as Marching Cubes you can extract final 3D model. Take a look at CTU.
I haven't used ipyvolume, but looking at the documentation it ought to be able to visualize DICOM image sets.
If you want to try another package, I use SimpleITK to load DICOM images and itkwidgets do volume visualization in a Jupyter notebook.
Here's a simple notebook that load a DICOM series and displays it:
import SimpleITK as sitk
import itkwidgets
# Get the DICOM file names in the current directory
names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames('.')
# Read the DICOM series
reader = sitk.ImageSeriesReader()
reader.SetFileNames(names)
img = reader.Execute()
itkwidgets.view(img)
If the directory has more than one DICOM series in it, GetGDCMSeriesFileNames has a seriesID parameter you can give it to specify which series to load.

How to make circle-clip image glyphs in bokeh

How do I add circle-clipped image glyphs to my chart, without processing and uploading the images manually beforehand? I'm open to using other modules.
I want the end result to look something like this chart (from nytimes).
http://imgur.com/a/Nv6ta
My current understanding is that we can only load images directly from urls, which is not my desired outcome.
http://docs.bokeh.org/en/latest/docs/reference/models/glyphs/image_url.html
My current understanding is that we can only load images directly from urls
This is not correct, there is also ImageRGBA which allows for sending images as raw RGBA data, directly embedded in the Bokeh document. See, e.g., this gallery example:
http://docs.bokeh.org/en/latest/docs/gallery/image_rgba.html
So assuming that images is a Python list of 2D NumPy arrays of RGBA data for the (pre-cropped) images you want to display, then Bokeh could show them with:
p.image_rgba(image=images, x=....)
Of course, you have to convert the images to RGBA arrays yourself, and also crop them, so things may simply be easier or more ready made for this use-case with another tool.

Saving an interactive plot with information about the data series using python

I have a question related to plots created by matplotlib. I have a GUI (in python) which creates plots from *.mat files, it saves the plots as a *.JPG file. A sample plot Looks as follows:
Now as you can see there are two data series present in the plot, and now i want to save the plot like an Excel plot where i can interact with the plot and also obtain the Location of the *.mat file from which it was created.
Can this be done using Python? I need ideas from you guys
Thanks
There are of course a lot of different options to save a file containing additional information. To list a few:
Using the EXIF tags of a jpg image to store the filenames. This might require a library like piexif.
Saving a dictionary like {"files" : ["filename1", "filename2"], "image" : <imagedata>} to a file, where <imagedata> could be the raw image or a numpy array of the image data.
Pickling a dictionary like {"files" : ["filename1", "filename2"], "image" : <figure>}, where figure is the matplotlib figure object.
At the end it might be much easier to just create a lookup table with image filenames and raw data filenames to see which image belongs to which data.

How to save jpeg data that is identical to the original jpeg file using OpenCV

I'm using OpenCV and Python. I have loaded a jpeg image into a numpy array. Now i want to save it back into jpeg format, but since the image was not modified, I don't want to compress it again. Is it possible to create a jpeg from the numpy array that is identical with the jpeg that it was loaded from?
I know this workflow (decode-encode without doing anything) sounds a bit stupid, but keeping the original jpeg data is not an option. I'm interested if it is possible to recreate the original jpeg just using the data at hand.
The question is different from Reading a .JPG Image and Saving it without file size change, as I don't modify anything in the picture. I really want to restore the original jpeg file based on the data at hand. I assume one could bypass the compression steps (the compression artifacts are already in the data) and just write the file in jpeg format. The question is, if this is possible with OpenCV.
Clarified answer, following comment below:
What you say makes no sense at all; You say that you have the raw, unmodified, RGB data. No you don't. You have the uncompressed data that has been reconstructed from the compressed jpeg file.
The JPEG standards specify how to un-compress an image / video. There is nothing in the standard about how to actually do this compression, so your original image data could have been compressed any one of a zillion different ways. You have no way of knowing the decoding steps that were required to recreate your data, so you cannot reverse them.
Image this.
"I have a number, 44, please tell me how I can get the original
numbers that this came from"
This is, essentially, what you are asking.
The only way you can do what you want (other than just copy the original file) is to read the image into an array before loading into openCV. Then if you want to save it, then just write the raw array to a file, something like this:
fi = 'C:\\Path\\to\\Image.jpg'
fo = 'C:\\Path\\to\\Copy_Image.jpg'
with open(fi,'rb') as myfile:
im_array = np.array(myfile.read())
# Do stuff here
image = cv2.imdecode(im_array)
# Do more stuff here
with open(fo,'wb') as myfile:
myfile.write(im_array)
Of course, it means you will have the data stored twice, effectively, in memory, but this seems to me to be your only option.
Sometimes, no matter how hard you want to do something, you have to accept that it just cannot be done.

Categories

Resources