How-To: OpenCV Load an Image

OpenCV Load Image

A few months ago, I was teaching an online seminar on the basics of computer vision.

And do you know what the most common question I got asked was?

How do I use OpenCV to load an image and display it on my screen?

It’s a pretty basic concept, but I think many instructors (myself included) quickly jump over this question and immediately dive into more advanced techniques such as blurring, edge detection, and thresholding.

Displaying an image to your screen is a simple way to debug a computer vision program, so let’s take a couple minutes and answer the this question.

How-To: OpenCV Load an Image

The objective of this post is to show you how to read an image off of disk using OpenCV, display it on your screen, and then wait for a key press to close the window and terminate the script.

While simply displaying an image on your screen isn’t practical by itself, it’s an important technique that you will use a lot when you’re developing (and more importantly, debugging) your own computer vision applications.

You see, displaying an image on your screen is much like a print statement when you’re debugging a tricky program.

When it comes to debugging, nothing beats a few well placed print statements to figure out where the problem is coming from.

The same is true in computer vision.

A few well placed calls to cv2.imshow will quickly help you resolve the problem.

So let’s go ahead and jump into some code:

Lines 1-2 handle importing the packages that we’ll need — argparse to parse command line arguments and cv2 for our OpenCV bindings.

Then, on Lines 4-6 we parse our command line arguments. We only need a single switch, --image, which is the path to where our image resides on disk.

Loading the image using OpenCV is taken care on Line 8 by making a call to the cv2.imread function. This function takes a single parameter — the path to where the image resides on disk, which is supplied as a command line argument.

Finally, we can display our image to our screen on Lines 10-11.

Displaying the image to our screen is handled by the cv2.imshow function. The first argument to cv2.imshow is a string containing the name of our window. This text will appear in the titlebar of the window. The second argument is the image that we loaded off of disk on Line 8.

After we have made a call to the cv2.imshow function, we then need to wait for a key press using the cv2.waitKey function on Line 11.

It’s very important that we make a call to this function, otherwise our window will close automatically!

See, the cv2.waitKey function pauses execution of our Python script and waits for a key press. If we removed Line 11, then the window containing our image would close automatically. By making a call to cv2.waitKey, we are able to pause the execution of our script, thus displaying our image on our screen, until we press any key on our keyboard.

The only argument cv2.waitKey takes is an integer, which is a delay in milliseconds. If this value is positive, then after the specified number of milliseconds elapses the window will close automatically. If the number of milliseconds is zero, then the function will wait infinitely until a key is pressed.

The return value of cv2.waitKey is either the code of the pressed key, or -1, indicating that no key was pressed prior to the supplied amount of milliseconds elapsing.

We can execute our script by issuing the following command:

You should then see an image on your screen:

Figure 1: Loading an image using OpenCV and displaying it to our screen.

Figure 1: Loading an image using OpenCV and displaying it to our screen.

Clearly a wild Doge has appeared! And I’m all out of Pokeballs…

Pressing any key on your keyboard will un-pause the script and close the window.


In this blog post I answered one of the most common questions I get asked: “How do I use OpenCV to load an image and display it on my screen?”

In order to load an image off of disk and display it using OpenCV, you first need to call the cv2.imread function, passing in the path to your image as the sole argument.

Then, a call to cv2.imshow will display your image on your screen.

But be sure to then use cv2.waitKey to wait for a key press, otherwise the window created by cv2.imshow will close automatically.

Learn the Basics of Computer Vision in a Single Weekend

Practical Python and OpenCV

If you’re interested in learning the basics of computer vision, but don’t know where to start, you should definitely check out my new eBook, Practical Python and OpenCV.

In this book I cover the basics of computer vision and image processing…and I can teach you in a single weekend!

I know, it sounds too good to be true.

But I promise you, this book is your guaranteed quick-start guide to learning the fundamentals of computer vision. After reading this book you will be well on your way to becoming an OpenCV guru!

So if you’re looking to learn the basics of OpenCV, definitely check out my book. You won’t be disappointed.


If you would like to download the code and images used in this post, please enter your email address in the form below. Not only will you get a .zip of the code, I’ll also send you a FREE 11-page Resource Guide on Computer Vision and Image Search Engines, including exclusive techniques that I don’t post on this blog! Sound good? If so, enter your email address and I’ll send you the code immediately!

, , ,

13 Responses to How-To: OpenCV Load an Image

  1. Sebastian October 1, 2015 at 9:26 pm #

    Hi Adrian, i’m trying to make the code work, but i can’t.

    Can you give me an example of how to put the path directory for a Raspberry Pi 2 on this line:


    • Adrian Rosebrock October 2, 2015 at 7:08 am #

      Please see the example at the bottom of this post:

      $ python --image doge.jpg

      The path to the image is passed via command line at execution, you do not need to modify the original code.

      • Sadecutz December 11, 2015 at 6:30 am #

        Hello sir. Can you please give me an example of a path u used in –image in this line of code:

        ap.add_argument(“-i”, “–image”, required = True, help = “Path to the image”)

        • Adrian Rosebrock December 11, 2015 at 6:37 am #

          You do not need to modify the argument parsing code. It just indicates that the args["image"] variable will be supplied via command line argument. You can see an example running the Python script via command line this post:

          $ python --image doge.jpg

          Notice how the --image switch points to the path to the doge.jpg image.

          If you’re unfamiliar with command line arguments, I suggest giving this tutorial a read.

  2. Mathys March 17, 2016 at 3:35 pm #

    Hi there

    For the life of me I cant get a image to load with argparse.
    I have tried running it from the command line etc etc

    Im using windows if that makes a difference.
    I dont know where to put my path to the image?


    • Adrian Rosebrock March 18, 2016 at 9:13 am #

      You specify the path to the image via command line argument. It’s been a long time since I’ve used Windows, but I believe it uses the “\” path separator instead of “/”, so your command should look something like:

      $ python --image path\to\your\image.jpg

  3. Mickey May 18, 2016 at 11:09 am #

    Hi Adrian,

    Love you blog. Are there any minor changes I can make to this code that would allow me to upload multiple images? Thank you!

    • Adrian Rosebrock May 19, 2016 at 6:05 pm #

      What do you mean “upload” images? Do you mean load images from disk? Or upload to a server?

      • Mickey May 28, 2016 at 3:25 pm #

        Hi Adrian,

        I meant to ask if there was a way to upload an entire folder of images for processing, without having to write in each image.

  4. Dave May 30, 2016 at 7:23 am #

    Hi, imread keeps returning “none”, even when I hardcode the file location as the argument for cv2.imread

    • Adrian Rosebrock May 31, 2016 at 4:03 pm #

      If you’re getting a NoneType from cv2.imread, then one of two things are happening: (1) The path to cv2.imread is (still) incorrect or (2) OpenCV cannot read the type of image you’re supplying to it.

  5. Coach August 4, 2016 at 3:17 pm #


    Love your blog. I just started you tutorails today, I seem to be opening an image just fine, but it appears I missed how to display images when I’m ssh’d into my Pi.

    I get the following error: (image:30709) Gtk-WARNING **: cannot open display:

    Please point me to instructions on how to open a display via ssh.


    • Adrian Rosebrock August 7, 2016 at 8:23 am #

      If you’re using SSH to access your Pi, make sure you enable X11 forwarding so that the frames can be displayed:

      $ ssh -X pi@your_ip_address

Leave a Reply