编写函数randomCollage(pic),将pic复制到“7inX95”中的5个随机位置输入.jpg”

2024-04-25 15:02:44 发布

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

我的代码似乎在将每一列复制到不同的位置,但我不确定要将兰德公司函数;我得到一个错误,当它在任何其他地方。 这是我目前的代码:

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)

Tags: 代码inforcountrangepicgetwidthcanv
1条回答
网友
1楼 · 发布于 2024-04-25 15:02:44

对于每个图像副本,您需要一个随机的基位置,但是从那里开始,行和列必须按顺序排列。该基础最好是看到的顶部,左角,你应该挑选它以外的循环,这样它就不会改变在像素复制操作。尝试以下操作:

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)

相关问题 更多 >

    热门问题