python - 如何使用 skimage 从旋转图像中删除灰色边框?
问题描述
我想删除使用旋转图像时出现的灰色边框skimage.transform.rotate
。我宁愿使用 skimage 库,因为在 Pillow 上旋转的图像更加像素化。这是一个在白色背景上使用黄色旋转图像的示例:
from skimage import transform
from skimage.io import imread, imshow, imsave
from PIL import Image
# white background
bg = Image.new('RGB', (557, 558), (255, 255, 255))
bg = bg.convert('RGBA')
# yellow image
y = Image.new('RGB', (557, 558), (254, 255, 14))
y = y.convert('RGBA')
y.save('yellow.png')
# rotated yellow image in skimage
y = imread('yellow.png')
rotate = transform.rotate(y, 20)
imsave('rotated.png', rotate)
# read rotated image in Pillow
rt = Image.open('rotated.png')
# superimpose yellow image on white background
com1 = Image.alpha_composite(bg, rt).convert('RGB')
com1.show()
解决方案
我认为您实际上想将旋转图像粘贴到背景图像上,同时尊重旋转图像的透明度。
所以,改变这一行:
com1 = Image.alpha_composite(bg, rt).convert('RGB')
至:
bg.paste(rt, mask=rt)
推荐阅读
- excel - 是否有使用 VBA 提取 PKZip 文件的特殊方法?
- architecture - 如何在 krakend-amqp 中实现 rpc,或者如何通过 krakend 中的队列根据请求向客户端返回响应?
- java - Java机器人编程。在拾取和放置任务中移动部件时如何跟踪它们的属性?
- sql - 更新 XML Clob 节点
- ios - 在 Docusign iOS sdk DSMManager.login(withEmail: ..) 中遇到了一些问题
- python - 根据 pandas 中的条件删除 DataFrame 行的最有效方法是什么?
- javascript - 有没有办法在状态下修改数组中的元素
- python-3.x - ValueError:时间数据“2017-01-02”与格式“%Y-%m-%d %H:%M:%S”不匹配。要改变什么?
- python - 为什么 index 在 python 列表中比 pop 快?
- r - 基于响应式数据框创建值框?