Basic Image Manipulations in Python and OpenCV: Resizing (scaling), Rotating, and Cropping

Are you ready to start building your first image search engine? Not so fast! Let’s first go over some basic image processing and manipulations that will come in handy along your image search engine journey. If you are already an image processing guru, this post will seem pretty boring to you, but give it a read none-the-less — you might pick up a trick or two.

Looking for the source code to this post?
Jump right to the downloads section.

OpenCV and Python versions:
This example will run on Python 2.7 and OpenCV 2.4.X/OpenCV 3.0+.

For this introduction to basic image processing, I’m going to assume that you have basic knowledge of how to create and execute Python scripts. I’m also going to assume that you have OpenCV installed. If you need help installing OpenCV, check out the quick start guides on the OpenCV website.

Continuing my obsession with Jurassic Park let’s use the Jurassic Park tour jeep as our example image to play with:

Figure 1: Our example image -- a Jurassic Park Tour Jeep.

Figure 1: Our example image — a Jurassic Park Tour Jeep.

Go ahead and download this image to your computer. You’ll need it to start playing with some of the Python and OpenCV sample code.

Ready? Here we go.

First, let’s load the image and display it on screen:

Executing this Python snippet gives me the following result on my computer:

Figure 2: Loading and Displaying the Jurassic Park tour jeep.

Figure 2: Loading and Displaying the Jurassic Park tour jeep.

As you can see, the image is now displaying. Let’s go ahead and break down the code:

  • Line 2: The first line is just telling the Python interpreter to import the OpenCV package.
  • Line 5: We are now loading the image off of disk. The imread functions returns a NumPy array, representing the image itself.
  • Line 6 and 7: A call to imshow displays the image on our screen. The first parameter is a string, the “name” of our window. The second parameter is a reference to the image we loaded off disk on Line 5. Finally, a call to waitKey pauses the execution of the script until we press a key on our keyboard. Using a parameter of “0” indicates that any keypress will un-pause the execution.

Practical Python and OpenCV

Just loading and displaying an image isn’t very interesting. Let’s resize this image and make it much smaller. We can examine the dimensions of the image by using the shape attribute of the image, since the image is a NumPy array after-all:

