Smart cropping with afre



Yet another exercise. I decided to make a new thread for it this time. I love cropping using the resize and crop commands and derivatives. Cropping also has other applications. I am figuring out a few things currently.

1. Given x0,y0,x1,y1, crop but so that it fits a certain aspect ratio. That presents two cases: the actual crop has points within (1a; smaller image) the original 4 or outside (1b; bigger image).

2. Idea 1b presents us with a case where the crop could overtake the original image’s bounds, which I wouldn’t want. In this case, the crop would be smart enough to stretch in the opposite direction (2a). The final problem that I could think of ATM is if both edges along one axis is reached, then no more growing is possible (2b); adjust accordingly.

Sounds like a mouthful already. I have more thoughts but lets start with this for now. Any suggestions? Any takers?

(G'MIC staff) #2

Isn’t this already what the command resize_ratio2d does?


Yes, but I want it to apply to a crop. I will study resize_ratio2d.

PS Let me elaborate on what I have in mind. In a GUI app, you can easily crop by aspect ratio. You start dragging and an appropriately constrained crop rectangle forms either from a corner or the centre.

CLI doesn’t have that option, the rectangle you make is what you crop and it doesn’t adhere to a specified aspect ratio unless you do the mental math and have the dexterity to operate the mouse well. Or you could determine the coordinates. Nah, how about write a script to do all of that, with the considerations I highlighted in the OP.

Except I have bigger ambitions. I want to mark a ROI and expand or shrink it to a specified size and / or aspect ratio. E.g., the aspect ratio for the cropped tiger (the ROI) below is 1.699669 (515x303). What if I wanted an aspect of 1.5 and the rectangle to be 2x, or a minimum width of 700px?


PPS I am aware of @snibgo’s (consider yourself pinged :stuck_out_tongue:) crop2det, and that much of what I am asking about is a common problem. My thought process is: how do I crop a PlayRaw image, for instance, based on its ROI? It an exercise that I want to work through with you all.

(Alan Gibson) #4

A script could be written that took an image, and x0,y0,x1,y1 coordinates and an aspect ratio, and returned an image, as in @afre’s OP.

As afre says, the actual returned image might be within the specified coordinates, or outside them. Or a combination of both.

From just that information, there is no unique solution. The script could return one of a large number of possibilities, and they would all be valid. How does the script decide which one is best?

My Crop to detail uses a fairly primitive measure: sharp parts of the image are more important (more salient) than blurred areas. This works fairly well for my own photos because I use a “full frame” camera, with a sensor about 24x36mm. It wouldn’t work well with phone cameras and their tiny sensors.

A more intelligent system might:

  • identify faces, and decide they are more salient than non-faces;

  • identify human-content versus nature (eg buildings versus bushes) and decide which is more salient;

  • identify other aesthetic features, eg colour, tone, line, patterns, texture etc, and base saliency on those. I fear this would most easily be implemented by a machine-learning system.


@snibgo I don’t intend to discuss how one would approach identifying the ROI. That is a deeper discussion to be had another time. A long time ago, I used to come up with a lot of interesting stuff but I don’t have the energy, smarts, memory and determination to do it anymore. :cry:

What I have been thinking about is how to approach the cropping about 1 chosen continuous ROI. Could be expanded to multiple ROI in the future but again I want to keep it simple for now.


I just found a issue with autocrop if this is related.

To clarify:


The above is a small palette with transparency info. If you apply s y,{h}, and apply autocrop, note that the transparency info is still retained. If I type in mirror x, the transparency info is no longer retained. Ooh, and I noticed one pixel get cropped out as well.

(G'MIC staff) #7

I’m not sure I understand what you expect and what you get. Could you please give us the command you are using for your experiment ?


s y,{h} autocrop

The result in this picture -

Evidently it crops one pixel to the right

The expect result is that all fully transparent pixel info has been removed, and all opaque pixels are retained. Neither are those two are met.

(G'MIC staff) #9

No, that is expected for me.
If you use autocrop without arguments, then G’MIC considers that the color to remove is the first encountered in the image, and in your case, that’s color 0,0,0,255.
So it removes the first pixel.

If you want another color, then specify it, e.g autocrop 0,0,0,0.
Note also that transparent pixels is quite a vague notion, because transparent pixels can be colored. If you specify autocrop 0,0,0,0, only black transparent pixels will be removed
(assuming your 4-channel image is RGBA, which is something G’MIC does not care at all for autocrop :stuck_out_tongue: ).


This would remove all fully transparent pixels at ends.

[palette] split_opacity autocrop_seq 0 append c