Image Processing in Python / Stage 2
Tags: programming, ai, python, codenewbie
Published: 2021-09-18 16:10:39

❗❗ 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 !

```python
   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:

![cropped.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k128nvvjxwqcmk0exvkd.jpg)
<br>
### 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.

```python
   image = Image.open("tux.jpg")
   rotated = image.rotate(90)
   rotated.show()
```
You will get this output after run the code above.

![rotated.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0lf1ps515otv582bntb2.jpg)
<br>
### 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 !

```python
   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. 
<br>

After running out the code above we get this output:

Output:

![image_1.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pc36u4eu3tt367j7lxoy.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**.

```python
   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. 

Output:
![greyscale.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7izv91mujy13x88az4n.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*.

```python
   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. 

```python
   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.

```python
   from PIL import ImageEnhance

   image = Image.open("tux.jpg")
   enhancer = ImageEnhance.Sharpness(image)
   enhancer.enhance(50.0).show()
```
You will see the output below.

Output:
![enhance.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88o8vhzni4f8jw8vcp30.jpg)

Let's adjust the contrast of image now.

```python
   enhancer = ImageEnhance.Contrast(image)
   enhancer.enhance(2).show()
```
Output:

![contrast.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgvgjop0mjhr3f1lnu4a.jpg)

What If we change the brightness ? 🤔

```python
   brightness = ImageEnhance.Brightness(image)
   brightness.enhance(3.0).show()
```
Output:
![brightness.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oa2u1llklfuijyw1er2p.jpg)

<br>

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

```python
   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:

![cntr.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0k0muoclzwtj1utppg1l.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. 

```python
   fedge_image = image.filter(ImageFilter.FIND_EDGES)
   fedge_image.show()
```
Output:
![fedge.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2et9mxk0y6yvyzni6uyu.jpg)