Detect x-value of right-most black pixel within a monochrome image?

Viewed 45

I'm attempting to script up something to convert some historical typefaces off of a Mac Plus emulator. I've got it where it screen captures each glyph individually, and I've pre-calculated the the height and left margin of it which allows me to mostly crop it the way I need. But these fonts seem to be proportional, and several characters are wider than the rest.

Is it possible with shell script tools to determine the x value of the right-most black pixels? Since I'm already using Imagemagick, can it do this itself? I had intended for this to be a quick and dirty project, so I'd like to avoid heavier scripting languages (perl, python) if at all possible.

enter image description here

enter image description here

1 Answers

Try this to get the trim box:

magick YOURIMAGE format %@ info:
6x10+1+5

You can read the width, height, x-offset and y-offset into bash variables like this:

IFS='[x+]' read w h x y < <(magick YOURIMAGE -format %@ info:)

You can add w and x with:

((right=w+x))

You may, or may not, want to subtract 1 from the answer above.


I don't know what you are actually trying to do, but you maybe just want to trim your letters to their smallest bounding box:

magick YOURIMAGE -trim result.png
Related