在JES中镜像图像

时间:2014-04-02 21:37:32

标签: jython jes

我正在尝试镜像图像。也就是说,如果例如一个人面向左边,当程序终止时我希望该人现在面向右边。

我理解镜像在JES中是如何工作的,但我不确定如何在这里继续。 以下是我正在尝试的内容;请注意image是另一个函数中声明的全局变量。

def flipPic(image):
  width = getWidth(image)
  height = getHeight(image)
  for y in range(0, height):
    for x in range(0, width):
       left = getPixel(image, x, y)
       right = getPixel(image, width-x-1, y)
       color = getColor(left)
       setColor(right, color)
  show(image)
  return image

3 个答案:

答案 0 :(得分:0)

试试这个

width = getWidth(pic)
height = getHeight(pic)
for y in range (0,height):
    for x in range (0, width/2):
        left=getPixel(pic, x, y)
        right=getPixel(pic, width-x-1,y)
        color1=getColor(left)
        color2=getColor(right)
        setColor(right, color1)
        setColor(left, color2)
repaint(pic) 

答案 1 :(得分:0)

我个人觉得repaint让新手感到困惑(就像我一样!)。 我建议这样的事情:

def mirrorImage(image):

width = getWidth(image)
height = getHeight(image)
for y in range (0,height):
    for x in range (0, width/2):
        left=getPixel(pic, x, y)
        right=getPixel(pic, width-x-1,y)
        color1=getColor(left)
        color2=getColor(right)
        setColor(right, color1)
        setColor(left, color2)
        show(image)
        return image
mirrorImage(image)

答案 2 :(得分:0)

这似乎运作良好..我发表了一些评论,因此你可以用自己的风格重写。

随时提出问题,但我认为您的问题可能已经得到解答^^

#this function will take the pixel values for a selected picture and 
#past them to a new canvas but fliped over! 
def flipPic(pict):
#here we take the height and width of the original picture
  width=getWidth(pict)
  height=getHeight(pict)
#here we make and empty canvas  
  newPict=makeEmptyPicture(width,height) 
#the Y for loop is setting the range to working for the y axes the started the X for loop 
  for y in range(0, height):
#the X for loop is setting the range to work in for the x axis 
    for x in range(0, width):
#here we are collecting the colour information for the origional pix in range of X and 
      colour=getColor(getPixel(pict,x,y))
#here we are setting the colour information to its new position on the blank canvas
      setColor(getPixel(newPict,width-x-1,y),colour)
      #setColor(getPixel(newPict,width-x-1,height-y-1),colour)#upsidedown
  show(newPict)

#drive function
pict = makePicture(pickAFile())
show(pict)
flipPic(pict)

如果先将其复制到JES,可能会更容易阅读:D

BTW我在编程课程的介绍中得到了满分;)