简体   繁体   English

如何从python中的图像中删除白色模糊

[英]How to remove white fuzziness from image in python

I'm trying to remove the background from product images, save them as transparent png's and got to a point where I can't figure out how and why I get the white line around the products like a fuzziness(see second image) don't know the real word for the effect. 我正在尝试从产品图像中删除背景,将它们保存为透明png并且达到了一个我无法弄清楚如何以及为什么我会在产品周围获得白线的模糊(参见第二张图片)don'知道效果的真实词。 Also I'm losing the Nike swoosh which is white too :( 我也失去了耐克旋风,这也是白色的:(

输入 产量

from PIL import Image

img = Image.open('test.jpg')
img = img.convert("RGBA")
datas = img.getdata()


newData = []


for item in datas:
    if item[0] > 247 and item[1] > 247 and item[2] > 247:
        newData.append((255, 255, 255, 0))
    else:
        newData.append(item)

img.putdata(newData)
img.save("test.png", "PNG")

Any ideas how I can fix this so I get clean selections, edges ? 我有什么想法可以解决这个问题,所以我得到了干净的选择,边缘?

Take a copy of your image and use PIL/Pillow's ImageDraw.floodfill() to flood fill from the top-left corner using a reasonable tolerance - that way you will only fill to the edges of the shirt and avoid the Nike logo. 拍摄您的图像副本并使用PIL / Pillow的ImageDraw.floodfill()以合理的公差从左上角填充填充物 - 这样您只会填充到衬衫的边缘并避开Nike徽标。

Then take the background outline and make it white and everything else black and try applying some morphology (from scikit-image maybe) to dilate the white a little larger to hide the jaggies. 然后采取背景轮廓,使其变成白色,其他一切都变黑,并尝试应用一些形态(可能来自scikit-image)来扩大白色稍微大一些以隐藏锯齿。

Finally, put the resulting new layer into the image with putalpha() . 最后,使用putalpha()将生成的新图层放入图像中。


I am really pushed for time, but here are the bones of it. 我真的很热爱,但这里有它的骨头。 Just missing the copy of the original image at the start and the putalpha() of the new alpha layer back at the end... 只是在开始时丢失原始图像的副本,并在putalpha()新alpha层的putalpha() ...

from PIL import Image, ImageDraw
import numpy as np
import skimage.morphology

# Open the shirt
im = Image.open('shirt.jpg')

# Make all background pixels (not including Nike logo) into magenta (255,0,255)
ImageDraw.floodfill(im,xy=(0,0),value=(255,0,255),thresh=10)

# DEBUG
im.show()

在此输入图像描述

Experiment with the threshold ( thresh ) here. 与阈值(实验thresh )在这里。 If you make it 50, it works much more cleanly and may be good enough to stop. 如果你做到50,它的工作要干净得多,可能足以让你停下来。

# Make into Numpy array
n = np.array(im)

# Mask of magenta background pixels
bgMask =(n[:, :, 0:3] == [255,0,255]).all(2)

# DEBUG
Image.fromarray((bgMask*255).astype(np.uint8)).show()

在此输入图像描述

# Make a disk-shaped structuring element
strel = skimage.morphology.disk(13)

# Perform a morphological closing with structuring element
closed = skimage.morphology.binary_closing(bgMask,selem=strel)

# DEBUG
Image.fromarray((closed*255).astype(np.uint8)).show()

在此输入图像描述

If you are unfamiliar with morphology, Anthony Thyssen has some excellent noes worth reading here . 如果你不熟悉的形态,安东尼蒂森有一些值得一读的优秀化整为零这里

By the way, you could also use potrace to smooth the outline somewhat. 顺便说一下,你也可以使用potrace来平滑轮廓。


I had a bit more time today so here is a more complete version. 今天我有更多的时间,所以这里是一个更完整的版本。 You can experiment with the morphology disk sizes and floodfill thresholds according to your images till you find something tailored for your needs: 您可以根据图像尝试形态磁盘大小和填充阈值,直到找到适合您需求的方式:

#!/bin/env python3

from PIL import Image, ImageDraw
import numpy as np
import skimage.morphology

# Open the shirt and make a clean copy before we dink with it too much
im = Image.open('shirt.jpg')
orig = im.copy()

# Make all background pixels (not including Nike logo) into magenta (255,0,255)
ImageDraw.floodfill(im,xy=(0,0),value=(255,0,255),thresh=50)

# DEBUG
im.show()

# Make into Numpy array
n = np.array(im)

# Mask of magenta background pixels
bgMask =(n[:, :, 0:3] == [255,0,255]).all(2)

# DEBUG
Image.fromarray((bgMask*255).astype(np.uint8)).show()

# Make a disk-shaped structuring element
strel = skimage.morphology.disk(13)

# Perform a morphological closing with structuring element to remove blobs
newalpha = skimage.morphology.binary_closing(bgMask,selem=strel)

# Perform a morphological dilation to expand mask right to edges of shirt
newalpha = skimage.morphology.binary_dilation(newalpha, selem=strel)

# Make a PIL representation of newalpha, converting from True/False to 0/255
newalphaPIL = (newalpha*255).astype(np.uint8)
newalphaPIL = Image.fromarray(255-newalphaPIL, mode='L')

# DEBUG
newalphaPIL.show()

# Put new, cleaned up image into alpha layer of original image
orig.putalpha(newalphaPIL)
orig.save('result.png')

在此输入图像描述


As regards using potrace to smooth the outline, you would save new alphaPIL as a PGM format image because that is what potrace likes as input. 关于使用potrace来平滑轮廓,你可以将new alphaPIL保存为PGM格式图像,因为这就是potrace喜欢的输入。 So that would be: 那就是:

newalphaPIL.save('newalpha.pgm')

Now you can play around, oops I meant "experiment carefully" with potrace to smooth the alpha outline. 现在你可以玩了,oops我的意思是用potrace “仔细试验”以平滑alpha轮廓。 The basic command is: 基本命令是:

potrace -b pgm newalpha.pgm -o smoothalpha.pgm

You can then re-load the image smoothalpha.pgm back into your Python and use it on the last line in the putalpha() call. 然后,您可以将图像smoothalpha.pgm重新加载到Python中,并在putalpha()调用的最后一行使用它。 Here is an animation of the difference between the original unsmoothed alpha and the smoothed one: 这是原始未平滑的alpha和平滑的alpha之间差异的动画:

在此输入图像描述

Look carefully at the edges to see the difference. 仔细看边缘,看看差异。 You may want to experiment with resizing the alpha either to twice the size or half the size before smoothing to see what effect that has. 在平滑之前,您可能希望尝试将alpha的大小调整为两倍大小或一半大小以查看其具有的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM