Creating a Gtk.Image from a pixel list - python

I am working on a GTK program that manipulates an image based on user input. I am using pypng to read the image and am now looking for a way to create a Gtk.Image from a pixel list without creating an intermediate image file.

Not entirely easy to do in Python directly. You could do this, using GtkImage, which can load images from a GdkPixbuf.Pixbuf using a method called from_pixdata().
A higher level of doing this is using (for example) the PIL (in python2) or Pillow (python3). There, you can construct images using the Image module and the PIL.Image.frombytes(), PIL.Image.fromstring(), and other methods.
There are also functions in matplotlib which convert arrays immediately to images.

Related

Custom pixelwise operation on two PIL.Image objects

I want to use PIL Images in conjunction with tkinter display. I would like to be able to do a custom pixel by pixel (probably exclusive or) operation on two images to create a third image. A cursory read of the PIL.Image documentation does not make it clear how best to get at those pixels. Any suggestion on the most efficient way to do this? (The two images will be stills from the same camera, so there will be no issue of size or mode mismatch.)
If there is a way to get access to the pixel map in such a way that I could pass it to a Numpy routine or a custom C extension, that would be ideal.

"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.

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.

Create a new images with smaller images using PIL

I am writing a photomosaic python application.
I want to know how to create the final image (.jpg) using multiple smaller images from a pool of images with Python/PIL. I have the order in which I want the various images to be in a 2-dimensional list.
Documented on the first PIL documenation page (paste() method):
http://effbot.org/imagingbook/image.htm
In addition:
http://www.daniweb.com/software-development/python/threads/128112
http://bytes.com/topic/python/answers/22566-overlaying-transparent-images-pil

Producing a color map image using Python

I have a working program in C++ that generates data for a Mandelbrot Set. I am able to get the color map image of the Mandelbrot set using gnuplot. In order to save the image, I just take a screenshot which doesn't give a very accurate image.
how I can use a Python script in order to produce and save the image.
The Python Imaging Library is the standard means to produce 2D images and image files in Python.
Source code Draw a Mandelbrot Set (Python)
PIL and NumPy ofcourse ;)

Categories

Resources