GMIC CLI outputting png vs jpg file formats

I’m a new user trying to learn G’MIC CLI. I am using G’MIC CLI v2.9.0 on Windows 7 and pulling my hair out this morning trying to figure out how to output an image to a png file format. After much reading and trial and error, I believe it has something to do with the alpha channel that exists in the png file.

For example, when manipulating a jpg image this command works perfectly resulting in a file that is 10 mb in size as expected.:

gmic C:\TEST\Test.jpg adjust_colors 0,25 output C:\TEST\Test_J.jpg

Here is the original input jpg file and the file output file generated

Attempting the same with a png version produces a new png image file, however the file appears to be empty (or perhaps containing only the alpha channel). The resulting file size is only 180 kb:

gmic C:\TEST\Test.png adjust_colors 0,25 output C:\TEST\Test_P.png

Here is the original png file. Attempting to post the output file inserts a large empty space in this message and I therefore removed from the post.

I surmise that I’m missing a command and need to perhaps blend all the layers in the png or remove the alpha channel before the output command. Unfortunately I’m new to these commands and not sure if I’m on the right track or have missed something really simple.

Any help would be greatly appreciated.

Give us file examples. Drag-and-drop.

Things to keep in mind

The stats of the image matter before saving. Often beginners have trouble with pixels being outside of the file type range (e.g., 0-255), which would be clipped on output. Floating point numbers would be rounded in JPGs and PNGs. Usually, I do a quick normalize 0,255 round before output. If you want to be precise, div 255 at the beginning of the command and mul 255 at the end, or use whatever max value you want or need.

G’MIC doesn’t colour manage nor is it super aware of alpha channels. The interpreter considers the input as a collection of values and that is about it. This is freeing and can be confusing at the same time. E.g., commands such as adjust_colors assume 0-255 and other commands assume sRGB. The only way of knowing what the command does is reading the underlying code here: https://raw.githubusercontent.com/dtschump/gmic/master/src/gmic_stdlib.gmic.

There is also code here: gmic-community/include at master · dtschump/gmic-community · GitHub. You asked me earlier what my role is with G’MIC. I am just a fan who happens to write community commands (afre.gmic is listed there). A simple gmic update will make everything available, provided that you have the latest stable version.

Thanks for the tip about out of range pixels and the link to the underlying code. I’ll take a look as the command reference doesn’t offer any detail on this. Perhaps by reading the code I can figure it out.

Update… I loaded the Test.png image into Gimp and removed the alpha channel, then exported as Test_no_alpha.png. Curiously the command works perfectly on the new png file. Therefore think the issue I’m experiencing has to do with the alpha channel. Here’s the Test_no_alpha.png file that works.

Hello @Michael_H,
When you import your .png file into G’MIC, it is imported as a 4-channel images.
In G’MIC, when you export a 4-channel image to a JPG, it assumes it must be a CMYK-encoded JPG.
That’s why you need to do something between the loading and saving steps.

Command to_rgb will discard the alpha-channel and force the image to have only 3 channels, and it is probably what you need here:

$ gmic input.png to_rgb o output.jpg

Note that you may also experiment the “classical” 16bits/8bits PNG to JPEG issue, which happens when you read a 16bits .png file, leading to values in range [0,65535], and try to save it as a JPEG file (which expect values in range [0,255]).
In that case, gmic input.jpg div 257 o output.jpg is the way to go.

Thank you, the ‘to.rgb’ solution solved my issue. I had searched looking for a command related to ‘channels, alpha, layers, etc’. It never occurred to me to look for ‘rgb’. In hindsight it makes perfect sense and see there’s also a command to add a alpha channel ‘to.rgba’.

This is an amazing forum. Sincerely appreciate everyone’s willingness to help!

1 Like