Python 3 function to rotate an image 90°

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












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]









share|improve this question



















  • 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














up vote
1
down vote

favorite
1












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]









share|improve this question



















  • 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












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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]









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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










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)]





share|improve this answer




















  • 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











  • @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










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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)]





share|improve this answer




















  • 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











  • @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














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)]





share|improve this answer




















  • 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











  • @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












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)]





share|improve this answer












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)]






share|improve this answer












share|improve this answer



share|improve this answer










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 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
















  • 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











  • @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















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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery