简体   繁体   中英

Having trouble rewriting code to list comprehension for image rotation in python

So after not finding my problem here on stackoverflow, which is how to rewrite a for-loop to a list comprehension, where values are inserted I have to ask now if it possible to rewrite this code:

rotate = np.zeros((w,h,c), np.uint8) # create an empty image filled with zeros turned 90°

for y in xrange(h):
    for x in xrange(w):
        rotate[x][y] = img[y][x]

into list comprehension? I thought something like this would work, but it didn't:

rotate[x][y] = img[y][x] for y in range(h) for x in range(w) 

after that I just played around with various combinations of adding indexes and brackets and I always got some syntax errors. Just for the record, I know that there are functions for rotation of images in opencv and in numpy, I'm just interested in rewriting the for-loop to list-comprehension.

rotate = np.array([[img[y][x] for y in xrange(h)] for x in xrange(w)])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM