gm
gm copied to clipboard
How to specify extent background color?
According to the docs, extent's description is:
composite image on background color canvas image.
The usage is:
gm("img.png").extent([width, height, options])
How do you use this function? Where do you specify the background color of the image.
The problem I'm trying to solve is converting a PNG (with transparency) to a JPG. By default, the transparent color gets filled with black. I'm trying to figure out how to fill it with a different color and I'm assuming I need to composite the image on top of another blank color canvas before saving the output image.
Have you solved this problem ? Now I am confusing with this.
I have no idea how to solve this. The problem is when I convert a PNG with a transparent background to a JPG. The script fills in the transparent background with black by default. I can't figure out how to specify a different background color.
I use the GM with C++. As you said, finally I put the png on the top of another canvas with needed color. I use the composite function. And this sovled this problem. This solution based on that, in the Gm's command line to do this job: gm convert input.png -flatten output.jpg I seeked into the "convert" command source code, the flatten parameter deals with image sequences, but if with one image as input, the GM would new a blank canvas as its background.So, my conclusion is the "composite" way. I am not sure my conclusion right or not. I am poor in C++, and in English, sorry about that.
In the GM, there exit two functions, backgroundColor(Color) and matteColor(Color), I have tried both, but no help.
I've been struggling with this too. Finally realised you have to "flatten" the png
gm("img.png").background('red').flatten().toFormat('jpg');
But there is no flatten() function in GM's C++ usage.
FlattenImage() maybe? http://www.graphicsmagick.org/api/transform.html#flattenimage
Aha. This function is what I need. When I do this to a png image, I got a jpg file with white background color: image = FlattenImages(image,&exception); However, it's a c function, and the parameter image is also a MagickLib::_Image. I use the GM's C++ interface. So, there should be a class converting problem. And, what is more interesting is, this function is FlattenImages, not FlattenImage. The user manual make a mistake.
So how do you do this with this gm
plugin?
@Jakobud
gm("img.png").background('#FF0000').flatten().toFormat('jpg');
Hey, @rwky only flatten is documented on http://aheckmann.github.io/gm/docs.html
@mikemonteith toFormat method is not defined:
Exception in callback of async function: TypeError: Object [object Object] has no method 'toFormat'
@digz6666 I'm not sure where I got toFormat
from :worried:
It should be .setFormat('jpg');
to explicitly set the output format.