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

我正在尝试编写一个 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,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,y)
        newPxl= getPixel(newPic,y)
        color = getColor(pxl)
        setColor (newPxl,color)

  return (newPic)

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...