Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- # ImageProc.py
- # Author: Abdul Fatir
- # E-Mail: abdulfatirs@gmail.com
- # The Image module contains the basic functions of image modification
- from PIL import Image
- # The ImageFilter module contains the filters that can be applied on the image
- from PIL import ImageFilter
- # Example 1 : Creating a new image and saving it with custom pixel data
- Width,Height = ImageSize = (1280,800)
- # Image.new(IMAGE_MODE, IMAGE_SIZE) where IMAGE_SIZE is a two tuple containing (Width,Height)
- # IMAGE_MODE is string and can be "RGB" for RGB Images, "L" for Gray-scale and few more.
- customPixImage = Image.new("RGB", ImageSize)
- PixelData = customPixImage.load()
- # Now we add our custom pixel data to the 2D array PixelData
- for y in range(Height):
- for x in range(Width):
- PixelData[x,y] = (100, x*255/1280, y*255/800) # PixelData[x,y] = (R,G,B)
- customPixImage.save("CustomPixelImage.png")
- del customPixImage
- # Example 2 : Opening an image, cropping, resizing and converting it to thumbnail
- ImgName = 'church.png'
- # Image.open(FILE_NAME) is used to open an image
- churchImage = Image.open(ImgName)
- W,H = churchImage.size
- print '[',ImgName,'] - Format: ',churchImage.format,', Mode: ',churchImage.mode,', Size: ',churchImage.size
- # The crop(box) function takes a four tuple containing co-ordinates of top-left and bottom-right
- croppedImage=churchImage.crop((0,0,300,200))
- croppedImage.save('cropped.png')
- del croppedImage
- # The resize((W,H)) function takes a two tuple containing width and height
- resizedImage=churchImage.resize((300,200))
- resizedImage.save('resized.png')
- del resizedImage
- # The thumbnail((W,H)) function takes a two tuple containing width and height
- thumb=churchImage.copy()
- thumb.thumbnail((200,200))
- thumb.save('thumb.png')
- del thumb
- # Example 3 : Converting images to different modes
- grayscale = churchImage.convert("L")
- grayscale.save('grayscale.png')
- del grayscale
- bw = churchImage.convert("1")
- bw.save('blackwhite.png')
- del bw
- cmyk = churchImage.convert("CMYK")
- cmyk.save('cmyk.jpg')
- del cmyk
- # Example 4 : Applying filters to images
- blur = churchImage.filter(ImageFilter.BLUR)
- blur.save('blurred.png')
- del blur
- emboss = churchImage.filter(ImageFilter.EMBOSS)
- emboss.save('embossed.png')
- del emboss
- contour = churchImage.filter(ImageFilter.CONTOUR)
- contour.save('contoured.png')
- del contour
- detail = churchImage.filter(ImageFilter.DETAIL)
- detail.save('detail.png')
- del detail
- edge= churchImage.filter(ImageFilter.EDGE_ENHANCE)
- edge.save('edgeenhanced.png')
- del edge
- smooth= churchImage.filter(ImageFilter.SMOOTH)
- smooth.save('smooth.png')
- del smooth
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement