【问题标题】:Mirror an image in JES在 JES 中镜像图像
【发布时间】:2014-04-02 21:37:31
【问题描述】:

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

我了解镜像在 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

【问题讨论】:

标签: jython jes


【解决方案1】:

试试这个

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) 

【讨论】:

  • 您好,我修复了您代码的格式。确保所有行都缩进四个空格,以便 Stack Overflow 正确格式化。您也可以选择代码并按工具栏上的{} 按钮自动执行此操作。
【解决方案2】:

我个人发现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)

【讨论】:

    【解决方案3】:

    这似乎工作得很好。我放了一些 cmets,这样你就可以用自己的风格重写。

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

    #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

    顺便说一句,我在编程课的介绍中获得了满分;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 2014-08-06
      • 2019-08-10
      • 2016-03-07
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      相关资源
      最近更新 更多