使用cImage旋转图像

时间:2016-03-12 08:15:18

标签: python

我正在尝试使用cImage(并且只有cImage)将Python中的图像旋转90度,这是我到目前为止所做的。

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)

不幸的是,当我加载这个函数时,它只给我一个完全黑的图像。有人能告诉我我的代码有什么问题吗? :[谢谢。

1 个答案:

答案 0 :(得分:0)

我使用mapzip基本上转置了您的列表:

从cImage导入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_)

这就是我得到的:

enter image description here

由于您为列表bigLists命名,我假设您要加载大图像。所以你应该考虑修改我的示例代码,因为(如代码注释中所述)我做了一些复制。

PS:坦率地说,我个人不会打扰使用无法进行基本转换的图像“库”。我查看cImage.py内部,似乎以PIL的{​​{1}}为基础。 Image本身确实提供了旋转(和其他基本的东西),如果我没记错的话(我更像是一个OpenCV人)虽然我不知道PIL自定义图像格式与cImage的图像处理工具兼容。