Read and Write operations on Images using OpenCV Python

Hello all,

This is the tenth article in the series Python for Data Science. If you are new to this series, we would recommend you to read our previous articles

  1. Python for Data Science Series - Part 1
  2. Python for Data Science Series - Part 2
  3. Using Numpy in Python
  4. Using Pandas in Python
  5. Data Visualization using Matplotlib Python
  6. Parsing XML file in python
  7. Parsing Json file in python
  8. Interactive Data Visualization using Plotly in Python
  9. Logging in Python

To Install OpenCV Library in your python environment by using the following command

pip install opencv-contrib-python

Please refer the videos below for detailed explanation on how to use opencv to Read and Write Images in Python


Please refer the following code to understand on how to read and write image files in python






In [1]:
import cv2
import os

main_folder_path = r"E:\openknowledgeshare.blogspot.com\Python\Data"

image_file_path = os.path.join(main_folder_path,"IMG_20180116_130048.jpg")

my_image = cv2.imread(image_file_path)
In [3]:
type(my_image)
Out[3]:
numpy.ndarray
In [4]:
my_image.shape
Out[4]:
(3456, 4608, 3)
In [5]:
import matplotlib.pyplot as plt
In [6]:
plt.imshow(my_image)
Out[6]:
<matplotlib.image.AxesImage at 0x21de94e8fd0>
In [8]:
import numpy as np
Out[8]:
255
In [9]:
np.max(my_image)
Out[9]:
255
In [10]:
np.min(my_image)
Out[10]:
0
In [14]:
my_image[0:400,0:1000,:] = 0
In [15]:
plt.imshow(my_image)
Out[15]:
<matplotlib.image.AxesImage at 0x21de943c320>
In [17]:
output_image_file_path = os.path.join(main_folder_path,"IMG_20180116_130048_modified.jpg")
cv2.imwrite(output_image_file_path,my_image)
Out[17]:
True
In [ ]:
 

Comments

Popular posts from this blog

How to run Jupyter Notebooks in Cloud

GUI Programming using Tkinter Python

How to download , install and configure Anaconda - Python