使用Jython将两张图片混合在一起

2024-07-04 08:18:27 发布

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

我正在尝试将两张图片混合在一起,每次运行此操作时都会出现错误:

getPixel(picture,x,y): x (= 310) is less than 0 or bigger than the width (= 309)

错误值为:

^{pr2}$

我已经多次调整图像大小,看看这是否有效,但它不起作用。任何帮助都会很好。在

目前我的照片大小如下:
羚羊=310x369
波纹管=250x341

def blendPictures():                #define a new function
  Pic1=makePicture(pickAFile())     #Pick Pic1 Antelope(Barb)
  Pic2=makePicture(pickAFile())     #Pick Pic2 Jackalope(Katie)
  canvas=makeEmptyPicture(640,480)  #Create an empty picture file
  sourceX=0
  for targetX in range(0,150):      #Let's ad our first loop
    sourceY=0
    for targetY in range(0,getHeight(Pic1)):
      color = getColor(getPixel(Pic1,sourceX,sourceY))
      setColor(getPixel(canvas,targetX,targetY),color)
      sourceY = sourceY + 1
    sourceX = sourceX +1
  overlap = getWidth(Pic1)-150
  sourceX=0
  for targetX in range(150,getHeight(Pic1)):
    sourceY=0
    for targetY in range(0,getHeight(Pic2)):
      APixel = getPixel(Pic1,sourceX+150,sourceY)
      BPixel = getPixel(Pic2,sourceX,sourceY)
      newRed=0.50*getRed(APixel)+0.50*getRed(BPixel)
      newGreen=0.50*getGreen(APixel)+0.50*getGreen(BPixel)
      newBlue=0.50*getBlue(APixel)+0.50*getBlue(BPixel)
      color=makeColor(newRed,newGreen,newBlue)
      setColor(getPixel(canvas,targetX,targetY),color)
      sourceY=sourceY+1
    sourceX=sourceX+1
  sourceX=overlap
  for targetX in range(150+overlap,150+getWidth(Pic2)):
    sourceY=0
    for targetY in range(0,getHeight(Pic2)):
       color=getColor(getPixel(Pic2,sourceX,sourceY))
       setColor(getPixel(canvas,targetX,targetY),color)
       sourceY=sourceY+1
    sourceX=sourceX+1
  show(canvas)
  return canvas

Tags: inforrangecolorcanvasgetpixeltargetxgetheight
1条回答
网友
1楼 · 发布于 2024-07-04 08:18:27

我不知道你想做什么, 但一个明显的问题是第二个循环部分:

for targetX in range(150, getHeight(Pic1)):

这应该是:

^{pr2}$

否则,沿x方向循环219次(369-150), 这将导致行中出现上述错误:

^{3}$

因为在219的第160次迭代中,代码将尝试getPixel(Pic1, 310, 0),这是越界的。在

相关问题 更多 >

    热门问题