Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python图像反转_Python - Fatal编程技术网

Python图像反转

Python图像反转,python,Python,我试图将图像翻转到完全相反的方向(从左到右),但似乎无法理解代码。我知道如何将它们对半镜像,然而,我似乎无法实现完全的翻转。到目前为止,我有: def flip(source): width=getWidth(source)/2 height=getHeight(source) for y in range(0,height): for x in range(0,width): leftPixel=getPixel(source,x,y) rightP

我试图将图像翻转到完全相反的方向(从左到右),但似乎无法理解代码。我知道如何将它们对半镜像,然而,我似乎无法实现完全的翻转。到目前为止,我有:

def flip(source):
  width=getWidth(source)/2
  height=getHeight(source)
  for y in range(0,height):
    for x in range(0,width):
      leftPixel=getPixel(source,x,y)
      rightPixel=getPixel(source,width-x-1,y)
      color1=getColor(leftPixel)
      color2=getColor(rightPixel)
      setColor(rightPixel,color1)
      setColor(leftPixel,color2)
在这一行中,
width
应该是图像的全宽,而不是宽度的一半。我建议将
/2
移动到内部循环范围

width  = getWidth (source)
height = getHeight(source)

for y in range(height):
  for x in range(width / 2):

相对于的函数,您的代码将需要很长时间才能运行。

使用已经实现的函数是一种很好的风格:

width  = getWidth (source)
height = getHeight(source)

for y in range(height):
  for x in range(width / 2):