Python Imaging Library: Difference between revisions
From charlesreid1
| Line 46: | Line 46: | ||
===Image Averaging for Time Lapses=== | ===Image Averaging for Time Lapses: 2 Images=== | ||
Why I want to take the average of two images: | Why I want to take the average of two images: | ||
| Line 61: | Line 61: | ||
[[Image:ClocktowerAvgAB.jpg|400px]] | [[Image:ClocktowerAvgAB.jpg|400px]] | ||
the code to do this is as follows here: | |||
<source lang="python"> | |||
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() | |||
</source> | |||
===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. | |||
Revision as of 10:24, 18 January 2014
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:
is:
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
is
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.