Image Jython(JES)-90度旋转函数
我需要写一个函数spin(pic,x),它将拍摄一张照片并逆时针旋转90度x次。在函数中,我只有90度顺时针旋转:Image Jython(JES)-90度旋转函数,image,rotation,jython,jes,Image,Rotation,Jython,Jes,我需要写一个函数spin(pic,x),它将拍摄一张照片并逆时针旋转90度x次。在函数中,我只有90度顺时针旋转: def rotate(pic): width = getWidth(pic) height = getHeight(pic) new = makeEmptyPicture(height,width) tarX = 0 for x in range(0,width): tarY = 0 for y in rang
def rotate(pic):
width = getWidth(pic)
height = getHeight(pic)
new = makeEmptyPicture(height,width)
tarX = 0
for x in range(0,width):
tarY = 0
for y in range(0,height):
p = getPixel(pic,x,y)
color = getColor(p)
setColor(getPixel(new,tarY,width-tarX-1),color)
tarY = tarY + 1
tarX = tarX +1
show(new)
return new
。。但我不知道如何编写一个函数,将它旋转X次。有人知道我怎么做吗 您可以调用
rotate()
X次:
def spin(pic, x):
new_pic = duplicatePicture(pic)
for i in range(x):
new_pic = rotate(new_pic)
return new_pic
a_file = pickAFile()
a_pic = makePicture(a_file)
show(spin(a_pic, 3))
但这显然不是最优化的方法,因为您将计算X图像,而不是您感兴趣的图像。我建议您首先尝试一种基本的切换…case
方法(即使Python中不存在此语句;):
然后,您可能会看到一种将这些情况合并为单个(但更复杂)double for loop
的方法。。。玩得开心
xx = (x % 4) # Just in case you want (x=7) to rotate 3 times...
if (xx == 1):
new = makeEmptyPicture(height,width)
tarX = 0
for x in range(0,width):
tarY = 0
for y in range(0,height):
p = getPixel(pic,x,y)
color = getColor(p)
setColor(getPixel(new,tarY,width-tarX-1),color)
tarY = tarY + 1
tarX = tarX +1
return new
elif (xx == 2):
new = makeEmptyPicture(height,width)
# Do it yourself...
return new
elif (xx == 3):
new = makeEmptyPicture(height,width)
# Do it yourself...
return new
else:
return pic