Quantcast

PIL Convert Alpha-transparent image to non-transparent?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

PIL Convert Alpha-transparent image to non-transparent?

Chander Ganesan-3
Hi Folks,

I've done quite a bit of searching on Google for this and have yet to
find a working result, so hopefully someone here will know...

I've got an image that has alpha transparency, it has a non-white
background color (that is transparent), and several pixels are
"partially" transparent.  If I take the image and convert it over from
RGBA to RGB, the text in the image looks "blocky".

I need to convert the image to a non-transparent one (so I can pass it
into tesseract for OCR) but I cannot seem to do so without loosing
quality of the text in the image.  Is there a standard way to do this?  
I've tried the following (all to no avail):

- Iterate over the pixels and find those that are more than 50%
transparent and convert them to white (ends up with a dithered image)
- Use Paste onto a white background that has no transparency, and then
convert to RGB (no luck there)
- Use Image.blend to attempt to blend with a white background (at
various levels), no luck there.

Any help - or pointers in the right direction - would be much appreciated!

Thanks!

chander

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PIL Convert Alpha-transparent image to non-transparent?

Donn-11
I'm no PIL expert, but some thoughts:

> - Iterate over the pixels and find those that are more than 50%
> transparent and convert them to white (ends up with a dithered image)
Why not choose a range of gray values white to black, say in steps of 10%?

> - Use Paste onto a white background that has no transparency, and then
> convert to RGB (no luck there)
That's a good plan. Did you display (img.show() )the image to see the
effect of the paste? Perhaps there is some mistake in the operation.

> - Use Image.blend to attempt to blend with a white background (at
> various levels), no luck there.
Ah, ok, good plan again. Still, check that image out visually.

I don't know tesseract, but perhaps closer reading of the docs will
reveal something you are missing in re the RGB format.

Another idea would be to try using Gimp automation, employing Gimp as
your muscle.

\d
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PIL Convert Alpha-transparent image to non-transparent?

Nils de Reus
In reply to this post by Chander Ganesan-3
Dear Chander,

I am intrigued, because at least some of the methods you tried should have worked. Could you make one of these images available for testing? I would be interested to have a look at what is happening.

Best regards,
    Nils


________________________________________
Subject: [Image-SIG] PIL Convert Alpha-transparent image to non-transparent?

I've got an image that has alpha transparency, it has a non-white
background color (that is transparent), and several pixels are
"partially" transparent.  If I take the image and convert it over from
RGBA to RGB, the text in the image looks "blocky".

I've tried the following (all to no avail):

- Iterate over the pixels and find those that are more than 50%
transparent and convert them to white
- Use Paste onto a white background that has no transparency, and then
convert to RGB
- Use Image.blend to attempt to blend with a white background (at
various levels)
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Loading...