image processing library (pil)

12
IMAGE PROCESSING LIBRARY (PIL) This is a package that you can import into python and it has quite a few methods that you can process image files with. There are loaders for the majority of the popular file formats The following import is required from PIL import Image The above works already in Anaconda. If you are building your own world using IDLE then you would need to download PIL and install in your environment. Auto generates x values here!

Upload: herrod-shaw

Post on 31-Dec-2015

73 views

Category:

Documents


1 download

DESCRIPTION

Image Processing library (PIL). This is a package that you can import into python and it has quite a few methods that you can process image files with. There are loaders for the majority of the popular file formats The following import is required from PIL import Image - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Image Processing library (PIL)

IMAGE PROCESSING LIBRARY (PIL)

This is a package that you can import into python and it has quite a few methods that you can process image files with. There are loaders for the majority of the popular file formats

The following import is required

from PIL import Image

The above works already in Anaconda. If you are building your own world using IDLE then you would need to download PIL and install in your environment.

Auto generates x values here!

Page 2: Image Processing library (PIL)

EXAMPLE COMMANDS IN PIL

The Image module is quite useful. Here are some examples

from PIL import Image im = Image.open("bride.jpg") #Read in the image

im.rotate(45) #Returns a rotated image, 45 degrees

im.crop(box) # Returns a copy of a rect. sub-region

im.filter(filter) #Returns a copy of an image filtered by the given #filter. For a list of available filters, see the ImageFilter module.

im.load() #Returns a high speed pixel access object.

im.resize() # Returns the image resized.

im.save(“Name”) #Save the image into file “Name”

Page 3: Image Processing library (PIL)

STRUCTURE OF A 24 BIT IMAGE

cols,rows=im.sizerows=18 and cols=35

Contents=(R,G,B) tuple i.e. (255,34,128)

(14,14)

Location=(col,row)

Page 4: Image Processing library (PIL)

LOOKING AT EVERY PIXEL

In order to process each pixel in the image we need to use a double loop. Here is a simple example.

for c in range(3): #col values for r in range(5): #row values print c,r

0 00 10 2 col 00 30 41 01 11 2 col 11 31 42 02 12 2 col 22 32 4

Page 5: Image Processing library (PIL)

ANTIALISING

This is a process that is used in image production that smooths out hard edges with a visual trick. Here is an example.

antialiased no antialising

Page 6: Image Processing library (PIL)

PAINT.NET ANTIALISING

Every image processing program has a button or icon to click to turn on/offantialising. The above is paint.nets method. Note you must be in one of thedrawing tools, ie pen to see the control. Turn it off or on BEFORE you draw.

Page 7: Image Processing library (PIL)

I CREATED THIS IMAGE WITHOUT ANTIALISING

Lets turn this red to grey

Page 8: Image Processing library (PIL)

CONVERT RED PIXELS TO GRAY BY LOOPING THRU EVERY PIXEL

import matplotlib.pyplot as pltfrom PIL import Image

im = Image.open('thingsnoanti.png') # read in imagepix = im.load() #allows fast access for pixel accesscols,rows=im.size #get dimensions from size tuple#look at every single pixel and if red turn it to gray. Capice?!for r in range(rows): for c in range(cols): if (pix[c,r] == (255,0,0)): #if pixel is red pix[c,r]=(220,220,220) #set it gray

plt.imshow(im)plt.show()

Note that you can do anything you want to every pixel using the above method. Just change the blue lines.

Page 9: Image Processing library (PIL)

USING FILTERS

The filter operations (BLUR, SMOOTH, SHARPEN etc) will loop thru the entire image for you.

Here is the result of bluras applied to our originalimage.

Page 10: Image Processing library (PIL)

AND HERE IS THE CODEimport matplotlib.pyplot as plt

from PIL import Image

from PIL import ImageFilter

im = Image.open('thingsnoanti.png')

plt.figure(1) #work on figure 1

plt.imshow(im) #show original image

plt.show()

im1 = im.filter(ImageFilter.BLUR) #lets blur it

plt.figure(2) #Work on figure 2

plt.imshow(im1) #show new image

plt.show()

From ImageFilter moduleBLURCONTOURDETAILEDGE_ENHANCEEDGE_ENHANCE_MOREEMBOSSFIND_EDGESSMOOTHSMOOTH_MORESHARPEN

Page 11: Image Processing library (PIL)

PLOT A HISTOGRAM OF A GRAY-SCALE IMAGEimport matplotlib.pyplot as plt

from PIL import Image

im = Image.open('sea.gif')

plt.figure(1)

plt.imshow(im) #show original image

plt.show()

hist = im.histogram()

#note that the hist here is a list not an image

plt.figure(2)

plt.plot(hist) #So we must plot the list NOT imshow()

plt.show()

Page 12: Image Processing library (PIL)

HERE IS AN EXAMPLE