How to make circle-clip image glyphs in bokeh - python

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.

Related

How to save matplotlib plot to a hdf5 file?

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

"imagio.imsave" vs "imageio.core.util.Array.tofile"

I am expanding my limited Python knowledge by converting some MATLAB image analysis code to Python. I am following Image manipulation and processing using Numpy and Scipy. The code in Section 2.6.1 saves an image using both imageio.imsave and face.tofile, where type(face)=<class 'imageio.core.util.Array>'.
I am trying to understand why there are two ways to export an image. I tried web-searching tofile, but got numpy.ndarray.tofile. It's very sparse, and doesn't seem to be specific to images. I also looked for imageio.core.util.Array.tofile, but wasn't able to find anything.
Why are there two ways to export files? And why does imageio.core.util.Array.tofile seem to be un-findable online?
The difference is in what the two functions write in the file.
imageio.imsave() saves a conventional image, like a picture or photo, in JPEG/PNG format that can be viewed with an image viewer like GIMP, feh, eog, Photoshop or MSxPaint.
tofile() saves in a Numpy-compatible format that only Numpy (and a small number of other Python tools) use.

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.

Convert SVG to png or other?

Since it's very easy to display the content of a SVG file inside the iPython notebook, is there also a way (easy too) to get what we see inside a png file or other ?
from IPython.display import SVG
SVG(filename='../images/python_logo.svg')
If I do svg = SVG(filename='../images/python_logo.svg')
How can I save it to a png file ?
SVG are vectors images (the drawings are saved as commands to draw lines, circles, etc). PNGs are bitmaps. So to convert SVG to PNG, you need a renderer.
The most obvious solution is ImageMagick, a library you have already installed, as it is used in several programs. A less obvious approach is using Inkscape. Using the commandline options, it's possible to use Inkscape as a conversion program. As Inkscape is vector oriented, I suspect quality to be better than ImageMagick (which is more bitmap-minded).
As a vector image (SVG) is a text file containing drawing instructions, it's easier to understand. PNGs contain just pixel information, and, to make things worse, they are compressed with a fairly complicated algorithm. Making sense of them is not as easy.
Have a look at the Inkscape man page, it's fairly obvious how to use it. This is the IMagick convert help.

Python: Import multiple images from a folder and scale/combine them into one image?

I have a script to save between 8 and 12 images to a local folder. These images are always GIFs. I am looking for a python script to combine all the images in that one specific folder into one image. The combined 8-12 images would have to be scaled down, but I do not want to compromise the original quality(resolution) of the images either (ie. when zoomed in on the combined images, they would look as they did initially)
The only way I am able to do this currently is by copying each image to power point.
Is this possible with python (or any other language, but preferably python)?
As an input to the script, I would type in the path where only the images are stores (ie. C:\Documents and Settings\user\My Documents\My Pictures\BearImages)
EDIT: I downloaded ImageMagick and have been using it with the python api and from the command line. This simple command worked great for what I wanted: montage "*.gif" -tile x4 -geometry +1+1 -background none combine.gif
If you want to be able to zoom into the images, you do not want to scale them. You'll have to rely on the image viewer to do the scaling as they're being displayed - that's what PowerPoint is doing for you now.
The input images are GIF so they all contain a palette to describe which colors are in the image. If your images don't all have identical palettes, you'll need to convert them to 24-bit color before you combine them. This means that the output can't be another GIF; good options would be PNG or JPG depending on whether you can tolerate a bit of loss in the image quality.
You can use PIL to read the images, combine them, and write the result. You'll need to create a new image that is the size of the final result, and copy each of the smaller images into different parts of it.
You may want to outsource the image manipulation part to ImageMagick. It has a montage command that gets you 90% of the way there; just pass it some options and the names of the files in the directory.
Have a look at Python Imaging Library.
The handbook contains several examples on both opening files, combining them and saving the result.
The easiest thing to do is turn the images into numpy matrices, and then construct a new, much bigger numpy matrix to house all of them. Then convert the np matrix back into an image. Of course it'll be enormous, so you may want to downsample.

Categories

Resources