Jython图像缩放算法

2024-05-15 02:37:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道这是一个算术问题,而不是纯粹的编程问题,但它涉及到一个问题,我正在努力解决。而且,我不知道有什么好地方可以问数学或JES相关的话题。在

我遇到的问题是,对于uni任务,我们要用JES画不同的国旗。标志可以是任何大小,由用户决定。旗帜是根据旗上星星的图案和数目来评定难度的。在

不管怎样,我正在试着画智利国旗,我遇到的唯一问题是如何计算这颗星星的比例。在

这是我用来缩放星星的函数:

def scaleStar(picture, scale):  
# the starting X position is set to 0
sourceX = 0
# Calculates the scaled width and height of the picture as an integer
scaleWidth = int(getWidth(picture) / scale)    
scaleHeight = int(getHeight(picture) / scale)
# Creates an empty picture with the scaled dimensions just calculated
scaledPicture = makeEmptyPicture(scaleWidth, scaleHeight)  
# Begins the loop that goes through the entire picture and copies the
# pixel from the source to the target pixel
for scaleX in range(0, scaleWidth):
  sourceY = 0
  for scaleY in range(0, scaleHeight):
    sourcePixel = getPixel(picture, int(sourceX), int(sourceY))
    scaledPixel = getPixel(scaledPicture, scaleX, scaleY)
    colour = getColor(sourcePixel)
    setColor(scaledPixel, colour)
    sourceY = sourceY + scale
  sourceX = sourceX + scale
return scaledPicture

我知道那颗星是整个国旗的1/12大小。所以对于scale参数,我尝试将值设为12。这适用于一幅300宽200高的旗帜画。但其他尺寸的就不行了。我尝试了整个标志的(height*width)/12,但这只返回了一个值错误。在过去的3天里,我一直在玩这个代码,每次我想我得到答案时,星星的大小都不一样。在

如果这是一个愚蠢的问题,我真的很抱歉,但如果有人对我如何解决这个问题有任何建议,我将不胜感激。如果您需要更多的信息来帮助您,请不要犹豫地问:)


Tags: theto标志intscalepicture旗帜国旗

热门问题