gpt4 book ai didi

python - 在Python中将三角形区域从一张图片复制到另一张图片

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

我正在尝试编写一个 python 函数,它将一个三角形区域从图片上的任何位置复制到一张新的空白图片。我可以从图片中复制一个矩形区域到一个新的空白图片中,但我就是不知道如何复制一个三角形。这就是我所拥有的,但它只复制了一个矩形区域。对不起,如果它看起来凌乱或过于复杂,但我才刚刚开始如何用 python 编写。

  def copyTriangle():
file=pickAFile()
oldPic=makePicture(file)
newPic=makeEmptyPicture(getWidth(oldPic),getHeight(oldPic))
xstart=getWidth(oldPic)/2
ystart=getHeight(oldPic)/2
for y in range(ystart,getHeight(oldPic)):
for x in range(xstart,getWidth(oldPic)):
oldPixel=getPixel(oldPic,x,y)
colour=getColor(oldPixel)
newPixel=getPixel(newPic,x,y)
setColor(newPixel,colour)

最佳答案

将三角形区域从一张图片复制到另一张图片的功能。

def selectTriangle(pic):
w= getWidth (pic)
h = getHeight(pic)
newPic = makeEmptyPicture(w,h)
x0=107#test point 0
y0=44
x1=52#test point 1
y1=177
x2=273 #test point 2
y2=216
#(y-y0)/(y1-y0)=(x-x0)/(x1-x0)

for y in range (0,h):
for x in range (0, w):
#finding pixels within the plotted lines between eat set of points
if (x>((y-y0)*(x1-x0)/(y1-y0)+x0) and x<((y-y0)*(x2-x0)/(y2-y0)+x0) and x>((y-y2)*(x1-x2)/(y1-y2)+x2)):
pxl = getPixel(pic, x, y)
newPxl= getPixel(newPic,x,y)
color = getColor(pxl)
setColor (newPxl, color)

return (newPic)

Original Image Triangle

关于python - 在Python中将三角形区域从一张图片复制到另一张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14300396/

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