Image Processing in Python

image processing in python (2 Part Series)

1 Image Processing in Python
2 Image Processing in Python / Stage 2

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
   pip install Pillow
pip install Pillow

Enter fullscreen mode Exit fullscreen mode

or

python -m pip install Pillow
   python -m pip install Pillow
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
   pip3 install Pillow
pip3 install Pillow

Enter fullscreen mode Exit fullscreen mode

or

python3 -m pip3 install Pillow
   python3 -m pip3 install Pillow
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.

<span>from</span> <span>PIL</span> <span>import</span> <span>Image</span>
   <span>from</span> <span>PIL</span> <span>import</span> <span>Image</span>
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].

<span>image</span> <span>=</span> <span>Image</span><span>.</span><span>open</span><span>(</span><span>"</span><span>tux.jpg</span><span>"</span><span>)</span>
   <span>image</span> <span>=</span> <span>Image</span><span>.</span><span>open</span><span>(</span><span>"</span><span>tux.jpg</span><span>"</span><span>)</span>
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.

<span>image_bands</span> <span>=</span> <span>image</span><span>.</span><span>getbands</span><span>()</span>
<span>print</span><span>(</span><span>image_bands</span><span>)</span>
   <span>image_bands</span> <span>=</span> <span>image</span><span>.</span><span>getbands</span><span>()</span>
   <span>print</span><span>(</span><span>image_bands</span><span>)</span>
image_bands = image.getbands() print(image_bands)

Enter fullscreen mode Exit fullscreen mode

You probably will get output like this:

('R', 'G', 'B')
   ('R', 'G', 'B')
('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.

<span>image_mode</span> <span>=</span> <span>image</span><span>.</span><span>mode</span>
<span>print</span><span>(</span><span>image_mode</span><span>)</span>
   <span>image_mode</span> <span>=</span> <span>image</span><span>.</span><span>mode</span>
   <span>print</span><span>(</span><span>image_mode</span><span>)</span>
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.

<span>image_size</span> <span>=</span> <span>image</span><span>.</span><span>size</span>
<span>print</span><span>(</span><span>image_size</span><span>)</span>
   <span>image_size</span> <span>=</span> <span>image</span><span>.</span><span>size</span>
   <span>print</span><span>(</span><span>image_size</span><span>)</span>
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)
   Output:
   (1000, 1000)
Output: (1000, 1000)

Enter fullscreen mode Exit fullscreen mode

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

<span>resized_image</span> <span>=</span> <span>image</span><span>.</span><span>resize</span><span>((</span><span>400</span><span>,</span> <span>400</span><span>))</span>
<span>resized_image</span><span>.</span><span>save</span><span>(</span><span>"</span><span>new_image.jpg</span><span>"</span><span>)</span>
   <span>resized_image</span> <span>=</span> <span>image</span><span>.</span><span>resize</span><span>((</span><span>400</span><span>,</span> <span>400</span><span>))</span>
   <span>resized_image</span><span>.</span><span>save</span><span>(</span><span>"</span><span>new_image.jpg</span><span>"</span><span>)</span>
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.

image processing in python (2 Part Series)

1 Image Processing in Python
2 Image Processing in Python / Stage 2

原文链接:Image Processing in Python

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
Little compliments mean so much to me sometimes.
有时候,一点微不足道的肯定,对我却意义非凡
评论 抢沙发

请登录后发表评论

    暂无评论内容