26
loading...
This website collects cookies to deliver better user experience
pip install pillow
easy_install Pillow
$ sudo pip install Pillow
sudo pip install Pillow
Image
from PIL and then call the Image.open()
function, passing the image path. A value of the Image object data type is returned by the Image.open()
function, and this image object can be used to perform various image manipulations with the help of other methods. All the modifications made to the image object can be saved to an image file with the save()
method. The show()
method displays the image that has been loaded in the image object.# importing the module
from PIL import Image
# opening the image stored in the local path.
im = Image.open('D:/Image/Kingfisher.jpg')
im.show()
# importing the module
from PIL import Image
im = Image.open('D:/Image/Kingfisher.jpg')
# Find image dimensions i.e. width and height
print("Image size:", im.size)
# Find the image format
print("Image format:", im.format)
# Find the image mode i.e. RGB, RGBA, HSV etc.
print("Image mode:", im.mode)
Image size: (1280, 853)
Image format: JPEG
Image mode: RGB
save()
method in Image module. This method takes two arguments, first the name of the new file and second the format into which we want to convert the image.#importing the module
from PIL import Image
im = Image.open('D:/Image/Kingfisher.jpg')
# Changing image format and saving the new image
im.save('Kingfisher.png', 'png')
Image.crop()
method helps in cropping the image and we can specify the dimensions of the crop. The crop()
method takes a 4-valued tuple defining the left, upper, right, and lower pixel coordinates.#importing the module
from PIL import Image
im = Image.open('D:/Image/Kingfisher.jpg')
# Cropping the loaded image using crop()
img_crop = im.crop((450,20,1000,500))
img_crop.show()
Image.rotate()
function can be used to rotate an image. It takes the rotation degree value as a parameter and returns a rotated copy of the image.from PIL import Image
im = Image.open('D:/Image/Kingfisher.jpg')
# Rotating image using rotate() function by 90 degrees.
img_rotate = im.rotate(90)
img_rotate.save('Kingfisher_Rotated.jpg')
Image.resize()
function can be used to resize an image to required height and width. The function takes width and height as a tuple and returns an image object of resized format.from PIL import Image
im = Image.open('D:/Image/Kingfisher.jpg')
# Resizing an image using resize() function.
img_resize = im.resize((500,150))
img_resize.show()
ImageFilter
module consists of definitions for a pre-defined set of filters. These filters can be used with the filter()
method and we can even blur an image by applying ImageFilter.BLUR
on the original image. The filter function then returns an image object that contains the blurred copy of the original image.# importing Image and ImageFilter modules
from PIL import Image, ImageFilter
im = Image.open("D:/Image/Kingfisher.jpg")
# Applying blur operation on the image
img_blur = im.filter(ImageFilter.BLUR)
img_blur.show()
Image.convert()
method allows us to convert colored images into grayscale images (black and white).# importing Image and ImageFilter modules
from PIL import Image, ImageFilter
im = Image.open("D:/Image/Kingfisher.jpg")
grayscale = im.convert('L')
grayscale.show()
ImageFilter
module contains various filters that can be used for image enhancement such as CONTOUR, DETAIL, EDGE_ENHANCE, EMBOSS, SMOOTH, SHARPEN, etc. Some of these filters are displayed below:# importing modules
from PIL import Image, ImageFilter
im = Image.open("D:/Image/Kingfisher.jpg")
# Applying filters
img1 = im.filter(ImageFilter.CONTOUR)
img2 = im.filter(ImageFilter.EMBOSS)
img1.show()
img2.show()
ImageDraw
module, we can easily draw simple 2D graphics on top of existing Image objects. After importing the ImageDraw module, we simply create an ImageDraw.Draw
object passing the Image object as the parameter. Then we can we can draw various objects such as line, rectangle, circle, triangle, etc. using the inbuilt functions. Here we have drawn a rectangle on the existing image. The rectangle()
function takes three parameters: the position of the four corner points, the outline color and the fill color values.# importing required modules
from PIL import Image, ImageDraw
im = Image.open("D:/Image/Kingfisher.jpg")
draw = ImageDraw.Draw(im)
# Drawing a rectangle using rectangle method()
draw.rectangle((200,200,400,400), fill = "red", outline = "blue")
im.show()
ImageColor.getrgb()
method converts a color value passed as a string into the equivalent RGB format. It takes the color string as an input and returns a tuple of RGB values.# importing ImageColor module
from PIL import ImageColor
im1 = ImageColor.getrgb("#00ff00")
print(im1)
im2 = ImageColor.getrgb("yellow")
print(im2)
(0, 255, 0)
(255, 255, 0)
new()
and getrgb()
method:# importing required modules
from PIL import Image, ImageColor
img = Image.new("RGB", (500, 500), ImageColor.getrgb("#FF7103"))
img.show()