DEV Community

Cover image for Image Processing in Python / Stage 2
Enes Karataş
Enes Karataş

Posted on • Originally published at dev.to

Image Processing in Python / Stage 2

❗❗ 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()
Enter fullscreen mode Exit fullscreen mode

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:

cropped.jpg

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()
Enter fullscreen mode Exit fullscreen mode

You will get this output after run the code above.

rotated.jpg

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()
Enter fullscreen mode Exit fullscreen mode

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:

image_1.jpg

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()
Enter fullscreen mode Exit fullscreen mode

The code above changes the modes of image by 'L'. If we show the image the output will be greyscale.

Output:
greyscale.jpg

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)
Enter fullscreen mode Exit fullscreen mode

Output:

   'L'
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

Output:

   ('L',) ('L',) ('L',)
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

You will see the output below.

Output:
enhance.jpg

Let's adjust the contrast of image now.

   enhancer = ImageEnhance.Contrast(image)
   enhancer.enhance(2).show()
Enter fullscreen mode Exit fullscreen mode

Output:

contrast.jpg

What If we change the brightness ? 🤔

   brightness = ImageEnhance.Brightness(image)
   brightness.enhance(3.0).show()
Enter fullscreen mode Exit fullscreen mode

Output:
brightness.jpg

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
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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:

cntr.jpg

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()
Enter fullscreen mode Exit fullscreen mode

Output:
fedge.jpg

Top comments (0)