使用 pillow 计算
依赖库
from PIL import Image, ImageDraw
import numpy as np
核心代码
def edge(img):
np_data = np.array(img)
rr = np.where(np_data[:, :, 3] != 0)
xmin = min(rr[1])
ymin = min(rr[0])
xmax = max(rr[1])
ymax = max(rr[0])
return xmin, ymin, xmax, ymax
测试代码
img = Image.open('/Users/admin/Downloads/anoyi.png')
img = img.convert('RGBA')
img_edge = edge(img)
draw = ImageDraw.Draw(img)
draw.rectangle(img_edge, outline="red")
img.show()
最小外接矩形
网友评论