gm icon indicating copy to clipboard operation
gm copied to clipboard

How to specify extent background color?

Open Jakobud opened this issue 9 years ago • 13 comments

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.

Jakobud avatar Jun 19 '15 17:06 Jakobud

Have you solved this problem ? Now I am confusing with this.

tingfengainiaini avatar Aug 13 '15 12:08 tingfengainiaini

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.

Jakobud avatar Aug 13 '15 14:08 Jakobud

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.

tingfengainiaini avatar Aug 17 '15 12:08 tingfengainiaini

In the GM, there exit two functions, backgroundColor(Color) and matteColor(Color), I have tried both, but no help.

tingfengainiaini avatar Aug 17 '15 12:08 tingfengainiaini

I've been struggling with this too. Finally realised you have to "flatten" the png

gm("img.png").background('red').flatten().toFormat('jpg');

mikemonteith avatar Aug 18 '15 10:08 mikemonteith

But there is no flatten() function in GM's C++ usage.

tingfengainiaini avatar Aug 19 '15 08:08 tingfengainiaini

FlattenImage() maybe? http://www.graphicsmagick.org/api/transform.html#flattenimage

mikemonteith avatar Aug 19 '15 08:08 mikemonteith

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.

tingfengainiaini avatar Aug 20 '15 03:08 tingfengainiaini

So how do you do this with this gm plugin?

Jakobud avatar Aug 20 '15 14:08 Jakobud

@Jakobud

gm("img.png").background('#FF0000').flatten().toFormat('jpg');

mikemonteith avatar Aug 20 '15 15:08 mikemonteith

Hey, @rwky only flatten is documented on http://aheckmann.github.io/gm/docs.html

kapouer avatar Sep 24 '15 17:09 kapouer

@mikemonteith toFormat method is not defined:

Exception in callback of async function: TypeError: Object [object Object] has no method 'toFormat'

digz6666 avatar Dec 06 '15 05:12 digz6666

@digz6666 I'm not sure where I got toFormat from :worried: It should be .setFormat('jpg'); to explicitly set the output format.

mikemonteith avatar Dec 07 '15 10:12 mikemonteith