Python 3 function to rotate an image 90°
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
A coding challenge that rotates an image 90 degrees counterclockwise. The image is represented as an array matrix. I believe the time complexity is O(n2), but I'd like to know for sure, as well as any other feedback.
def rotate_image(img):
rotated_image = [ for x in range(len(img))]
for i in range(len(img)):
for j in range(len(img[i])):
rotated_image[len(img) - j - 1].append(img[i][j])
return rotated_image
Example usage:
image = [
[1, 1, 5, 9, 9],
[2, 2, 6, 0, 0],
[3, 3, 7, 1, 1],
[4, 4, 8, 2, 2],
[5, 5, 9, 3, 3]
]
rotated_img = rotate_image(image)
for i in rotated_img:
print(i)
Outputs:
[9, 0, 1, 2, 3]
[9, 0, 1, 2, 3]
[5, 6, 7, 8, 9]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
python python-3.x programming-challenge image matrix
add a comment |Â
up vote
1
down vote
favorite
A coding challenge that rotates an image 90 degrees counterclockwise. The image is represented as an array matrix. I believe the time complexity is O(n2), but I'd like to know for sure, as well as any other feedback.
def rotate_image(img):
rotated_image = [ for x in range(len(img))]
for i in range(len(img)):
for j in range(len(img[i])):
rotated_image[len(img) - j - 1].append(img[i][j])
return rotated_image
Example usage:
image = [
[1, 1, 5, 9, 9],
[2, 2, 6, 0, 0],
[3, 3, 7, 1, 1],
[4, 4, 8, 2, 2],
[5, 5, 9, 3, 3]
]
rotated_img = rotate_image(image)
for i in rotated_img:
print(i)
Outputs:
[9, 0, 1, 2, 3]
[9, 0, 1, 2, 3]
[5, 6, 7, 8, 9]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
python python-3.x programming-challenge image matrix
1
O(N^2)
wrt.N
being the number of pixels or side of the squre?
– Dima Tisnek
2 hours ago
1
In your analysis, what exactly is "n"? Are you implying that the matrix is necessarily square?
– 200_success
2 hours ago
@200_success Yes the matrix is expected to always be proportional, i.e., the same length everywhere.
– NewbieWanKenobi
2 hours ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
A coding challenge that rotates an image 90 degrees counterclockwise. The image is represented as an array matrix. I believe the time complexity is O(n2), but I'd like to know for sure, as well as any other feedback.
def rotate_image(img):
rotated_image = [ for x in range(len(img))]
for i in range(len(img)):
for j in range(len(img[i])):
rotated_image[len(img) - j - 1].append(img[i][j])
return rotated_image
Example usage:
image = [
[1, 1, 5, 9, 9],
[2, 2, 6, 0, 0],
[3, 3, 7, 1, 1],
[4, 4, 8, 2, 2],
[5, 5, 9, 3, 3]
]
rotated_img = rotate_image(image)
for i in rotated_img:
print(i)
Outputs:
[9, 0, 1, 2, 3]
[9, 0, 1, 2, 3]
[5, 6, 7, 8, 9]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
python python-3.x programming-challenge image matrix
A coding challenge that rotates an image 90 degrees counterclockwise. The image is represented as an array matrix. I believe the time complexity is O(n2), but I'd like to know for sure, as well as any other feedback.
def rotate_image(img):
rotated_image = [ for x in range(len(img))]
for i in range(len(img)):
for j in range(len(img[i])):
rotated_image[len(img) - j - 1].append(img[i][j])
return rotated_image
Example usage:
image = [
[1, 1, 5, 9, 9],
[2, 2, 6, 0, 0],
[3, 3, 7, 1, 1],
[4, 4, 8, 2, 2],
[5, 5, 9, 3, 3]
]
rotated_img = rotate_image(image)
for i in rotated_img:
print(i)
Outputs:
[9, 0, 1, 2, 3]
[9, 0, 1, 2, 3]
[5, 6, 7, 8, 9]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
python python-3.x programming-challenge image matrix
python python-3.x programming-challenge image matrix
edited 2 hours ago


200_success
124k14144401
124k14144401
asked 2 hours ago


