Page 1 of 1

Artifacts when converting PNG to JPG

Posted: 2014-09-16T03:27:12-07:00
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!

Re: Artifacts when converting PNG to JPG

Posted: 2014-09-16T08:30:52-07:00
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".

Re: Artifacts when converting PNG to JPG

Posted: 2014-09-16T10:21:57-07:00
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.

Re: Artifacts when converting PNG to JPG

Posted: 2014-09-16T22:55:38-07:00
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"

Re: Artifacts when converting PNG to JPG

Posted: 2014-09-17T09:54:56-07:00
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

Re: Artifacts when converting PNG to JPG

Posted: 2014-09-18T00:28:44-07:00
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