When executing this code, we see that (388, 647, 3) is outputted to our terminal. This means that the image has 388 rows, 647 columns, and 3 channels (the RGB components).  When we write matrices, it is common to write them in the form (# of rows x # of columns) — which is the same way you specify the matrix size in NumPy.

However, when working with images this can become a bit confusing since we normally specify images in terms of width x height. Looking at the shape of the matrix, we may think that our image is 388 pixels wide and 647 pixels tall. However, this would be incorrect. Our image is actually 647 pixels wide and 388 pixels tall, implying that the height is the first entry in the shape and the width is the second. This little may be a bit confusing if you’re just getting started with OpenCV and is important to keep in mind.

Since we know that our image is 647 pixels wide, let’s resize it and make it 100 pixels wide:

Executing this code we can now see that the new resized image is only 100 pixels wide:

Figure 3: We have now resized the image to 100px wide.

Figure 3: We have now resized the image to 100px wide.

Let’s breakdown the code and examine it:

  • Line 15 and 16: We have to keep the aspect ratio of the image in mind, which is the proportional relationship of the width and the height of the image. In this case, we are resizing the image to have a 100 pixel width, therefore, we need to calculate r, the ratio of the new width to the old width. Then, we construct the new dimensions of the image by using 100 pixels for the width, and r x the old image height. Doing this allows us to maintain the aspect ratio of the image.
  • Lines 19-21: The actual resizing of the image happens here. The first parameter is the original image that we want to resize and the second argument is the our calculated dimensions of the new image. The third parameter tells us the algorithm to use when resizing. Don’t worry about that for now. Finally, we show the image and wait for a key to be pressed.

Resizing an image wasn’t so bad. Now, let’s pretend that we are the Tyrannosaurus Rex from the Jurassic Park movie — let’s flip this jeep upside down:

So what does the jeep look like now? You guessed it — flipped upside down.

Figure 4: The jeep has now been flipped upside. We could have also rotated the jeep by any arbitrary angle.

Figure 4: The jeep has now been flipped upside. We could have also rotated the jeep by any arbitrary angle.

This is the most involved example that we’ve looked at thus far. Let’s break it down:

  • Line 25: For convenience, we grab the width and height of the image and store them in their respective variables.
  • Line 26: Calculate the center of the image — we simply divide the width and height by 2.
  • Line 29: Compute a matrix that can be used for rotating (and scaling) the image. The first argument is the center of the image that we computed. If you wanted to rotate the image around any arbitrary point, this is where you would supply that point. The second argument is our rotation angle (in degrees). And the third argument is our scaling factor — in this case, 1.0, because we want to maintain the original scale of the image. If we wanted to halve the size of the image, we would use 0.5. Similarly, if we wanted to double the size of the image, we would use 2.0.
  • Line 30: Perform the actual rotation, by suppling the image, the rotation matrix, and the output dimensions.
  • Lines 31-32: Show the rotated image.

Rotating an image is definitely the most complicated image processing technique we’ve done thus far.

Let’s move on to cropping the image and grab a close-up of Grant:

Take a look at Grant. Does he look like he sees a sick Triceratops?

Figure 5: Cropping is simple with Python and OpenCV -- we're just slicing NumPy arrays!

Figure 5: Cropping is simple with Python and OpenCV — we’re just slicing NumPy arrays!

Cropping is dead as Dennis Nedry in Python and OpenCV. All we are doing is slicing arrays. We first supply the startY and endY coordinates, followed by the startX and endX coordinates to the slice. That’s it. We’ve cropped the image!

As a final example, let’s save the cropped image to disk, only in PNG format (the original was a JPG):

All we are doing here is providing the path to the file (the first argument) and then the image we want to save (the second argument). It’s that simple.

Figure 6: We have now saved 'thumbnail.png' to disk.

Figure 6: We have now saved ‘thumbnail.png’ to disk.

As you can see, OpenCV takes care of changing formats for us.

And there you have it! Basic image manipulations in Python and OpenCV! Go ahead and play around with the code yourself and try it out on your favorite Jurassic Park images.

If you have any questions, either leave them in the comments or send me an email — I will be happy to help out.


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 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL! Sound good? If so, enter your email address and I’ll send you the code immediately!

, , , , ,

91 Responses to Basic Image Manipulations in Python and OpenCV: Resizing (scaling), Rotating, and Cropping

  1. Rish_S November 13, 2014 at 5:39 pm #

    I believe numpy.shape returns you number of rows and then number of columns and not the other way round (which is consistent with how we write matrices). Otherwise, 647 would denote the height, not the width.

    • Adrian Rosebrock November 14, 2014 at 6:42 am #

      Hi Rich, thanks for the comment. I was just trying to make the point that we normally think of images as width x height. But if you look at the NumPy shape, you’re actually seeing the height first, followed by the width. It is consistent with writing matrices and for numerical processing, but can be a bit confusing if you’re first starting to use OpenCV.

  2. John Harris November 28, 2014 at 2:35 am #

    Question, where can I find pyimagesearch with imutils

    • Adrian Rosebrock November 28, 2014 at 8:19 am #

      Hi John, the imutils file is included in the source code for this blog post. I’ve been meaning to put up on GitHub, I’ll make sure that it is a priority.

  3. MY0613 December 17, 2014 at 12:50 am #

    you can remove “import imutils”, and use the following code to replace “cv2.imshow(“Puzzle”, imutils.resize(puzzle, height = 650))”.

    r = 650.0 / puzzle.shape[0]
    dim = (int(puzzle.shape[1] * r), 650)

    # perform the actual resizing of the image and show it
    resized = cv2.resize(puzzle, dim, interpolation = cv2.INTER_AREA)

    # display the images
    cv2.imshow(“Puzzle”, resized)

  4. Jay December 17, 2014 at 3:35 am #

    Hi this is very nice and great article . I was looking to resize by providing height and width . Can you tell me how to do in your code

    • Adrian Rosebrock December 17, 2014 at 7:59 am #

      Hi Jay, if you want to provide both the width and the height, just change the dim tuple. For example, let’s say I wanted to hardcode that an image should be resized to 32×32 pixels, ignoring aspect ratio. Then, I would change my code to be: resized = cv2.resize(puzzle, (32, 32), interpolation = cv2.INTER_AREA)

      • Vaibhav July 3, 2017 at 10:46 am #

        What if I do not want to ignore aspect ratio, I would like to resize an image with 1000×800 pixels without ignoring aspect ratio.
        Thanks Adrian for your posts, really helpful.

        • Adrian Rosebrock July 5, 2017 at 6:10 am #

          Simply use my imutils.resize function inside the imutils library. This function will preserve aspect ratio.

  5. Dave January 25, 2015 at 8:51 pm #

    Interesting page. I ve just signed up for your crash course.
    Quick question though.
    What if I want to crop an image not based on a rectangle but on a -say- circle?? How can this be done with opencv?

    Your reply will be highly appreciated

    • Adrian Rosebrock January 26, 2015 at 6:46 am #

      Hi Dave, there are two ways to crop an image based on a circle. The first method would be to take the area of the circle and convert it to a rectangle (based on the radius) and crop the image that way. However, a better approach would be to take your circle and apply a bitwise ‘and’ using the cv2.bitwise_and function. Check out this post and see how the cv2.bitwise_and function is used to crop arbitrary shaped regions from the image.

  6. shaikh ibrahim April 18, 2015 at 12:54 pm #


    Can you please tell me how to cut image in four quadrant in opencv in visual studio 2010.

    I need generalised method to cut the image, that means it should calculate the starting point and ending point of the image and then perform the cutting operation.

    • Adrian Rosebrock April 18, 2015 at 1:28 pm #

      Hey Shaikh, this is actually a Python + OpenCV blog, I don’t do any C++ coding. But in general, all you need to do is compute the center of the image and then use that to extract the four quadrants.

  7. Py September 11, 2015 at 8:02 am #

    In the image.shape part, you wrote that the image has 388 columns, 647 rows, but it looks like the image has 388 rows and 647 columns. So does image.shape() give the output as [rows, cols, channels] ?

    • Adrian Rosebrock September 12, 2015 at 6:51 am #

      Thanks for the heads up! I have corrected this issue.

  8. Timon September 16, 2015 at 2:07 am #

    Hello, thanks a lot! You make opencv so easy and I have enrolled for the crash course. I believe it will really be of benefit to me.
    Meanwhile, I have a question, I have a 12 MB image that I want to reduce size, not making it smaller in terms of height and width but rather in terms of disk space. How do I do that with opencv?
    Thank you again

    • Adrian Rosebrock September 16, 2015 at 7:23 am #

      That all depends on the format of your image. Files such as BMP are lossless but don’t apply any compression, making them very large. PNG images are also lossless, but apply compression, making them a good choice between file size and image quality. In your case, you might want to convert your image to a JPEG file.

  9. Keith Prisbrey September 29, 2015 at 9:22 pm #

    Thank you, thank you. I’m deep into your premium bundle, your crash course, and greatly appreciate this blog. I’m segmenting overlapping characters from ancient handwritten Chinese family records (jiapu) as a volunteer at a public genealogy library. The library has millions of camera images that need to be computer indexed through OCR for online computer searching for family names and histories. I have tried 2d fft to extract features with limited success, and will next try HOG. I have also been reading about Haar cascades.

  10. akif December 8, 2015 at 4:53 am #

    Your cropped image is in rectangular form.How do you cropp a circular area in image?

    • Adrian Rosebrock December 8, 2015 at 6:26 am #

      You can’t really “crop” a circular region from an image. A bounding box, by definition, is rectangular. If you would like to extract a circular region, you’ll need to first extract the bounding box, then use a circular mask. But again, the image will still appear to be rectangular.

      • akif December 8, 2015 at 10:35 am #

        So can we use mask instead in order to see only circular area?How?

        • Adrian Rosebrock December 8, 2015 at 2:39 pm #

          Hey Akif — you can take a look at this post for an example of masking. You can do the same thing using the function. Otherwise, you should take a look at Practical Python and OpenCV where I discover masking in more detail (including how to extract a circular area mask).

  11. Haobai December 30, 2015 at 7:20 pm #

    Hello, Love the website! I like to compare various sources and have found your to be one of the easiest reads and is always accurate. Thanks for the resource.

    On the rotate, I think that you took the easy manipulation, 180 degrees. I needed 90 and was surprised that cv2.imshow() doesn’t recognize the rotation in it’s dimensions. One change was forced by transposing the width and height in the cv2.warpAffine(). Yet, the image was offset by half width and half height. Does this need to be re-manipulated in the cv2.imshow()?

    Also, if you let the rotation handle the resize, you end up with another scenario entirely.

    I am just learning openCV and python and understand the conveniences of the resources. So, if this is just part of the system, I’ll accept that. But, how would you handle this?

    • Adrian Rosebrock December 31, 2015 at 7:03 am #

      Thanks for the kind words. And regarding the cv2.imshow and cv2.warpAffine method, I think there is a bit of confusion. The cv2.imshow doesn’t care how the image is rotated, how it’s distorted or what it’s contents are — it simply takes the contents of the NumPy array and displays it to your screen.

      In the case that you need to rotate an image and still have all parts of it fit into the size of the allocated NumPy array (sort of like allocating an array in C/C++), then you’ll need to handle this yourself. If you need to rotate 90 degrees (or any other degree), simply specify the size of the image after the rotation to the cv2.warpAffine method and it will work. See this StackOverflow question for more information.

      • Sahil December 13, 2017 at 3:49 pm #

        How about img = scipy.ndimage.rotate() ?

        • Adrian Rosebrock December 15, 2017 at 8:32 am #

          Following up on this, I actually wrote a blog post that discusses how to keep the entire image in view after rotation.

  12. Aris January 11, 2016 at 3:50 am #

    Thanks for your information on image processing, really helpful.

    For my project I want to crop and scale video, is it smart to do it the same as mentioned above frame by frame? I want to reduce the video to run other processes (for which I’m using the video) much faster

    • Adrian Rosebrock January 11, 2016 at 6:34 am #

      If you camera sensor allows you to reduce the resolution during capture, that will certainly speedup your pipeline. Otherwise, yes, you’ll need to resize frame by frame.

  13. Navdeep January 20, 2016 at 3:36 pm #

    which method could be used to zoom image on a particular pixel coordinate or
    zoom on a specific point in image.
    please help

    • Adrian Rosebrock January 21, 2016 at 5:04 pm #

      For this type of application, I wouldn’t suggest OpenCV. If you’re trying to develop a GUI to zoom in on a particular point, OpenCV is overkill — and the GUI functionality isn’t the best. You would need to create your own custom mouse listeners to detect a click, determine the ROI, and then zoom in on it. Realistically, this would be a lot of work to build with OpenCV, and again, not something I recommend.

  14. Sumit February 23, 2016 at 5:58 am #

    how to find the startY, endY and startX, endX during cropping task when there is no reference scale available? thanking you in advance.

    • Adrian Rosebrock February 23, 2016 at 3:23 pm #

      Hey Sumit — can you elaborate more on what you’re trying to do? If you don’t know the starting and ending coordinates, you need to “find” them in your image. There are lots of algorithms to do this, but it really depends on what you’re trying to accomplish.

  15. Lakshmi March 1, 2016 at 6:13 am #

    how to crop an irregular shape from an image ?

    • Adrian Rosebrock March 1, 2016 at 3:37 pm #

      You can’t “crop” a non-rectangular shape from an image. All cropping ROIs must be specified in terms of (x, y)-coordinates. That said, you can use masks to define irregular shapes of an image — but again, by definition of an image, all croppings are rectangular.

  16. borse vivek September 4, 2016 at 11:54 am #

    how can we crop 2 rectangles from same image ?
    in case of detection if two faces are present in one image then how can we crop each face at one time

    • Adrian Rosebrock September 5, 2016 at 8:01 am #

      I’m not sure what you mean “crop each face at one time”, but we normally crop a bounding box for each of the faces in the images. That means if you detect 2 faces in an image, you end up with 2 bounding boxes, and therefore 2 cropped regions. Is that what you are trying to do? Otherwise, I don’t understand your question.

  17. Danlan October 17, 2016 at 10:04 pm #

    what is the coordinate of an image,

    • Adrian Rosebrock October 20, 2016 at 9:02 am #

      The coordinate of an image? Can you please elaborate?

  18. Ryan November 22, 2016 at 1:35 pm #

    Hi Adrian,

    LOVE the website! Instructions are very clear, thanks for spending the time and effort to help people.

    But I had a problem when following the steps, I used enthought canopy with opencv packages (2.4.9-4) and I stumble with issue that the python always crash and the window freeze (deadly spinning beach ball) when I use the imshow() method.
    Here is my simple code:

    import cv2
    image = cv2.imread(“test.png”)
    cv2.imshow(“original”, image)

    Is there anything I did wrong?

    • Adrian Rosebrock November 22, 2016 at 3:44 pm #

      Hey Ryan — I haven’t used Enthought Canopy before so I can’t comment on how they have setup their OpenCV distributions. I would instead recommend that you follow my tutorials and install OpenCV from source.

  19. Ghanshyam Savaliya December 26, 2016 at 11:48 pm #

    Hello Adrian,
    I am not able to understand following line .Will you please explain it.
    (h, w) = image.shape[:2]
    how slicing notation is used with shape??

    • Adrian Rosebrock December 31, 2016 at 1:39 pm #

      An image in OpenCV + Python is simply a NumPy array. Each NumPy array has a .shape. For multi-channel images the shape is a tuple of (height, width, depth). Since we don’t need the depth we use slicing to only grab the height and width.

  20. yaswanth kumar January 3, 2017 at 5:16 am #

    Hi Adrian, Thanks for the post.
    Is numpy.resize same as cv2.resize

  21. Venky January 4, 2017 at 8:54 am #

    How to distort a proper prospective image with four corner coordinates named as A,B,C,D into given output coordinates?

    I need to obtain a distorted, unprospective and 90 degree rotated image.
    I need to write code in Python 2.7 using OpenCV 3.2

    I’d be glad if you could help. Thanks in advance!

    • Adrian Rosebrock January 4, 2017 at 10:39 am #

      It sounds like you need to obtain a top-down, birds-eye view of object? If so, please see this blog post as good example.

  22. Peter April 12, 2017 at 3:29 am #

    Hey I know this sounds like a silly question but I’ve been wondering for sometime now, how do people go on about in getting the exact coordinates of the part in which they would like to crop?

    cropped = image[70:170, 440:540]

    How do you get the start y, end y and same goes for the x coordinates.

    I know this sounds silly but I would really like to know. Thank you!

    • Adrian Rosebrock April 12, 2017 at 1:01 pm #

      It depends on your actual application. In most cases, you can treat this as object detection such as color thresholding or HOG + Linear SVM. There are a number of techniques you can apply to obtain the (x, y)-coordinates of the bounding box, but again, this is highly dependent on what you are trying to build. I discuss these different types of method inside Practical Python and OpenCV and inside the PyImageSearch Gurus course.

  23. Alexa May 15, 2017 at 10:36 pm #

    First time following the code and having it work actually!
    Thank you:)

    • Adrian Rosebrock May 17, 2017 at 10:03 am #

      Glad to hear it Alexa, congrats!

  24. Navdesh May 30, 2017 at 2:00 am #

    hey Adrian,
    your blog is too much helpful. Thanks for providing the help.
    can you please provide the same operations on video? how to process with videos like separating the frames and background subtraction etc?

  25. falah obaid June 6, 2017 at 9:49 am #

    Hi Adrian
    how can shrink video sequnce. I wnat to reduce the length of clip video theor length ranges (12-34) frames to became 7 frames

    • Adrian Rosebrock June 6, 2017 at 11:52 am #

      I would suggest reading up on how to write videos to file with OpenCV. Basically, you will want to skip every N-th frame in your input video and write it to a separate output video.

  26. Vaibhav July 3, 2017 at 3:26 pm #

    Hi Adrian,

    1. I have one source/original scanned image file,
    2. Also there are scanned copies for same template.
    3. but other scanned copies(point:2) are of different size and skewed, I need to deskew and resize same.
    4. I am able to deskew it using your one of post,
    my query is how to resize scanned copies in to original scan copy size.

    These are required for OMR. I would appreciate your suggestion, Thanks

  27. varun Risbud August 22, 2017 at 1:15 am #

    hello sir , it was a beautiful article . I have one question . my problem statement is , if I have to make a paper checker (multiple choice question checker) . So i l be giving an input , it will be captured by my camera .Suppose I have one paper to be corrected, How should I crop that paper so that when get the image that will be only paper , no background? You know in camscanner application how they do it , something like that

  28. Anushka September 2, 2017 at 1:27 pm #


    On resizing an image, are we changing the number of pixels in the image?
    If so, can we find the size of one pixel?

    • Adrian Rosebrock September 5, 2017 at 9:33 am #

      Hi Anushka — resizing an image by definition changes the number of pixels in an image. If you need to determine a real-world metric (i.e., inches, millimeters, etc.) of a given object, I would consult this blog post. If you’re just getting started learning computer vision and image processing I would definitely encourage you to work through Practical Python and OpenCV. This book will help you get up to speed quickly.

  29. Ambika September 21, 2017 at 2:41 am #

    Hi Adrian!

    I wanted to ask if we can crop some part out of the image keeping the rest of the image intact. It would be the reverse of the cropping that you have done above. By reverse, I mean to say that in your case you are discarding the whole image and keeping just the cropped part. But my question is that can we discard the cropped part out of the image and keep the rest of the image in which the cropped part would be blacked out?

    • Adrian Rosebrock September 23, 2017 at 10:16 am #

      Yes. This is called “masking”. You would typically use the cv2.bitwise_and function for this. I cover the basics of masking and how to accomplish what you are asking inside the first few chapters of Practical Python and OpenCV (take a look at the section on “masking”).

  30. sinthea October 31, 2017 at 12:38 pm #

    where do I find pyimagesearch module? it shows me an error.

    from pyimagesearch import imutils

    ModuleNotFoundError Traceback (most recent call last)
    in ()
    —-> 1 from pyimagesearch import imutils

    • Adrian Rosebrock November 2, 2017 at 2:45 pm #

      Please use the “Downloads” section of this blog post to download the source code, pyimagesearch module, etc.

  31. KRK December 18, 2017 at 12:40 pm #

    hello, what if I need to capture video live from 2 different cameras at same time and detect emotions ……suggestions welcome…

  32. otis December 28, 2017 at 9:12 pm #

    Hi Adrian,
    Do you ship your book to Malaysia? Any additional shipping charges?

    • Adrian Rosebrock December 31, 2017 at 9:56 am #

      Yes, I can ship to Malaysia. No, there are no additional shipping charges.

  33. nidhi February 7, 2018 at 12:08 am #

    hiii adrain,

    I am using haar cascade to detect an object, and it works pretty well. But the problem is i want it to detect only a particular size (eg. width : 360 and height :250 ) and size greater than this and ignore the smaller ones.

    • Adrian Rosebrock February 8, 2018 at 8:37 am #

      When you loop over the bounding boxes check their width and height. If the width is less than 360 and the height less than 250, use an “if” statement to catch this and ignore the bounding box.

  34. Amyraah February 25, 2018 at 10:04 pm #

    Hi Adrian,

    I plan to have a training sets of several features from 100 images.

    For example, in a car image, i want to crop the tires, door, and mirror.
    And tree, i want to crop the image of trunk, leaves and roots for example.

    Above training sets are to be kept in separate folders of tires, doors, leaves, and etc.

    Do you have any suggestion for me to do so?

    • Adrian Rosebrock February 26, 2018 at 1:51 pm #

      I personally like to organize in separate folders as you have done. Take a look at this blog post to see how I prefer to structure my datasets on disk.

      • Amyraah February 26, 2018 at 8:49 pm #

        Thanks Adrian for your help!

        Rlly appreciate it! 🙂

      • Freddy August 5, 2019 at 9:49 am #

        Hello Adrian,

        I’m trying to crop a region from all images of folder, and want to save the cropped regions into folder B. How would approach to do that? This is for training a Haarcascade for detecting an object (electrical panel turning switch).

  35. rosa March 21, 2018 at 8:08 am #

    Dear Adrian,

    thank you very much for your post! It is the very easily explained and the only one very clear on this argument.

    I have some x, y coordinate of a pixel in an image and the diameter of a circle (this is actually and keypoints.size). How can I crop a square around the circle centre?

    It maybe sounds a trivial question but thanks you for your help!



    • Adrian Rosebrock March 22, 2018 at 10:01 am #

      I would suggest you compute the bounding box of the keypoint by taking into account the keypoint coordinates and radius. From there you can apply simple array slicing to extract the region. If you are having trouble with the math, see this thread.

  36. Kent July 1, 2018 at 2:23 am #

    Hi Adrian,

    I’m just starting to explore openCV with Python, and it looks like your site is one of the more organized resources available. Thank you for creating it.

    I think there is a very minor typo on this page. In the Loading and Displaying an Image snippet section where you say “Line 1: The first line is just telling the Python interpreter to import the OpenCV package.” I believe you mean Line 2, not Line 1. Line 1 contains a comment, not the import statement.

    • Adrian Rosebrock July 3, 2018 at 8:36 am #

      Thanks Kent, I appreciate it. Typo fixed.

  37. Aravind February 23, 2019 at 9:30 am #

    This is very helpful, but i also like to know how can we use cv2.selectroi, for cropping during runtime?

  38. Amir February 28, 2019 at 2:15 am #

    great tutorial to start opencv image processing, i m loving so much!! thanks adrian:)

    • Adrian Rosebrock February 28, 2019 at 1:41 pm #

      Thanks Amir!

  39. WESLEY S March 4, 2019 at 2:06 am #

    hi…how to crop a live video using opencv python. the crop window has to show a live feed

    • Adrian Rosebrock March 5, 2019 at 8:48 am #

      You can use basic NumPy array slicing to crop a region from an image/video with OpenCV and Python. I cover that topic in Practical Python and OpenCV.

  40. Alabi December 30, 2019 at 9:49 am #

    I’m working on a face recognition stuff and would like to know if changing pixel resolution (either row-wise alone or column-wise alone) can affects the performance..
    i mean whether the execution time or recognition accuracy can be somehow affected.

    • Adrian Rosebrock January 2, 2020 at 8:53 am #

      Hey Alabi — you can easily test that yourself by using Python’s “time.time()” function to measure how long a function call takes. You can also measure the accuracy as well. You have your hypothesis/idea, now it’s time for you to implement it and test it.


  1. Python and OpenCV: Finding OpenCV contours with cv2.findContours - April 28, 2014

    […] you’re wondering about imutils on Line 2, refer back to my post on Basic Image Manipulations in Python and OpenCV, where I go over resizing, rotating, and translating. The file in the pyimagesearch […]

  2. Playing card detection using OpenCV (Mark II) | R D Milligan - July 13, 2014

    […] got the image rotating code from Adrian Rosebrock’s Basic Image Manipulations […]

  3. Tutorial: Skin Detection Example using Python and OpenCV - August 18, 2014

    […] functions for resizing, rotating, etc. You can read more about the imutils  package in my Basics of Image Manipulations […]

  4. How to Build a Kick-Ass Mobile Document Scanner in Just 5 Minutes - PyImageSearch - September 2, 2014

    […] We’ll also be using the imutils  module, which contains convenience functions for resizing, rotating, and cropping images. You can read more about imutils  in my basic image manipulations post. […]

