best command line options for jpg to transp png image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
michaelmuller

best command line options for jpg to transp png image

Post by michaelmuller »

I am using IM in an application for a t-shirt screen printing company where people can upload their artwork and use it in a neat flash tool with clipart, text, etc.

IM does a great job of making the white transparent, making thumbnails, and converting all images to PNG so they'll work in the flash program.

The issue I'm having now is when someone uploads a JPG file with lots of off-white noise. You can really see the speckles and the artifacts that the compressed JPG format generates, and when the white is made transparent and you put the image over a solid color like red you see all the extra dots. It looks pretty bad.

So, I've been trying all kinds of switches on IM, such as these...

-despeckle -noise n -white-threshold n% -fuzz n

...and they either do nothing or make the image look really terrible.

I'm hoping some IM experts out there will give some good advice on the best switches to use for this problem. I'm sure many of you have bumped into the same problem. If not, here's a sample file:

http://www.silverscreendesign.com/Custo ... s/mont.jpg

convert -transparent white [path]\mont.jpg [path]\mont.png

Any advice would be welcomed.

Thanks,

Mik
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: best command line options for jpg to transp png image

Post by fmw42 »

easy. just add -fuzz XX%. that will find all pixels that are within XX% of white and make them transparent

convert mont.jpg -fuzz 15% -transparent white mont_w15.png

You may have to use trial and error to get the best XX% for your image.

Note, IM 6.5.2-4 will not produce a transparent png from the above command --- bug. So I tested with a gif.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: best command line options for jpg to transp png image

Post by anthony »

Make sure you give the -fuzz BEFORE the operation using it!

Also if you plan to print the JPEG image onto non-white T-shirts, you may have to look at the advanced semi-transparency restoration techniques to get the edge colors correct. Without it edge pixel may get a white 'halo' around it. though that can be a interest effect for T-shirts too!

See IM Examples Channels and Masks, Transparency Restoration and Masking...
Masking with Anti-Aliased Edges
http://www.imagemagick.org/Usage/channe ... antialised

The specific is example is with a non-white background, but should work even better with a solid background color, such as white.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
michaelmuller

Re: best command line options for jpg to transp png image

Post by michaelmuller »

Anthony,

Hmmm... seems pretty cool. I'll try it out tomorrow. If it doesn't work out, are you available for a consulting? People send all kinds of files on all color backgrounds, and we need a methodology for all contingencies.

Mik

for fuzz in 01 03 06 28 32 34; do \
convert diff_mask.png -fill blue -fuzz $fuzz% \
-bordercolor black -border 1x1 -floodfill +0+0 black \
-shave 1x1 diff_mask_$fuzz.png; \
done
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: best command line options for jpg to transp png image

Post by anthony »

I doubt their is a technique that will work for all contingies, but if this is programmed into a script it should be able to handle most cases. Just run script with different fuzz factors until you get a acceptable result.

To automate it further would require the program to try multiple fuzz factors and count the pixels each mask masks to fine appropriate limits.

What color I also have ideas about how to better select the color for the semi-transparent pixels, based on the color of the fully-opaque pixels. It is something that was only worked out recently and has not made it into IM examples yet.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: best command line options for jpg to transp png image

Post by fmw42 »

Anthony wrote:See IM Examples Channels and Masks, Transparency Restoration and Masking...
Masking with Anti-Aliased Edges
http://www.imagemagick.org/Usage/channe ... antialised

The specific is example is with a non-white background, but should work even better with a solid background color, such as white.
Nice example. Very intriguing.
michaelmuller

Re: best command line options for jpg to transp png image

Post by michaelmuller »

Regarding transparency again, can I pass a range for white? Currently I'm using...

-transparent white

...but wish to be able to do something like...

-transparent ffffff|eeeeee

... and IM would determine what the background color based on this range.

Or, perhaps there's a better method? I was thinking that I could find the top-left-most pixel color and set that as the background, but it's possible the image has a border, so... maybe not.

What other techniques should I try?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: best command line options for jpg to transp png image

Post by fmw42 »

you can not do that for a range of colors, but you can use -fuzz XX% to get XX% close to white

convert image -fuzz XX% -transparent white result

just figure out the percent difference between FFFFFF and EEEEEE

for example see http://www.yellowpipe.com/yis/tools/hex ... verter.php
which says that EEEEEE is graylevel 238 out of 255

So the percent difference = 100*(255-238)/255 = 6.66%
michaelmuller

Re: best command line options for jpg to transp png image

Post by michaelmuller »

Hmm... I'm using 10% right now, so I should be ok.

Thanks.
michaelmuller

Re: best command line options for jpg to transp png image

Post by michaelmuller »

Ok, this is working a little better, though some images get quite "chunky" when the original was anti-aliased.

Now I need to know how to set an image to no longer have a transparent color. I've tried this but it doesn't work.

-transparent none
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: best command line options for jpg to transp png image

Post by fmw42 »

-fill whatevercolor -opaque none

or

-background whatevercolor -flatten
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: best command line options for jpg to transp png image

Post by anthony »

michaelmuller wrote:though some images get quite "chunky" when the original was anti-aliased.
You just have to remember that -transparent and -opaque are straight Boolean color replacement. That is it replaces the color or not, with no inbetween. as such you loose anti-aliasing.

I have made an initial script for the anti-aliasing background removal
http://www.imagemagick.org/Usage/scripts/bg_removal

It is still a little rough, but combines all the separate steps into a single convert command, with options to change how the masking should be performed.

At the moment the replacement semi-transparent color is just black. Which is not always the case. I have been thinking of alternatives to try to improve that, but noting definitive at this point.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply