Page 2 of 2

Re: Crop image based on coordinates from a "-trim +repage" of a different image?

Posted: 2017-12-10T21:20:57-07:00
by the dsc

These results from the original script, with the modifcations suggested, and a few iterations.

The intermediate files/steps are the same.

Without the subprocess (or "physical" intermediate file), or the whole mask-cropping twice thing, all I could get were things like that. Just the amount of mismatch changes.

My best guess, but still a wild guess, is that it may be possible to use the original file's dimensions to give parameters analog to those on "cropvar", and then apply it to the mask itself, skipping the "extent" subprocess, or the whole thing of having a second generation of masks to crop the masks themselves.

Re: Crop image based on coordinates from a "-trim +repage" of a different image?

Posted: 2017-12-11T07:58:55-07:00
by the dsc
The idea of cropping the mask itself based on the original image size did work, and was incomparably simpler than the "second version". I haven't yet made it clean the temporary files, and perhaps there are some declarations of variables that aren't even being used, leftovers from earlier attempts, with different logic. But it works...

Code: Select all


convert montage.png -crop 220x220 +repage maskplain-%02d.png

\ls -1 *.jpg > originals.txt
\ls -1 maskplain*.png > masks.txt

while read original <&3 && read mask <&4 ; do 

    res=($(identify -format  "%w %h" {$original}))


     if ((x>y)) ; then



    ((y>x)) && samplevar="x${y}"

    unset cropmask

    if ! ((x==y)) ; then

        cropmask="-crop $cropmaskvar"


    convert ${mask} -colorspace gray -background white -alpha remove -threshold 99% -sample ${samplevar} $cropmask +repage /dev/shm/masktmp-${original}.gif

    cropv=`convert /dev/shm/masktmp-${original}.gif  -format "%@" info:` ; 

    #convert "${original}" -crop $cropv +repage "${original/.jpg/-cropped.jpg}"
    # I have found that for the final crop, jpegtran does an incomparably faster job than imagemagick. This line will attempt first to do a lossless cropping, and proceed to a normal cropping, if it fails.
     jpegtran -perfect -crop $cropv  "${original}" > "${original/.jpg/-cropped.jpg}" || jpegtran -crop $cropv "${original}" > "${original/.jpg/-cropped.jpg}"

done 3<originals.txt 4<masks.txt

(There was yet another instance of "convert" subprocess that was apparently unnecessary.)

Suggestion, In order to make the computer (or old computers) still usable while it's working with big images: adding early on the script:

Code: Select all

convert="ionice -c3 convert"
And replacing all the following instances of "convert" to "$convert".

Additionally I have every convert line ending like this:

Code: Select all

 & cpulimit -l 10 -z -p $!
Which requires the "cpulimit" to be installed, less common than ionice, which I believe is a standard thing.