Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to rotate image 90 degrees(counterclockwise and clockwise ) in python Here i

ID: 3567745 • Letter: H

Question

How to rotate image 90 degrees(counterclockwise and clockwise ) in python

Here is my code r1 for clockwise, r2 for counterclockwise

def r1(img):
w = img.getWidth()
h = img.getHeight()
new = Picture(h,w)
for y in range(h):
for x in range(w):
r,g,b = img.getPixel(x,y)
new.setPixel(y,x,[r,g,b])

def r2(img):
w = img.getWidth()
h = img.getHeight()
new = Picture(h,w)
for y in range (h):
for x in range (w):
r,g,b = img.getPixel(w-x-1,h-y-1)
new.setPixel(y,x,[r,g,b])

New image is rotating, but not directly 90 degree, more like rotate 90 degree and flip it horizontally.

Can somebody to help me fix my python problem?

Explanation / Answer

But this is clearly not the most optimized way because you'll compute X images instead of the one you are interested in. I suggest you try a basic switch...case approach first (even if this statement doesn't exists in Python ;):

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote