Create a new images with smaller images using PIL - python

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

Related

Image to pixel array in python

Info: I have 30,000 jpg images that I need to convert into (NumPy) pixel arrays.
Problem: I have tried using Pillow to do the image conversions but it does about 2 images a second which would take hours to complete.
from PIL import Image
for img_num in range(30_000):
img = Image.open(img_dir+img_num+extension)
img_list.append(np.array(img))
Question: What is the best and fastest way to convert a large number of jpg images to pixel arrays using python.
I think what is taking the longest is the append() function.
Also, you are appending 30000 images to img_list, this means this single variable is extremely heavy in memory, do you actually need it? (if you image had 1000 pixels, you'd already be trying to allocate more than 30Mb)
In PIL and openCV the read oropen` functions directly make them jumpy arrays.

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.

Creating a Gtk.Image from a pixel list

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.

Loading greyscale images in wxPython

I'm writing a little desktop app using wxPython that does a bit of image manipulation, but I'm finding it's running rather slowly.
One of the biggest problems at the moment is that I am using three channels of data (RGB) where I only really need one - greyscale images are fine for my purposes.
At the moment I'm manipulating the images by loading them into a numpy array. Once the processing is done, they're converted into a wx Image object (via the ImageFromBuffer() function, which is then loaded into a StaticBitmap for the user interface. This seems like a lot of steps...
So I have two questions - the first is how would you load a numpy array directly into a greyscale wx Image? Is this even possible?
The second is more general - what is the fastest way of dealing with images in wxPython? I don't have much choice but to loop over the numpy arrays (I need the mathematical functionality), but any way of speeding things up before and after this process would be welcome!
you could do a pingpong with PIL like this :)
def convertToGrayscale(img):
import Image, ImageOps
orig = Image.new("RGB", img.GetSize())
orig.fromstring(img.GetData())
pil = ImageOps.grayscale(pil)
ret = wx.EmptyImage(pil.size[0], pil.size[1])
ret.SetData(pil.convert('RGB').tostring())
return ret
refer this link link

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