From charlesreid1

Installing

Pip

The python imaging library can be installed with pip:

$ pip install PIL

Importing

Sometimes you can import the PIL like this:

import PIL as ThePIL

with other installations though, you have to import it like this:

import Image

here is a link with some notes on how to use the PIL when you import it as import Image:

http://effbot.org/imagingbook/introduction.htm

Averaging

Color Averaging

See http://charlesmartinreid.com/wordpress/2012/08/python-image-averaging-and-color-averaging/

Image Averaging

Average two images, like this:

The average of these two images:

Hipstamatic 0380.JPG and Hipstamatic 0390.JPG

is:

HipstamaticAverage 0380 0390.png


Image Averaging for Time Lapses: 2 Images

Why I want to take the average of two images:

I'm taking a timelapse photo, and want to stretch out my frames per second without the video seeming too choppy. So I wanted to add an image between each photo pair that averages the two images, doubling the number of frames and smoothing image transitions in the final timelapse.

Here, image averaging is used for more subtle image differences:

The average of

ClocktowerA.jpg and ClocktowerB.jpg

is

ClocktowerAvgAB.jpg

the code to do this is as follows here:

import os, numpy, Image

imgA = "ClocktowerA.JPG"
imgB = "ClocktowerB.JPG"

imlist = [imgA,imgB]

# Assuming all images are the same size, get dimensions of first image
w,h=Image.open(imlist[0]).size
N=len(imlist)

# Create a numpy array of floats to store the average (assume RGB images)
arr=numpy.zeros((h,w,3),numpy.float)


# Build up average pixel intensities, casting each image as an array of floats
for im in imlist:
    imarr=numpy.array(Image.open(im),dtype=numpy.float)
    arr=arr+imarr/N


# Round values in array and cast as 8-bit integer
arr=numpy.array(numpy.round(arr),dtype=numpy.uint8)

# Generate, save and preview final image
out=Image.fromarray(arr,mode="RGB")
out.save("ClocktowerAverageAB.jpg")
out.show()


Image Averaging for Time Lapses: Many Images

Having multiple images, and adding an average of each pair, is slightly more involved than the two-image case.

For my application, I'm interested in stitching a large number of images together, with average images in between each.

This looks for a set of images with the extension .JPG. It then blends them together, and renames the files so they are in-sequence images, with the first image placed first, the blended image placed second, and the third image placed third.

import os, numpy, Image
from subprocess import call

allfiles=os.listdir(os.getcwd())
imlist=[filename for filename in allfiles if  filename[-4:] in [".JPG"]]
N=len(imlist)

Nnewimages = len(range(1,N))

# Assuming all images are the same size, get dimensions of first image
w,h=Image.open(imlist[0]).size

# Create a numpy array of floats to store the average (assume RGB images)
arr=numpy.zeros((h,w,3),numpy.float)

# Round values in array and cast as 8-bit integer
arr=numpy.array(numpy.round(arr),dtype=numpy.uint8)

ii = 0
jj = 0

for ii in range(1,N):

    ##################################
    # Step 1: Save first picture

    call(["cp",imlist[ii-1],"processed_%d.jpg"%(jj)])
    jj = jj + 1
    print jj

    impair = [imlist[ii],imlist[ii-1]]

    ##################################
    # Step 2: Save average picture

    # Build up average pixel intensities, casting each image as an array of floats
    for im in impair:
        imarr=numpy.array(Image.open(im),dtype=numpy.float)
        arr=arr+imarr/N

    # Generate, save and preview final image
    out=Image.fromarray(arr,mode="RGB")
    out.save("processed_%d.jpg"%(jj))
    #out.show()
    jj = jj + 1
    print jj,"..."

##################################
# Step 3: Save last picture

call(["cp",imlist[N],"processed_%d.jpg"%(jj)])
jj = jj + 1