# PIL (python imaging library) or Mathematics help both appreciated

A

#### amine

well, here is the background. I have images of objects (cars,
clothes, ...) with a white background in most of the cases

I have to build a function with PIL that takes away the background.
it seems simple, just look for the "white" and make it transparent but
the problem is in reality much more complex:
1) the image could contain some white inside the object (e.g. shoes
with some white in between straps)
2) there are often pixels that are part of the background but have a
colour different from white which leaves a few points throughout the
image

to be more concrete:
here is a bit of code of what i've made so far

def transparent(im):
#i take all the images of the pixel
pixels = list(im.getdata())
#i convert the image into png
if im.mode != 'RGBA':
im = im.convert('RGBA')
#i create a new image with the same dimension with one unique layer
for transparency
width , height = im.size
white = { 'r' : 255 , 'g' : 255, 'b' : 255 }
#i browse the pixels of the image
for y in range(height):
yp = y * width
for x in range(width):
xy = yp + x
pix = pixels[xy]
#the color of the current pixel
c = { 'r' : pix[0] , 'g' : pix[1], 'b' : pix[2] }
#i calculate the vectorial distance between the current color and
the color white
d = sqrt( pow((c['r']- white['r'] ),2) + pow((c['g'] - white['g']),
2) + pow((c['b'] - white['b']),2) )
if d < 5 :
#if it is more or less white, i make the pixel transparent
else:
#otherwise i show the color

after the layer of transparency of the new image is done, the
algorithm works generally fine except there are some small but
noticeable quality issues. i am just asking myself if there is maybe
not a better approach either in terms of algorithms or even
mathematics or maybe refine the algorithm that i've create. anything
would help.

i know the function will not be 100% precise but I just hope the image
can be presentable and that the image is homogenous.

A

#### Arnaud Delobelle

Well not much maths in my answers but...

well, here is the background.  I have images of objects (cars,
clothes, ...) with a white background in most of the cases

I have to build a function with PIL that takes away the background.
it seems simple, just look for the "white" and make it transparent but
the problem is in reality much more complex:
1) the image could contain some white inside the object (e.g. shoes
with some white in between straps)

A simple solution would be to start with a transparent pixel in the
top left corner say, then scan the image from left to right (line by
line): if the current pixel is white (or pale enough) and has a
transparent pixel above it or to its left, then make it transparent.

I remember when I was a kid playing graphical adventure games on my
C64, you could actually see this happening as the picture was being
built on the screen (as a floppy could only contain around 160k, you
couldn't store bitmaps unless you had very few pictures).
2) there are often pixels that are part of the background but have a
colour different from white which leaves a few points throughout the
image

What you're doing is fine I think: calculate the distance to white.

HTH

J

#### Jorgen Grahn

well, here is the background. I have images of objects (cars,
clothes, ...) with a white background in most of the cases

I have to build a function with PIL that takes away the background.
it seems simple, just look for the "white" and make it transparent but
the problem is in reality much more complex:
1) the image could contain some white inside the object (e.g. shoes
with some white in between straps)
2) there are often pixels that are part of the background but have a
colour different from white which leaves a few points throughout the
image

to be more concrete:
here is a bit of code of what i've made so far ....
after the layer of transparency of the new image is done, the
algorithm works generally fine except there are some small but
noticeable quality issues. i am just asking myself if there is maybe
not a better approach either in terms of algorithms or even
mathematics or maybe refine the algorithm that i've create. anything
would help.

i know the function will not be 100% precise but I just hope the image
can be presentable and that the image is homogenous.

How about calling on the Gimp? I haven't done it, but I seem to recall
it has a Python interface. Hopefully that means you can use its
algorithms from a standalone Python program, with no GUI.

I guess in the Gimp you'd use a fuzzy "select a continuous region
around this pixel with approximately this color" algorithm. Then you'd
use another one which replaces the "whiteness" with transparency.

/Jorgen

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

### Members online

No members online now.