加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 11:24:49 所属栏目:Python 来源:网络整理
导读:我正在尝试编写一个 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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读