Artifacts when converting PNG to JPG

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
Shion
Posts: 13
Joined: 2013-11-26T04:18:21-07:00
Authentication code: 6789

Artifacts when converting PNG to JPG

Post by Shion »

I am currently working on automatically converting PNG to JPG files.

The two files I attached show the original PNGs.
ImageImage

I am trying to make a similar JPG. The simplified commands I use are as follows:

Code: Select all

convert.exe "a.png[0]" "1.jpg"
convert.exe "a.png" "2.jpg"
convert.exe "a.png" -flatten "3.jpg"
The outputs are (for both PNGs mentioned above):
ImageImageImage

Only output #3 is correct. I would expect that the output for at least the first command should be the same as for the third?
The problem I am facing is that I can't use the command #2 or #3 because I remember having other problems with different PNGs. (If this is really relevant, I will search again for those and post them here).
So I am stuck with using version #1.

Can anybody point me in the correct direction to which command I should really be using for PNGs?
Thanks in advance!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Artifacts when converting PNG to JPG

Post by snibgo »

You haven't attached the original PNGs, but JPG versions. So we can't see what the PNG actually contains.

PNG files contain only one image, so "[0]" should make no difference.

You might try "-alpha off".
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Artifacts when converting PNG to JPG

Post by fmw42 »

JPG does not support transparency which is likely in your PNG. So you must use the alpha channel to put some background color (looks like you want white) under the transparency using -background color -flatten or -background color -alpha background. The default background color is likely white, which is why your third example worked. You may be able to remove the alpha channel with -alpha off, if the background under the alpha channel in your base image is already white.
Shion
Posts: 13
Joined: 2013-11-26T04:18:21-07:00
Authentication code: 6789

Re: Artifacts when converting PNG to JPG

Post by Shion »

@snibgo:
The PNGs are the first 2 files. They are there, I double-checked it.

@fmw42:
Thanks for your suggestion.
Actually, it doesn't make any difference. I just stripped down the example to the simplest possible command.
My real command looks like this, and it doesn't work either:

Code: Select all

"input.png[0]" -strip -density 300x300 -resize "3508x3508>" -quality 95 -background white -alpha off "output.jpg"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Artifacts when converting PNG to JPG

Post by fmw42 »

The area under the transparency in your original image has color regions and is not pure white. So turning alpha off will show those areas. You need to make the area under the transparency white, so do either

Code: Select all

convert a_removed.png -background white -alpha remove result.jpg
or

Code: Select all

convert a_removed.png -background white -flatten result.jpg
Shion
Posts: 13
Joined: 2013-11-26T04:18:21-07:00
Authentication code: 6789

Re: Artifacts when converting PNG to JPG

Post by Shion »

Dear fmw42:
This seems to solve the problem in all my test-cases.
I will try to reprocess all my images (some thousand) and look if all work.

EDIT: Yep, this works!

Thanks a lot
Post Reply