0

I'm trying to mirror an image. That is, if, e.g., a person is facing to the left, when the program terminates I want that person to now be facing instead to the right.

I understand how mirroring works in JES, but I'm unsure how to proceed here. Below is what I'm trying; be aware that image is a global variable declared in another function.

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
4

3 回答 3

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) 
于 2014-04-08T23:04:50.020 回答
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)
于 2015-04-16T08:06:47.290 回答
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

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

于 2015-10-21T09:41:18.727 回答