python消除图像模块区域_使用Python / PIL裁剪图像的非对称区域

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?)

解决方案

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:

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值