Rotating an image using cImage
Question: I'm trying to rotate an image in Python by 90 degrees using cImage (and only
cImage) and here's what I've got so far.

    
    
    def rotateImage90CW(imageFile):
        myWin = ImageWin("90D", 350, 630)
        oldIm = FileImage(imageFile)
        newIm = EmptyImage(350, 630)
        bigList = []
        for row in range(oldIm.getHeight()):
            bigList.append([]);
            for column in range(oldIm.getWidth()):
                x = oldIm.getPixel(column, row)
                bigList[row].append(x)
        bigList.reverse()
        for row in range(newIm.getHeight()):
            for column in range(newIm.getWidth()):
                newIm.setPixel(column, row, bigList[column][row])
        newIm.draw(myWin)
    

Unfortunately, it only gives me a fully black image when I load the function.
Can someone tell me what's wrong with my code? :[ Thanks.


Answer: I used `map` and `zip` to basically transpose your list:

from cImage import ImageWin, FileImage, EmptyImage

    
    
    def rotate90D(img):
      '''
      Rotate +90deg
      '''
      assert img != None, 'Image is empty!'
      map(list, zip(*img)) # transpose; this can be called multiple times to rotate to 0, 90, 180, 270
    
    def rotate90D_(img):
      '''
      Rotate -90deg
      '''
      assert img != None, 'Image is empty!'
      img.reverse()
      map(list, zip(*img)) # transpose; this can be called multiple times to rotate to 0, 90, 180, 270
    
    def unshared_copy(inList):
      '''
      Create a copy of a lists of lists
      '''
      # I use this because of map(list, zip(*img))
      if isinstance(inList, list):
        return list( map(unshared_copy, inList) )
      return inList
    
    # Load image
    oldIm = FileImage('/home/USER/Pictures/google doodles/google-doodle-90th-anniversary-of-the-first-demonstration-of-television-6281357497991168.2-hp.jpg')
    # Create two empty image (one for +90deg and another for -90deg
    newIm90D = EmptyImage(oldIm.height, oldIm.width)
    newIm90D_ = EmptyImage(oldIm.height, oldIm.width)
    
    # Create windows for displaying all the images
    myWin0D = ImageWin('0Deg', oldIm.width, oldIm.height)
    myWin90D = ImageWin('+90Deg', newIm90D.width, newIm90D.height)
    myWin90D_ = ImageWin('-90Deg', newIm90D_.width, newIm90D_.height)
    
    # Generate a list of lists from the loaded image
    img_to_matrix = []
    for row in range(oldIm.getHeight()):
      t = [];
      for column in range(oldIm.getWidth()):
        x = oldIm.getPixel(column, row)
        t.append(x)
      img_to_matrix.append(t)
    
    # Create a copy of the list of lists so that we can demonstrate rotation in both directions
    img_to_matrix2 = unshared_copy(img_to_matrix)
    
    # Rotate +90deg
    rotate90D(img_to_matrix)
    # Rotate -90deg
    rotate90D_(img_to_matrix2)
    
    # Load the pixel data in the respective images
    for row in range(newIm90D.getHeight()):
      for col in range(newIm90D.getWidth()):
        newIm90D.setPixel(col, row, img_to_matrix[col][row])
    
    for row in range(newIm90D_.getHeight()):
      for col in range(newIm90D_.getWidth()):
        newIm90D_.setPixel(col, row, img_to_matrix2[col][row])
    
    # Display the images
    oldIm.draw(myWin0D)
    newIm90D.draw(myWin90D)
    newIm90D_.draw(myWin90D_)
    

And here is what I get:

[![enter image description
here](http://i.stack.imgur.com/BjcYX.png)](http://i.stack.imgur.com/BjcYX.png)

Since you named your list of lists `bigLists` I would assume that you want to
load huge image. So you should consider modifying my sample code since (as
mentioned in the code comments) I do some copying.

**PS:** Frankly, I personally wouldn't bother using an image "library" that
can't do basic transformations. I looked inside `cImage.py` and it seems that
it uses `PIL`'s `Image` as a basis. `PIL` itself does offer rotation (and
other such basic things) out of the box if I recall correctly (I'm more of an
OpenCV guy) though I don't know if the `cImage` custom image format is
compatible with `PIL`'s image processing tools.

