Preventing Semi-Transparent Pixels after Resizing

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
dfelder
Posts: 42
Joined: 2011-02-25T16:49:09-07:00
Authentication code: 8675308

Preventing Semi-Transparent Pixels after Resizing

Post by dfelder »

Hello,

I have a large PNG file with a transparent background: Image

This original art has NO semi-transparent pixels:

Image


I need to reduce it in size to 800 pixels wide. I have tried -resize & -thumbnail, but each time I get semi-transparent pixels around the image:

Image

In PhotoShop, one would call this "anti-aliasing", so I experimented all day with "+antialias" and "-sample" and a bunch of other things, but nothing renders the proper image.

The "-sample" command dramatically changes the image (note the jagged lines of the bottle relative to the original art):

Image

How do I take the reduce the original image so it still looks "smooth" on the inside, but still has no semi-transparent edge pixels?

NOTE: Another solution would be to either delete all pixels that are not 100% opaque OR duplicate the image on top of itself MANY times. (If this is the easiest way, can someone show me how to remove the pixels or duplicate?)

Thanks for any insight.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Preventing Semi-Transparent Pixels after Resizing

Post by fmw42 »

convert original.png -sample 800x result.png

Image

is the best you are going to do. It does not have partial transparency and the edges are either transparent or red (no mixed colors). No matter what you do the edges will be somewhat jagged without the partial transparency.

IM 6.6.9.5 Q16 Mac OSX Tiger
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Preventing Semi-Transparent Pixels after Resizing

Post by anthony »

If it is just semi-transparency... Threshold it

Code: Select all

  -channel A -threshold 50% +channel
This is demonstrated in GIF transparency handling examples
http://www.imagemagick.org/Usage/formats/#boolean_trans
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
dfelder
Posts: 42
Joined: 2011-02-25T16:49:09-07:00
Authentication code: 8675308

Re: Preventing Semi-Transparent Pixels after Resizing

Post by dfelder »

I guess I will settle with what I got. Thanks!

BTW, what does:

-channel A -threshold 50% +channel

do? Zap away any semi-transparent pixels? Force them to 100% opaque? Given the business I'm in, I guess I'd like to be able to do both. I looked at the link and the description wasn't very clear.

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

Re: Preventing Semi-Transparent Pixels after Resizing

Post by fmw42 »

dfelder wrote:I guess I will settle with what I got. Thanks!

BTW, what does:

-channel A -threshold 50% +channel

do? Zap away any semi-transparent pixels? Force them to 100% opaque? Given the business I'm in, I guess I'd like to be able to do both. I looked at the link and the description wasn't very clear.

Thanks.
-channel A selects only the alpha channel (from the rgba channels), then converts all shades of gray (8-bit values) to either black or white at mid gray using -threshold 50, then +channel re-enables all the other channels to continue processing or write your output.

However, if you have used -resize rather than use -sample on your image, you will have mixed some of the red with background under the transparency (whatever color you have, probably white or black, unless it is red also), so that the edge colors are changed. Then when you threshold the alpha channel, some of these pixels will be hidden and others will show as off-color from pure red.

I don't see any way to get around the aliasing. The best you can do is use -sample to make your image smaller, if you don't want the anti-aliasing in your alpha channel. Alternately, you would need to use -threshold 0 in the above, so that any partial transparent alpha pixel becomes opaque, thus you cover over any of the mixed red color. But it will be aliased as well. You can try both if you want and see which you prefer.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Preventing Semi-Transparent Pixels after Resizing

Post by anthony »

May be the image can be rendered on white and black backgrounds. We have a perfect background removal (to transparency) if we have a copy of the image on two different known opaque background colors. When the backgroung color effects are removed, you can threshold transparency any way you like.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
dfelder
Posts: 42
Joined: 2011-02-25T16:49:09-07:00
Authentication code: 8675308

Re: Preventing Semi-Transparent Pixels after Resizing

Post by dfelder »

To FMW42's response...

If I take any image with anti-aliasing and apply: -channel A -threshold 0% +channel imagein imageout, it will remove all anti-aliased (semi-transparent) pixels?

If I take any image with anti-aliasing and apply: -channel A -threshold 100% +channel imagein imageout, it will make all anti-aliased (semi-transparent) pixels 100% opaque?

To anthony...

I don't understand what this means: IMay be the image can be rendered on white and black backgrounds.

I like the concept of flattening the image to both a black & white background then performing an operation on it. Logically, it seems I could identify those edge pixels. I'm not sure if "IMay" is a reference to Image Magick or something else. (snicker)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Preventing Semi-Transparent Pixels after Resizing

Post by fmw42 »

If I take any image with anti-aliasing and apply: -channel A -threshold 0% +channel imagein imageout, it will remove all anti-aliased (semi-transparent) pixels?

If I take any image with anti-aliasing and apply: -channel A -threshold 100% +channel imagein imageout, it will make all anti-aliased (semi-transparent) pixels 100% opaque?
Not exactly,

convert imagein -channel A -threshold 0 +channel imageout

will make all semi-transparent pixels opaque

and


convert imagein -channel A -negate -threshold 0 -negate +channel imageout

will make all semi-transparent pixels fully transparent.

My mistake about -threshold 0% and -threshold 100% as the latter will make all pixels transparent and the percent is wrong in the former
Last edited by fmw42 on 2011-04-26T17:59:20-07:00, edited 1 time in total.
dfelder
Posts: 42
Joined: 2011-02-25T16:49:09-07:00
Authentication code: 8675308

Re: Preventing Semi-Transparent Pixels after Resizing

Post by dfelder »

Great Stuff, guys! Thanks for the input. I ended up using the -sample command because we simply cannot handle semi-transparent pixels in our image rendering.

I successfully scripted--using VBA--the automation of web art for our shop where we make print-on-demand t-shirts! Woohoo!

First, the very simple code (I'm posting it because it's virtually impossible to find any VBA examples here):

Code: Select all

ObjMi.Convert "-profile", "C:\Program Files\Adobe\ColorMatchRGB.icc", myimagename, "-sample", "800x", "PNG32:C:\Web Art\" & "web_" & Left(myimagename, InStr(actname, ".") - 1) & ".png"
DoEvents
Note that this converts a CMYK transparent PSD to an RGB transparent PNG, hence the color profile...

Next, the results at our POD shop, Garment Deli...this particular client is the Dikkers Shop

You can see how the bottle is still a little jagged, but we think it's sufficient.

BTW, I'm sure everyone uses IM differenly; but, I must say I LOVE this tool. Once you get the hang of it, it's amazing what it can do. We thought we were cool when we figured out PhotoShop Actions & Scripting. Doing this "behind-the-scenes" is way better and it's dramtically improved our processes. We currently use IM for art measurement, resolution validation, thumbnailing, and now web art creation. With the ability to leverage IM through a VBA library, the options seem unlimited.

Cheers!
Post Reply