使用Python/PIL裁剪图像的非对称区域

发布于 2024-09-18 16:07:45 字数 379 浏览 17 评论 0原文

有没有办法用 Python PIL 剪切图像的矩形区域?

例如,在这张图片中,我想排除所有黑色区域以及塔、屋顶和电线杆。
http://img153.imageshack.us/img153/5330/skybig.jpg

我想 ImagePath 模块可以做到这一点,但此外,我如何读取 svg 文件的数据并将其转换为路径?

任何帮助将不胜感激。


(我的子问题可能是更简单的任务:如何切割至少一个图像的圆?)

Is there a way to cut out non rectangular areas of an image with Python PIL?

e.g. in this picture I want to exclude all black areas as well as towers, rooftops and poles.
http://img153.imageshack.us/img153/5330/skybig.jpg

I guess the ImagePath Module can do that, but furthermore, how can I read data of e.g. a svg file and convert it into a path?

Any help will be appreciated.

(My sub question is presumably the easier task: how to cut at least a circle of an image?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是暖光i 2024-09-25 16:07:45

如果我理解正确的话,您想让图像中的某些区域变得透明。这些区域的形状是随机的。最简单的方法(我能想到的)是创建一个蒙版并将其放入图像的 Alpha 通道。下面的代码展示了如何执行此操作。

如果您的问题是“如何创建多边形蒙版”,我会将您重定向到:

SciPy 创建 2D 多边形屏蔽

并查看已接受的答案。

br,

Juha

import numpy
import Image

# read image as RGB and add alpha (transparency)
im = Image.open("lena.png").convert("RGBA")

# convert to numpy (for convenience)
imArray = numpy.asarray(im)

# create mask (zeros + circle with ones)
center = (200,200)
radius = 100
mask = numpy.zeros((imArray.shape[0],imArray.shape[1]))
for i in range(imArray.shape[0]):
    for j in range(imArray.shape[1]):
        if (i-center[0])**2 + (j-center[0])**2 < radius**2:
            mask[i,j] = 1

# assemble new image (uint8: 0-255)
newImArray = numpy.empty(imArray.shape,dtype='uint8')

# colors (three first columns, RGB)
newImArray[:,:,:3] = imArray[:,:,:3]

# transparency (4th column)
newImArray[:,:,3] = mask*255          

# back to Image from numpy
newIm = Image.fromarray(newImArray, "RGBA")
newIm.save("lena3.png")

编辑

实际上,我无法抗拒...多边形遮罩解决方案是如此优雅(用这个替换上面的圆圈):

# create mask
polygon = [(100,100), (200,100), (150,150)]
maskIm = Image.new('L', (imArray.shape[0], imArray.shape[1]), 0)
ImageDraw.Draw(maskIm).polygon(polygon, outline=1, fill=1)
mask = numpy.array(maskIm)

Edit2

现在当我想到它时。如果您有黑白 svg,则可以直接将 svg 加载为蒙版(假设白色是您的蒙版)。我没有示例 svg 图像,所以我无法对此进行测试。我不确定 PIL 是否可以打开 svg 图像。

If I understood correctly, you want to make some areas transparent within the image. And these areas are random shaped. Easiest way (that I can think of) is to create a mask and put it to the alpha channel of the image. Below is a code that shows how to do this.

If your question was "How to create a polygon mask" I will redirect you to:

SciPy Create 2D Polygon Mask

and look the accepted answer.

br,

Juha

import numpy
import Image

# read image as RGB and add alpha (transparency)
im = Image.open("lena.png").convert("RGBA")

# convert to numpy (for convenience)
imArray = numpy.asarray(im)

# create mask (zeros + circle with ones)
center = (200,200)
radius = 100
mask = numpy.zeros((imArray.shape[0],imArray.shape[1]))
for i in range(imArray.shape[0]):
    for j in range(imArray.shape[1]):
        if (i-center[0])**2 + (j-center[0])**2 < radius**2:
            mask[i,j] = 1

# assemble new image (uint8: 0-255)
newImArray = numpy.empty(imArray.shape,dtype='uint8')

# colors (three first columns, RGB)
newImArray[:,:,:3] = imArray[:,:,:3]

# transparency (4th column)
newImArray[:,:,3] = mask*255          

# back to Image from numpy
newIm = Image.fromarray(newImArray, "RGBA")
newIm.save("lena3.png")

Edit

Actually, I could not resist... the polygon mask solution was so elegant (replace the above circle with this):

# create mask
polygon = [(100,100), (200,100), (150,150)]
maskIm = Image.new('L', (imArray.shape[0], imArray.shape[1]), 0)
ImageDraw.Draw(maskIm).polygon(polygon, outline=1, fill=1)
mask = numpy.array(maskIm)

Edit2

Now when I think of it. If you have a black and white svg, you can load your svg directly as mask (assuming white is your mask). I have no sample svg images, so I cannot test this. I am not sure if PIL can open svg images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文