Before you leave a comment...

Hey, Adrian here, author of the PyImageSearch blog. I'd love to hear from you, but before you submit a comment, please follow these guidelines:

  1. If you have a question, read the comments first. You should also search this page (i.e., ctrl + f) for keywords related to your question. It's likely that I have already addressed your question in the comments.
  2. If you are copying and pasting code/terminal output, please don't. Reviewing another programmers’ code is a very time consuming and tedious task, and due to the volume of emails and contact requests I receive, I simply cannot do it.
  3. Be respectful of the space. I put a lot of my own personal time into creating these free weekly tutorials. On average, each tutorial takes me 15-20 hours to put together. I love offering these guides to you and I take pride in the content I create. Therefore, I will not approve comments that include large code blocks/terminal output as it destroys the formatting of the page. Kindly be respectful of this space.
  4. Be patient. I receive 200+ comments and emails per day. Due to spam, and my desire to personally answer as many questions as I can, I hand moderate all new comments (typically once per week). I try to answer as many questions as I can, but I'm only one person. Please don't be offended if I cannot get to your question
  5. Do you need priority support? Consider purchasing one of my books and courses. I place customer questions and emails in a separate, special priority queue and answer them first. If you are a customer of mine you will receive a guaranteed response from me. If there's any time left over, I focus on the community at large and attempt to answer as many of those questions as I possibly can.

Thank you for keeping these guidelines in mind before submitting your comment.

Leave a Reply