天天看點

python圖像切割成多邊形_使用Python/PIL裁剪多邊形

另一個基于@user2667409答案的解決方案,

它使用每個元素1位來表示掩碼,并将最終結果導出為JPEG格式。import numpy

from PIL import Image, ImageDraw

# read image as RGB (without alpha)

img = Image.open("crop.jpg").convert("RGB")

# convert to numpy (for convenience)

img_array = numpy.asarray(img)

# create mask

polygon = [(444,203),(623,243),(691,177),(581,26),(482,42)]

# create new image ("1-bit pixels, black and white", (width, height), "default color")

mask_img = Image.new('1', (img_array.shape[1], img_array.shape[0]), 0)

ImageDraw.Draw(mask_img).polygon(polygon, outline=1, fill=1)

mask = numpy.array(mask_img)

# assemble new image (uint8: 0-255)

new_img_array = numpy.empty(img_array.shape, dtype='uint8')

# copy color values (RGB)

new_img_array[:,:,:3] = img_array[:,:,:3]

# filtering image by mask

new_img_array[:,:,0] = new_img_array[:,:,0] * mask

new_img_array[:,:,1] = new_img_array[:,:,1] * mask

new_img_array[:,:,2] = new_img_array[:,:,2] * mask

# back to Image from numpy

newIm = Image.fromarray(new_img_array, "RGB")

newIm.save("out.jpg")