0

我的代码似乎将每一列复制到不同的位置,但我不确定将 rand.int 函数移到哪里;当它在其他任何地方时,我得到一个错误。这是我当前的代码:

def randomCollage(pic, count):
  pic = makePicture(getMediaPath(pic))
  canv = makePicture(getMediaPath(r"7inX95in.jpg"))
  startX = 0
  startY = 0
  endX = getWidth(canv) - getWidth(pic)
  endY = getHeight(canv) - getHeight(pic) 

  for count in range (0, count):
    targetX = random.randint(startX, endX)  
    for sourceX in range(0, getWidth(pic)):   
      targetY = random.randint(startY, endY)
      for sourceY in range(0, getHeight(pic)):
        color = getColor(getPixel(pic, sourceX, sourceY))
        setColor(getPixel(canv, targetX, targetY), color)
        targetY = targetY + 1
      targetX = targetX + 1

  explore(canv)
  return(canv)
4

1 回答 1

0

每个图像副本都需要一个随机的基本位置,但是从那里开始,行和列必须是有序的。该基地最好被视为左上角,您应该在循环之外选择它,这样它就不会在像素复制操作期间发生变化。尝试类似:

targetX = random.randint(startX, endX)  
targetY = random.randint(startY, endY)
for count in range (0, count):
    for sourceX in range(0, getWidth(pic)):   
        for sourceY in range(0, getHeight(pic)):
            color = getColor(getPixel(pic, sourceX, sourceY))
            setColor(getPixel(canv, targetX+sourceX, targetY+sourceY), color)
于 2013-11-04T02:13:44.203 回答