gpt4 book ai didi

python - 裁剪功能 Jython JES

转载 作者:太空宇宙 更新时间:2023-11-03 19:03:05 25 4
gpt4 key购买 nike

您好,我正在尝试在 Jython Environment For Students 中创建裁剪函数,这是我迄今为止所拥有的。

我还试图弄清楚如何使 Canvas 的大小能够容纳所有新像素。

任何帮助

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)

最佳答案

试试这个:

注意:生成的 Canvas 的大小可以通过(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 …………


<小时/>

输出参数错误:

>>> cropPic = crop(picture, 50, 500, 50, 180)
>>>
======= Loading Progam =======
Error: bad cropping bounds ! - Expected [0..258] x [0..193]

关于python - 裁剪功能 Jython JES,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15543488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com