How to use image command with transparency / alpha blending?

(Dan) #1

I’m using G’MIC to make montages of photos for a photo book. I have written a custom command that accepts a list of images to arrange on a page. It uses the image command to place each one at the desired place on the page.

Some of my images have transparency or alpha and I’d like to overlay them based on that transparency. Do the opacity-related parameters of the image command provide that capability?

I think the following steps would be equivalent:

  1. duplicate the background
  2. crop the duplicate background to the area being overlaid
  3. blend the cropped piece and the image with transparency/alpha
  4. image the blended piece back into the background page

OK, in retrospect, that’s not too hard. Is that just the correct way to do this in G’MIC?

(G'MIC staff) #2

Yes, you have to supply the last channel of your RGBA image to the image command, in order to deal with opacity.
This example below is worth a thousand words:

$ gmic lena.bmp milla_transp_RGBA.png +channels[-1] 100% +image[0] [1],20%,20%,0,0,1,[2],255

gives you the following stack of images:

(dumb) #3

Also, if you’re gonna be doing anything which doesn’t play nicely with alpha channels (which usually means it’s for RGB images), use split_opacity or consider sh if you know what you’re doing to make your command run a lot faster.

(Dan) #4

Thank you, that would be a good example to add to the documentation page also! Two follow-up questions:

  1. What does the 100% parameter do on the channels command and how does it select the alpha channel? Given an RGBA image, would 3 also work instead of 100?

  2. What does the value of 1 mean for the opacity parameter of the image command?

(G'MIC staff) #5
  • 100% means ‘the last channel’, and it is equivalent to 3 indeed for color images (but if you have a grey+alpha image with 2 channels, 100% will also be valid).
  • You can still apply a ‘global’ alpha, even with an additional alpha mask. Try 0.5 for instance to make the opaque pixels of the copied RGBA image semi-transparent as well in the destination image.

(Dan) #6


The one last bit of kinda ugly code in my custom command resizes an image to fit an arbitrary rectangle, with background-colored bars on the left/right or top/bottom. It does a resize with dirichlet boundary condition, and then draws rectangles of the desired background color. Is there a more elegant solution for this?

Also, any way to help with documentation? I made heavy of use of reference.shtml in doing this project, and would be happy to contribute if I could.

(G'MIC staff) #7

Not sure what you meant. Could you show just an example of resulting image and an input, so we can analyze the problem more precisely ?

Sure, the reference documentation is generated by G’MIC directly from the source file gmic_stdlib.gmic. Anyone is free to contribute by correcting / adding new documentation in this file (pull requests on framagit or github would be appreciated :slight_smile: ).

Here’s a link to the development version of this file :

Reference documentation is generated from lines starting with #@cli, for instance, command flower has this small doc header :

#@cli flower : _amplitude,_frequency,_offset_r[%],_angle,_center_x[%],_center_y[%],_boundary_conditions={ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror}
#@cli : Apply flower deformation on selected images.
#@cli : Default values: 'amplitude=30', 'frequency=6', 'offset_r=0', 'angle=0', 'center_x=center_y=50%' and 'boundary_conditions=3'.
#@cli : $ image.jpg flower ,

It’s then easy to add more description and more examples if needed.

(Dan) #8

Here’s an example: I have a spot on my photobook page that is 750x600 pixels, and I’d like to display the tiger image there, which has a 3x2 aspect ratio. Resizing it to 750x600 adds pixels to the image, which need to be filled in with a desired background color (in this example white).

gmic sample tiger
resize 750,600,100%,100%,0,0,0.5,0.5
rectangle 0,0,749,49,255,255,255
rectangle 0,550,749,599,255,255,255

It is also nice to align it to either the top:

gmic sample tiger
resize 750,600,100%,100%,0,0,0,0
rectangle 0,500,749,599,255,255,255

or the bottom:

gmic sample tiger
resize 750,600,100%,100%,0,0,1,1
rectangle 0,0,749,99,255,255,255

frame_xy 0,50,255,255,255 does the same as the first example, but a different approach would be needed to align to the top or bottom.

(G'MIC staff) #9

There is a trick I use sometimes for that kind of resizing.
If the color has the same values for R,G,B (e.g. white), then you can do something as :

$ gmic sp tiger - 255 r 1024,1024,1,3,0,0,0.5,0.5 + 255

and if the color has different components, then:

$ gmic sp tiger color=[255,128,64] - \'\$color\' r 1024,1024,1,3,0,0,0.5,0.5 + \'\$color\'

(backslashes are added to pass the single quote and dollar to the G’MIC interpreter through the bash call, but are not necessary if the command is written in a G’MIC command file).

(Dan) #10

That makes me smile, thank you!