1 / 12

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

herrod-shaw
Télécharger la présentation

Image Processing library (PIL)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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!

  2. 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”

  3. structure of A 24 bit Image Location=(col,row) (14,14) cols,rows=im.size rows=18 and cols=35 Contents=(R,G,B) tuple i.e. (255,34,128)

  4. 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 0 0 1 0 2 col 0 0 3 0 4 1 0 1 1 1 2 col 1 1 3 1 4 2 0 2 1 2 2 col 2 2 3 2 4

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

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

  7. I created this image without antialising Lets turn this red to grey

  8. Convert red pixels to gray by looping thru every pixel import matplotlib.pyplot as plt from PIL import Image im = Image.open('thingsnoanti.png') # read in image pix = im.load() #allows fast access for pixel access cols,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.

  9. Using filters • The filter operations (BLUR, SMOOTH, SHARPEN etc) will loop thru the entire image for you. Here is the result of blur as applied to our original image.

  10. And here is the code From ImageFilter module BLUR CONTOUR DETAIL EDGE_ENHANCE EDGE_ENHANCE_MORE EMBOSS FIND_EDGES SMOOTH SMOOTH_MORE SHARPEN • import 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()

  11. Plot a Histogram of a gray-scale image • import 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 plotthe list NOT imshow() • plt.show()

  12. Here is an example

More Related