DEV Community

Cover image for Image Processing in Python
Enes Karataş
Enes Karataş

Posted on • Originally published at dev.to

Image Processing in Python

Start Tutorial

📌
In this post I am going to share some information about what image processing is. So, let's begin with the main methodology of image processing.
Firstly, we are going to start with the definition of image processing.

❗❗ Note that this tutorial will be published as a serie hence I recommend you to read the rest of all.

What is image processing ?


Image processing is the manipulation of images in digital environments💻 over some programming languages through some algorithms. We can analyze the images and also transform some properties like colour and dimensions. Apart of that we can categorize the image like RGB and Grayscale image. Also look here

Alright it is time to look at how can we handle that process. At this point we are going to use python. Python is one of the best programming languages to image processing so that we will use some modules of python language.

The list of modules that we will use is;
📋

  • Pillow(PIL)
  • OpenCV
  • NumPy
  • Sckit-Image

Note that some of those modules will be used less than the others.
Let's start the first of list above.

📍PIL(Pillow)

➡ Pillow is one of the most popular module to basic image processing in Python. This module is used for processing the images in its different properties and also is used so commonly by data scientists. The module provides support various image formats and also includes some popular formats like JPEG, PNG.


Alright! We have done so far and let's go to usage of the modules.

To use Pillow we need to install before so, we can do installation via pip:

   pip install Pillow
Enter fullscreen mode Exit fullscreen mode

or

   python -m pip install Pillow
Enter fullscreen mode Exit fullscreen mode

If you have linux system then you can also use those instructions to installation as following;

   pip3 install Pillow
Enter fullscreen mode Exit fullscreen mode

or

   python3 -m pip3 install Pillow
Enter fullscreen mode Exit fullscreen mode

Ready to use now ! Let's go to instances of PIL.

📍Usage and Instances for PIL

➡ Now we have Pillow module that imported to python main. It is time to write instances. In python file we firstly import several modules from PIL that we will use. The first it is going to be Image as well.

   from PIL import Image
Enter fullscreen mode Exit fullscreen mode

Image function is imported from PIL so, we are going to use Image function by its open() module firstly. For this tutorial I have prepared linux tux image as you see below and you also are able to find source [here].

tux.jpg

   image = Image.open("tux.jpg")
Enter fullscreen mode Exit fullscreen mode

Image is loaded now let's look at some properties of image. One of those properties is image band. Every image has one or more bands. To demonstrate bands of the image we use getbands() function as following.

   image_bands = image.getbands()
   print(image_bands)
Enter fullscreen mode Exit fullscreen mode

You probably will get output like this:

   ('R', 'G', 'B')
Enter fullscreen mode Exit fullscreen mode

Another property is mode. Image has mode which defines the type and depth of a pixel in the image. To obtain the mode of image we are going to do as following.

   image_mode = image.mode
   print(image_mode)
Enter fullscreen mode Exit fullscreen mode

And then the output will be that string "RGB" for this image. There are some modes that Pillow provides below:

📋

  • 1
  • L
  • P
  • RGB
  • RGBA
  • CMYK
  • YCbCr
  • HSV
  • I
  • F

The another property is size, it can change the size of image as you see below.

   image_size = image.size
   print(image_size)
Enter fullscreen mode Exit fullscreen mode

We got this output for image that we have used.

   Output:
   (1000, 1000)
Enter fullscreen mode Exit fullscreen mode

Now we can change the size ! We do that using resize function.

   resized_image = image.resize((400, 400))
   resized_image.save("new_image.jpg")
Enter fullscreen mode Exit fullscreen mode

You probably are going to see a new image called like "new_image.jpg" in the same directory with your python code file.

So far so good !

Alright it's time to use of functions that can make more complex process on the images. We are going to use some of them in second section of this serie.

The following series allows you to access the other sections.

Top comments (0)