❗❗ This section is the second part of before hence you should read before post. We are going to continue from the last of first.
📌
CROPPING IMAGE
We are going to start with cropping image. When an image is to be cropped, a region will be needed to define the area to be cropped. Let's code it !
image = Image.open("tux.jpg")
box = (250, 200, 550, 600)
cropped = image.crop(box)
cropped.show()
We have opened tux.jpg as the first so, the box is the region to use when we crop the image. As the last we passed the box as parameter to crop() method. You probably are going to get output like this:
ROTATING IMAGE
We can also rotate image using pillow like a media player. Rotate() method does this by taking integer or float angular value as parameter.
image = Image.open("tux.jpg")
rotated = image.rotate(90)
rotated.show()
You will get this output after run the code above.
MERGING IMAGE
The other one property of PIL is merging. This allows us to merge two images. At this time we are going to use 2 different images. One of them is our tux.jpg and the other is new one, tux1.jpg. Let's code it !
image_1 = Image.open("tux.jpg")
image_2 = Image.open("tux2.jpg")
position = (350, 480)
image_1.paste(image_2, position)
image_1.show()
In the code above we have defined two images and also position as tuple. Position is used to define where to merge the image. After defining the position we have used paste method and given its parameters, position and image.
After running out the code above we get this output:
Output:
COLOUR TRANSFORMATION
We can change the colour that image has based on the mods. For instance if we look at the coloured image modes we probably see the result RGB. So we need to change this mode by another one. Alright let's try to change the mode RGB by L.
image = Image.open("tux.jpg")
img = image.convert('L')
img.show()
The code above changes the modes of image by 'L'. If we show the image the output will be greyscale.
On the other hand we also have changed the mode then let's check out the mode of image. The output will be L.
print(img.mode)
Output:
'L'
So far so good ! Alright now suppose you have an image has RGB mode and you can separate the image to subimages. Each image of subimages has a mode from the image mode. To be able to do this we need to use split() method.
image = Image.open("tux.jpg")
red, green, blue = image.split()
print(green.getbands(), green.getbands(), blue.getbands())
Output:
('L',) ('L',) ('L',)
In the code above we have separated the image as red, green, blue. However, Although their bands are the same they are not the same as each other so that they have different colour scale. Try using show() method to see difference. :)
IMAGE ENHANCEMENTS
Sometimes we may want to change some properties of the image while working on. So, ImageEnhance from PIL is the better way to adjusting some properties like contrast, color, brightness, and sharpnes. To use this method we should import before. The code following demonstrates how to use this method.
from PIL import ImageEnhance
image = Image.open("tux.jpg")
enhancer = ImageEnhance.Sharpness(image)
enhancer.enhance(50.0).show()
You will see the output below.
Let's adjust the contrast of image now.
enhancer = ImageEnhance.Contrast(image)
enhancer.enhance(2).show()
Output:
What If we change the brightness ? 🤔
brightness = ImageEnhance.Brightness(image)
brightness.enhance(3.0).show()
IMAGE FILTERING
Another super cool thing we can do with Pillow is filtering images. There are several filters that pillow provides us like following.
- BLUR
- CONTOUR
- DETAIL
- EDGE_ENHANCE
- EDGE_ENHANCE_MORE
- EMBOSS
- FIND_EDGES
- SHARPEN
- SMOOTH
- SMOOTH_MORE
Once we import the ImageFilter module from PIL we can do something on the image like following.
image = Image.open("tux.jpg")
cont_image = image.filter(ImageFilter.CONTOUR)
cont_image.show()
In the code above we have added the first filter to the image that is CONTOUR. We probably get this output while we run out he code:
Let's try one more of filter details. For instance let us take BLUR at this time and look at what does it look like.
fedge_image = image.filter(ImageFilter.FIND_EDGES)
fedge_image.show()
Top comments (0)