python-3.x - 如何使用 features.rasterize 在 Python 3 中创建遮罩图像?
问题描述
目前,我使用以下代码来创建蒙版图像(classes = ['tree', 'car', 'bicycle']
,polygons
是几何对象的列表,其中每个几何对象具有coordinates
定义图像上的多边形的字段,该多边形是类对象的边界框):
def create_mask(self, mask_size, classes, polygons):
# type (Tuple[int, int], List[str], List[geometry]) -> Image
# Create a new palette image, the default color of Image.new() is black
# https://pillow.readthedocs.io/en/3.3.x/handbook/concepts.html#modes
img = Image.new('P', mask_size)
img.putpalette(self.palette) # palette = [0, 0, 0, 255, 0, 0, ...]
draw = ImageDraw.Draw(img)
for i, class_ in enumerate(classes):
color_index = self.class_to_color_index[class_]
draw.polygon(xy=polygons[i].exterior.coords, fill=color_index)
del draw
return img
有没有办法用 using 重写这段代码features.rasterize
?
解决方案
推荐阅读
- python - 向新列添加时间戳
- amazon-s3 - 为挂载的 S3 存储桶目录中的文件设置默认权限
- python - 你如何计算二维列表中的值
- flutter - Flutter - 如何在横向模式下打开全屏键盘
- laravel - [Vue 警告]:未知的自定义元素:
- 您是否正确注册了组件?(在发现 ) - web-services - 用 Gin 实现的 Go HTTP 服务发送了大约 50% 的数据然后关闭连接,为什么?
- android - Android Broadcast Reciver 在 ViewPager 中使用时注册多个侦听器
- arrays - 如何将项目添加到 JSON 文件中的嵌套数组,然后保存这些更改。节点 Js
- bash - 检查目录中是否存在文件数组并检查 bash 脚本中文件名中的模式
- sql-server - 如何从正在导入的平面文件中删除随机中断(SSIS)