首页 > 解决方案 > 如何使用 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()

标签: pythonimage-processingrotationpython-imaging-libraryscikit-image

解决方案


我认为您实际上想将旋转图像粘贴到背景图像上,同时尊重旋转图像的透明度。

所以,改变这一行:

com1 = Image.alpha_composite(bg, rt).convert('RGB')

至:

bg.paste(rt, mask=rt)

推荐阅读