5

How can I rotate an image in Python with the help of the OpenCV library, and by changing the value of height and width of the image (without using the built-in methods for rotation in OpenCV). It has to implement with two nested loops.

img=cv2.imread('Images/Screenshot.png',cv2.IMREAD_GRAYSCALE)

height, width = img.shape

# for i in range(0,height):
#     for j in range(0,width):
#         img[i][j]=

# show rotated image
cv2.imshow("image",img)

Thanks for your attention!

5
  • 2
    Can you post some code for what you've tried so far? Oct 2, 2018 at 22:33
  • @G.Anderson I have added some code to the post
    – Hamid
    Oct 2, 2018 at 22:50
  • 1
    And what comes out the other end when you run that code, and how does that compare to what you expect? Not trying to be mean, genuinely curious about what your code is doing. Do you mean to assign img[i][j] to img[j][i] in your loop? Oct 2, 2018 at 23:00
  • 4
    Take a sheet of graph paper (one with bigger squares), draw a small grid, say 5x7, label the axes and fill in some of the "pixels". Then try to draw the same thing rotated the way you want. Observe what algorithm you use to produce the result. (or even better, imagine you had to instruct a friend over the phone, to generate the rotated image. What would you tell them to do step, by, step.?)
    – Dan Mašek
    Oct 2, 2018 at 23:29
  • Rotate in any angle: stackoverflow.com/a/52595346/3547485
    – Kinght 金
    Oct 4, 2018 at 3:33

3 Answers 3

6

If you need a simple rotation in OpenCV, it's built in:

img=cv2.imread('Images/Screenshot.png',cv2.IMREAD_GRAYSCALE)

imgrot = cv2.rotate(img,cv2.ROTATE_90_CLOCKWISE)

Other possibilities are cv2.ROTATE_90_COUNTERCLOCKWISE and cv2.ROTATE_180

2
  • 1
    This is literally the answer. No idea why it isn't the top solution.
    – bfeist
    Mar 28, 2021 at 2:53
  • 1
    It isn't. OP asked for NOT built-in methods, to implement in a double loop (so this is a matrix transformation problem in maths). Therefore this solution does work, but not for OP nor the question @bfeist
    – M.K
    Sep 29, 2022 at 15:22
5

Does it have to be OpenCv? Cause if not you can easily do it with PIL:

from PIL import Image

def rotate_img(img_path, rt_degr):
    img = Image.open(img_path)
    return img.rotate(rt_degr, expand=1)

img_rt_90 = rotate_img('Images/Screenshot.png', 90)
img_rt_90.save('img_rt_90.png')
3
  • 1
    This will drastically decrease image quality.
    – Boz
    Apr 15, 2021 at 23:57
  • Why and how? @Boz
    – M.K
    Sep 29, 2022 at 15:11
  • 1
    @M.K, expand = 1, means swapping the width and height, doing so will change the quality of the image.
    – Boz
    Sep 29, 2022 at 19:59
0

I use the PIL package, it's very simple to do that.

from PIL import Image
path = '/Users/diegodsp/sample.jpg'
img = Image.open(path)
img = img.rotate(90) # 90, -90, 180, ...
img.save(path) # to override your old file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.