close

How to read an image in Python OpenCV

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to read an image in Python OpenCV in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to read an image in Python OpenCV?

  1. How to read an image in Python OpenCV?

    To read an image with OpenCV you have to use the following synthax. If it doesn't work, there is a problem with the installation.

  2. read an image in Python OpenCV

    To read an image with OpenCV you have to use the following synthax. If it doesn't work, there is a problem with the installation.

Method 1

If you are trying to display OpenCV image using matplotlib, use the code below.

import numpy as np
import cv2
import matplotlib.pyplot as plt
%matplotlib inline  # if you are running this code in Jupyter notebook

# reads image 'opencv-logo.png' as grayscale
img = cv2.imread('/path_to_image/opencv-logo.png', 0) 
plt.imshow(img, cmap='gray')

Method 2

To read an image with OpenCV you have to use the following synthax. If it doesn’t work, there is a problem with the installation.

import cv2

image = cv2.imread('path_of_the_image.png')

cv2.imshow('img', image)
cv2.waitKey(0)

You didn’t post the error it gives..

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read