我在试着镜像一个图像。也就是说,如果一个人面朝左,当程序终止时,我希望这个人现在面朝右。
我理解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发布于 2014-04-09 07:04:50
尝尝这个
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) 发布于 2015-04-16 16:06:47
我个人发现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-10-21 17:41:19
这似乎工作得很好..我放了一些注释,这样你就可以用你自己的风格重写了。
请随时提问,但我认为您的问题可能已经得到回答^^
#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
顺便说一句,我在我的编程入门课上得到了满分;)
https://stackoverflow.com/questions/22823603
复制相似问题