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

Scale image with transparent background but convert background to black

Post by niyanwen »

Hi,

We are using scale image command, but for some specific image which background is transparent, it will set the background to black after scale command, could you help me why this happened?

command I am using:
convert original_image.png -resize 4500x5400! output_image.png

original image:
https://drive.google.com/file/d/195xebA ... sp=sharing

output image:
https://drive.google.com/file/d/1bcbqK0 ... sp=sharing

User avatar
fmw42
Posts: 26050
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 »

It works fine for me on IM 6.9.10.74 Q16 Mac OSX with libpng 1.6.36. Please always provide your IM version and platform when asking questions.

What version of ImageMagick are you using and on what platform and also what version of libpng. You can get the former from

Code: Select all

convert -version
and the latter from

Code: Select all

convert -list format
and look at the end of the line for PNG for the version.

I suspect either an old or buggy version of IM or an old version of libpng

User avatar
GeeMack
Posts: 738
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

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

Post by GeeMack »

niyanwen wrote:
2019-11-26T14:11:48-07:00
We are using scale image command, but for some specific image which background is transparent, it will set the background to black after scale command, could you help me why this happened?
I've had a few PNG images with backgrounds that went from transparent to black when converting. You should be able to get past that issue if you add "-background none" early in the command.

Code: Select all

convert original_image.png -background none -resize 4500x5400! output_image.png
Using your command with your sample image, the background goes to black for me with both IM 7.0.9-5 and 6.9.10-68 on Windows 10. It stays transparent by adding "-background none" as in the example above.

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 »

yeah

add "background none" works for me, any side effects will cause to have this?

User avatar
GeeMack
Posts: 738
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

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

Post by GeeMack »

niyanwen wrote:
2019-11-27T11:26:22-07:00
... any side effects will cause to have this?
That shouldn't affect anything else in most simple commands. If you use more complicated commands there are times you might need to set the background color to one thing, and later in the command set it to something else. Just make sure you do that ahead of the places in the command where you need it.

snibgo
Posts: 12648
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

The simple command...

Code: Select all

magick "SHIRT_ART_IMAGE B07KW1Q17J.png" -resize "4500x5400^!" x.png
... works for me with IM 7.0.8-64 and 6.9.9-50. I suppose the requirement to add "-background None" is a bug in newer versions.
snibgo's IM pages: im.snibgo.com

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 »

snibgo wrote:
2019-11-27T12:10:56-07:00
The simple command...

Code: Select all

magick "SHIRT_ART_IMAGE B07KW1Q17J.png" -resize "4500x5400^!" x.png
... works for me with IM 7.0.8-64 and 6.9.9-50. I suppose the requirement to add "-background None" is a bug in newer versions.
we are using older version (IM6 some version), and we can only use convert but not magick, so the bug exists in the old version looks like

User avatar
fmw42
Posts: 26050
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 »

Perhaps it is your version of libpng. If it is old, try upgrading.

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 »

fmw42 wrote:
2019-11-27T17:48:53-07:00
Perhaps it is your version of libpng. If it is old, try upgrading.
Hi,

we cannot upgrade ImageMagick easily, I am just wondering if add "background none" in the scale command, will it introduce more bug? or it is safe to add it?

User avatar
fmw42
Posts: 26050
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 »

Should be fine. But did you check your version of libpng?

Code: Select all

convert -list format
look at end of line for PNG

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,

looks like this issue randomly happen, sometimes it works sometimes not, since we have 30+ hosts is there a way to add it in the command to fix this issue eventually? for now I have already added "background none" in the command

User avatar
fmw42
Posts: 26050
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 »

On what platform is it failing? What version of ImageMagick? What version of libpng? Does it fail if you upgrade both ImageMagick and libpng to their most current versions?

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 »

the issue happens is add "-backgound none", for some image, after run the command, image becomes a whole white background
anyone knows how to fix it?

if I remove "-backgound none", this issue went away, but blackground issue exists for some images

I found this post and it said add -alpha remove can fix it, just want to confirm is this the right way? I got it work after adding this command
https://imagemagick.org/discourse-serve ... hp?t=22275

User avatar
fmw42
Posts: 26050
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 »

Please post your image and the exact command you used. Also please answer my questions. Are you converting from PDF or PNG? If PDF to PNG, then try

Code: Select all

convert -background none image.pdf -resize WxH image.png

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
Last edited by niyanwen on 2019-12-27T19:24:35-07:00, edited 1 time in total.

Post Reply