Polygon crop/clip using Python / PIL

前端 未结 3 1069
南方客
南方客 2020-12-15 13:34

The polygon points along with the uncut, original image are sent by client to the server.

Is there a way that I can clip (crop) the original image along these point

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 13:53

    I did this code to clip an area of an image defined by a polygon.

    from PIL import Image, ImageDraw
    
    original = Image.open("original.jpg")
    xy = [(100,100),(1000,100),(1000,800),(100,800)]
    mask = Image.new("L", original.size, 0)
    draw = ImageDraw.Draw(mask)
    draw.polygon(xy, fill=255, outline=None)
    black =  Image.new("L", original.size, 0)
    result = Image.composite(original, black, mask)
    result.save("result.jpg")
    

提交回复
热议问题