作物功能Jython JE

2024-09-26 18:04:59 发布

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

嗨,我正在尝试在Jython环境中为学生制作一个裁剪函数。在

我也在尝试如何使画布的大小,所有的新像素将采取。在

有什么帮助吗

def crop(pic, startX, endX, startY, endY):
    canvas = makeEmptyPicture(500, 800)
    for sourceX in range(startX, endX):
        for sourceY in range(startY, endY):
            color = getColor(getPixel(pic, sourceX, sourceY))
            setColor(getPixel(canvas, startX, startY), color)
            startY = startY + 1
        startX = startX + 1
    show(canvas)

Tags: infor环境rangejythoncolorcanvaspic
1条回答
网友
1楼 · 发布于 2024-09-26 18:04:59

试试这个:

注意:结果画布的大小可以从(endX-startX)x(endY-startY)获得

def crop(pic, startX, endX, startY, endY):  
    # Check if the cropping bounds are OK with size of the original picture 
    if (endX - startX > 0) and (endY - startY > 0) and \
                         (endX < getWidth(pic)) and (endY < getHeight(pic)):
        # Create a canvas with correct size
        canvas = makeEmptyPicture(endX - startX, endY - startY)
        # Browse the interesting part
        for sourceX in range(startX, endX):
            for sourceY in range(startY, endY):
                color = getColor(getPixel(pic, sourceX, sourceY))
                # Write the pixels, 
                # starting from 0 (=startX(Y)-startX(Y)) to endX(Y)
                setColor(getPixel(canvas, 
                              sourceX - startX, sourceY - startY), color)
        return canvas
    else:
        # Print error when passing wrong bounds
        printNow("Error: bad cropping bounds ! - Expected [0.." + 
            str(getWidth(pic)-1) + "] x [0.." +  str(getHeight(pic)-1) + "]")        
        return None

def main():
  file = pickAFile()
  picture = makePicture(file)
  cropPic = crop(picture, 50, 150, 50, 180)
  if (cropPic):
      show(cropPic)

main()


给予:




………enter image description here……………………….enter image description here。。。。。。。。。。。




输出参数错误:

^{pr2}$

相关问题 更多 >

    热门问题