NewbieWanKenobi
3131212
3131212
1
O(N^2)
wrt.N
being the number of pixels or side of the squre?
– Dima Tisnek
2 hours ago
1
In your analysis, what exactly is "n"? Are you implying that the matrix is necessarily square?
– 200_success
2 hours ago
@200_success Yes the matrix is expected to always be proportional, i.e., the same length everywhere.
– NewbieWanKenobi
2 hours ago
add a comment |Â
1
O(N^2)
wrt.N
being the number of pixels or side of the squre?
– Dima Tisnek
2 hours ago
1
In your analysis, what exactly is "n"? Are you implying that the matrix is necessarily square?
– 200_success
2 hours ago
@200_success Yes the matrix is expected to always be proportional, i.e., the same length everywhere.
– NewbieWanKenobi
2 hours ago
1
1
O(N^2)
wrt. N
being the number of pixels or side of the squre?– Dima Tisnek
2 hours ago
O(N^2)
wrt. N
being the number of pixels or side of the squre?– Dima Tisnek
2 hours ago
1
1
In your analysis, what exactly is "n"? Are you implying that the matrix is necessarily square?
– 200_success
2 hours ago
In your analysis, what exactly is "n"? Are you implying that the matrix is necessarily square?
– 200_success
2 hours ago
@200_success Yes the matrix is expected to always be proportional, i.e., the same length everywhere.
– NewbieWanKenobi
2 hours ago
@200_success Yes the matrix is expected to always be proportional, i.e., the same length everywhere.
– NewbieWanKenobi
2 hours ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
How about using Python built-ins to do the job?
img = [[1, 2, 3], [10, 20, 30], [100, 200, 300]]
list(reversed(list(zip(*img))))
[(3, 30, 300), (2, 20, 200), (1, 10, 100)]
Very nice but I'd like the output to be formatted like the input, or an array matrix.
– NewbieWanKenobi
1 hour ago
Just dofor row in reversed(list(zip(*img))): print(row)
– Ludisposed
1 hour ago
@NewbieWanKenobi You canmap
thelist
constructor tozip(*img)
if you don't like tuples in your output. Alternatively,[list(row) for row in zip(*img)][::-1]
does the same but feels more readable (to me).
– Mathias Ettinger
22 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
How about using Python built-ins to do the job?
img = [[1, 2, 3], [10, 20, 30], [100, 200, 300]]
list(reversed(list(zip(*img))))
[(3, 30, 300), (2, 20, 200), (1, 10, 100)]
Very nice but I'd like the output to be formatted like the input, or an array matrix.
– NewbieWanKenobi
1 hour ago
Just dofor row in reversed(list(zip(*img))): print(row)
– Ludisposed
1 hour ago
@NewbieWanKenobi You canmap
thelist
constructor tozip(*img)
if you don't like tuples in your output. Alternatively,[list(row) for row in zip(*img)][::-1]
does the same but feels more readable (to me).
– Mathias Ettinger
22 mins ago
add a comment |Â
up vote
3
down vote
How about using Python built-ins to do the job?
img = [[1, 2, 3], [10, 20, 30], [100, 200, 300]]
list(reversed(list(zip(*img))))
[(3, 30, 300), (2, 20, 200), (1, 10, 100)]
Very nice but I'd like the output to be formatted like the input, or an array matrix.
– NewbieWanKenobi
1 hour ago
Just dofor row in reversed(list(zip(*img))): print(row)
– Ludisposed
1 hour ago
@NewbieWanKenobi You canmap
thelist
constructor tozip(*img)
if you don't like tuples in your output. Alternatively,[list(row) for row in zip(*img)][::-1]
does the same but feels more readable (to me).
– Mathias Ettinger
22 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
How about using Python built-ins to do the job?
img = [[1, 2, 3], [10, 20, 30], [100, 200, 300]]
list(reversed(list(zip(*img))))
[(3, 30, 300), (2, 20, 200), (1, 10, 100)]
How about using Python built-ins to do the job?
img = [[1, 2, 3], [10, 20, 30], [100, 200, 300]]
list(reversed(list(zip(*img))))
[(3, 30, 300), (2, 20, 200), (1, 10, 100)]
answered 2 hours ago
Dima Tisnek
22613
22613
Very nice but I'd like the output to be formatted like the input, or an array matrix.
– NewbieWanKenobi
1 hour ago
Just dofor row in reversed(list(zip(*img))): print(row)
– Ludisposed
1 hour ago
@NewbieWanKenobi You canmap
thelist
constructor tozip(*img)
if you don't like tuples in your output. Alternatively,[list(row) for row in zip(*img)][::-1]
does the same but feels more readable (to me).
– Mathias Ettinger
22 mins ago
add a comment |Â
Very nice but I'd like the output to be formatted like the input, or an array matrix.
– NewbieWanKenobi
1 hour ago
Just dofor row in reversed(list(zip(*img))): print(row)
– Ludisposed
1 hour ago
@NewbieWanKenobi You canmap
thelist
constructor tozip(*img)
if you don't like tuples in your output. Alternatively,[list(row) for row in zip(*img)][::-1]
does the same but feels more readable (to me).
– Mathias Ettinger
22 mins ago
Very nice but I'd like the output to be formatted like the input, or an array matrix.
– NewbieWanKenobi
1 hour ago
Very nice but I'd like the output to be formatted like the input, or an array matrix.
– NewbieWanKenobi
1 hour ago
Just do
for row in reversed(list(zip(*img))): print(row)
– Ludisposed
1 hour ago
Just do
for row in reversed(list(zip(*img))): print(row)
– Ludisposed
1 hour ago
@NewbieWanKenobi You can
map
the list
constructor to zip(*img)
if you don't like tuples in your output. Alternatively, [list(row) for row in zip(*img)][::-1]
does the same but feels more readable (to me).– Mathias Ettinger
22 mins ago
@NewbieWanKenobi You can
map
the list
constructor to zip(*img)
if you don't like tuples in your output. Alternatively, [list(row) for row in zip(*img)][::-1]
does the same but feels more readable (to me).– Mathias Ettinger
22 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f204032%2fpython-3-function-to-rotate-an-image-90%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
O(N^2)
wrt.N
being the number of pixels or side of the squre?– Dima Tisnek
2 hours ago
1
In your analysis, what exactly is "n"? Are you implying that the matrix is necessarily square?
– 200_success
2 hours ago
@200_success Yes the matrix is expected to always be proportional, i.e., the same length everywhere.
– NewbieWanKenobi
2 hours ago