Scale image with transparent background but convert background to black

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?".
niyanwen
Posts: 81
Joined: 2019-05-23T16:19:12-07:00
Authentication code: 1152

Re: Scale image with transparent background but convert background to black

Post by niyanwen »

Hi,
I am not convert PDF to PNG, I am converting PNG to PNG

Image Magick version I use is IM6.9.4_1 ( 6.9.4_1.204869.0 ), the other stuff I will check later, but since we work in industry, this iM version is shared by hundreds of team, so I cannot switch IM version or update other things you mentioned, so I think what I can do is only modify the command
Also, for the image, since the image is customer design, I cannot upload here directly

the command I am using is
convert original_image.png -resize 4500x5400! -background none output_image.png

this fix set the image to black background issue, but we start facing for some specific image, we got whole white background

the way I try is
convert original_image.png -resize 4500x5400! -background none -alpha remove output_image.png
this looks like fixing the issue I mentioned above, but is it a right way?

let me know anything you need

niyanwen
Posts: 81
Joined: 2019-05-23T16:19:12-07:00
Authentication code: 1152

Re: Scale image with transparent background but convert background to black

Post by niyanwen »

from what I found is this issue only happens when original image size is same as the target size,
it will set background to black, after adding background none, it will set background to whole white box

so the bug is iM cannot scale this kind of image same as its original size

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

Re: Scale image with transparent background but convert background to black

Post by fmw42 »

I do not understand. Do you want your result to be transparent or black.

If transparent, try

Code: Select all

convert original_image.png -background none -resize 4500x5400! PNG32:output_image.png 
If you want it black, then try

Code: Select all

convert original_image.png -background black -alpha background -alpha off -resize 4500x5400! PNG32:output_image.png 
or

Code: Select all

convert original_image.png -background black -flatten -resize 4500x5400! PNG32:output_image.png 
Your version of ImageMagick is ancient. You should consider upgrading. Also you did not answer my question about your version of libpng. Perhaps it is causing the issue if it is old.

niyanwen
Posts: 81
Joined: 2019-05-23T16:19:12-07:00
Authentication code: 1152

Re: Scale image with transparent background but convert background to black

Post by niyanwen »

Hi,

I want the background to be transparent, but IM auto set it to black, then adding "background none" command it auto set it to a whole white box, this is the issue, if you can understand

so I found the bug is for convert scale command(convert resize), if using target size as same as original image size, this bug happened, if not looks like this bug went away, so I assume there is a bug there

as I said I work in industry, I cannot upgrade IM version or update libpng, so even the problem is here, it won't help, hope you can understand

I will try adding PNG32: to see if it works

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

Re: Scale image with transparent background but convert background to black

Post by fmw42 »

Try the following and let us know if that works or not.

# create transparent image:

Code: Select all

convert logo: -transparent white logot.png
# resize

Code: Select all

convert logot.png -resize 50% logot2.png
or
convert logot.png -resize 50% PNG32:logot2.png
or
convert logot.png -background none -resize 50% logot2.png
or
convert logot.png -background none -resize 50% PNG32:logot2.png
Are all 4 of the output results transparent. If not, which ones are not?

If all 4 are transparent, then you may need to provide an example input image that fails for the developers to test. You can send that privately to them, if you want.

Post Reply