我正在尝试编写一个
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。