Scaling and Converting a .png to .jpg

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
SoulBeaver
Posts: 1
Joined: 2016-01-19T06:02:20-07:00
Authentication code: 1151

Scaling and Converting a .png to .jpg

Post by SoulBeaver » 2016-01-19T06:10:48-07:00

Hello everyone!

Lest my question seem too innocuous, this is the workflow I'm trying to achieve:

Image

I take a .png with a transparent background, fill the background with a new color, resize the image and convert it to a .jpg. This gives me the following command in IM:
convert -resize 800x1200 -background #000000 -alpha Background -quality 100% src.png dest.jpg
The result is this:

Image

I've tried stripping away everything and perform a simple resize and conversion in two steps:
convert -resize 800x1200 src.png src_tmp.png
convert -quality 100% src_tmp.png dest.jpg
But that still results in:

Image

Source image here: Image

Further Notes:

1. I have tried several filters including triangle, Lancsoz, LancsozSharp and a few others
2. I've tried changing the colorspace: convert -colorspace RGB -resize 800x1200 -colorspace sRGB src.png dest.jpg

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

Re: Scaling and Converting a .png to .jpg

Post by snibgo » 2016-01-19T06:30:06-07:00

"-alpha Background" changes fully-transparent pixels to black, which is only part of the work you want. You should also use the correct syntax (read the input, do the processing, write the output). So we have:

Code: Select all

convert dehEQJ3.png -resize 800x1200 -background black -flatten out.jpg
snibgo's IM pages: im.snibgo.com

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

Re: Scaling and Converting a .png to .jpg

Post by GeeMack » 2016-01-19T07:07:33-07:00

SoulBeaver wrote:I take a .png with a transparent background, fill the background with a new color, resize the image and convert it to a .jpg. This gives me the following command in IM:
convert -resize 800x1200 -background #000000 -alpha Background -quality 100% src.png dest.jpg
Using IM 6.9.3-1 Q16 x64 on Windows 7 64, I get the result from your first example by cloning the image, making the clone all black, compositing the image over the black, then resizing it to 800x1200 after it's on the black background, something like this...

Code: Select all

convert dehEQJ3.png ( +clone -flatten -fill black -colorize 100 ) +swap -composite -resize 800x1200 -quality 100 dest.jpg
It looks like you have to flatten that clone in order to get the entire rectangle black, otherwise the "-colorize" only makes the oval center black.

EDITED TO ADD: Looks like snibgo gave you the simpler method while I was writing my reply! :)

Post Reply