Image Processing in Computer vision: How to Read and Write Images in Python
An image is considered as a matrix of range of values from 0 to 255 (uint8) which are called as pixels. Moreover, if you have an image of high bit depth, more color combinations are available and range of pixel values will vary. For any color image there are three channels, i.e Red, Green, and Blue. A matrix is formed for each channel and these matrix are joined to give the pixel value at each point. The value of pixel describes the brightness intensity at that point. For a black and white image only one channel exists which displays the pixel value. This particular blog is about read and write images in python. For images, our focus would be on matrix while for videos, it would be on frames. In the previous blog, there is a brief explanation of images and its types. If readers have missed it out, please do read it from the below link
Let us see what is to be covered in this article
Reading and Writing images in python
Reading and Writing images in Python
Python holds wide variety of tools and libraries to read and writing images for image processing such as OpenCV, PIL and skimage. Now let us discuss below how an image is read using different libraries of Python.
Library1: OpenCV is a wrapper class for C++ that can be used with Python.
- First of all an image is loaded using imread function.
- Use the function imread()to read an image. Take care you give the path of the image.
As there are different type of images, so we use a second argument which is a flag that specifies the way image should be read.
- cv2.IMREAD_COLOR: Loads a color image.
- cv2.IMREAD_GRAYSCALE: Loads image in grayscale mode.
- cv2.IMREAD_UNCHANGED: Loads image as such including alpha channel.
Before going to the code, please install this library using following command
If you are using virtual environment or need to install and create it, please follow this link
Reading images from the folder using OpenCV
Writing images in the new folder using OpenCV
Let us see the code for writing the images which are read using the OpenCV function cv2.write. Exception handling is also implemented for displaying any text file in the images folder. This is just to show a sample how to use try and except in the code.
PIL (Python Imaging Library): PIL is a library which supports various image operations. It is an open source applicable in different operating systems such as Window, Mac, and Linux. It can be used to pixel manipulation, image enhancement, and brightness adjustment etc. Let us see how to read and write images using PIL.
Before going to the code, let us install it using the below command
Code for read and write image using PIL. You can install and create virtual environment using this link.
Read and Write Images using PIL
In a nutshell, it can be said that there are many libraries to read and write images in python. I have explained only two of them such as OpenCV and PIL. One can go for skimage and other modules also. Concept will nearly remain same. I have also tried to explain the concepts with the help of live codes and comments so that readers can also practice along with reading as practice of coding will make oneself more confident. Codes not only helps you to get lucrative jobs but also enhances your skills. I hope you have gained some knowledge about reading and writing images in python after reading this tutorial. Next tutorial will be very interesting as I am planning to write on playing with videos and frames so please keep in touch with us.