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

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.

Related

Plotting inside a picture or video

I have a system written in C++ that do some processing on some input video. As a result of this processing I get some statistics .
Right now the system produces number data that is saved on a text file.
Later I have written some python script that takes these numbers and shows some plots.
I was thinking, it would be nice if I can put this info in the video.
If it is just numerical values I can do that easily with the text writing functions in OpenCV.
However, perhaps it would be nice to include a small plot in the video.
How can I do that? Does OpenCV has something of this sort?
I also found this question about plotting with C++. I wonder if some answers there could be of any help. (As you can see the nature of the question although related is a bit different)
I am thinking I can go in one of two ways:
Implementing this plotting and then embedding the plot directly in my C++ code immediately after finding the values
or
Processing the video, getting the values in a text file as now, and then processing this text file, the video in Python to embed a plot in it
The key concept here is embedding a small plot in a video. How can I do that in either C++ or python?

In atom editor using hydrogen for jupyter/python, how to extract/save an image?

Is there a straightforward way (one line code or even context menu) to save an image that is shown in a return bubble from the Hydrogen package in the Atom.io editor?
I actually managed to extract the base64 code for the png from the developer-pane inspecting the bubble, and this can be used to generate the image. However, that is very cumbersome and it's easier to use pyhton to save the image (see below).
Remark:
Of course, since python is used to generate the image, it is in principle possible to save the image by injecting python code, however, sometime I just want to save the picture generated without going back to the file that contains the code that generates it.
This is not currently possible due to an Atom limitation. See: https://github.com/nteract/hydrogen/issues/245

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 - Identifying/Processing an Image

I'm looking in to learning about processing and handling images with Python. I'm experimenting with searching the inside of an image for a specific picture. For example, this picture has two images in it that are the same;
In Python, how would I go about detecting which two images are the same?
I would recommend you to take a look at OpenCV and PIL, if you want to implement simple (or complex) algorithms on your own.
Furthermore you can integrate OpenCV with PIL and also numpy, which makes it a really powerful tool for this kind of jobs.

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