Possible?: Changing the "Line Height" of a Text in an Image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
vielhuber
Posts: 23
Joined: 2010-09-05T15:40:14-07:00
Authentication code: 8675308

Possible?: Changing the "Line Height" of a Text in an Image

Post by vielhuber »

Hello friends!

I think this is a hard task and I need your help.
Is it possible to reach the following effect with ImageMagick:

Image

--------------------------------------------------------------------
to
--------------------------------------------------------------------

Image

IM has to trim the white space not around the edges (this is easy), but INSIDE the document.
It has to recognize areas (width: 100%, height: variable, say minimum 5px) and trim them (like splice them out of the document).

Is this possible? THANKS IN ADVANCE.
Please tell me also, if this is NOT possible.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Possible?: Changing the "Line Height" of a Text in an Im

Post by fmw42 »

Not easily if these are images (though I suspect it is possible with some scripting work to locate non-white areas and crop them out and append them back again).

If you are starting with text and creating the images, then you can adjust the line height when converting text to image via -interline-spacing. see http://www.imagemagick.org/Usage/text/# ... ne-spacing
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Possible?: Changing the "Line Height" of a Text in an Im

Post by anthony »

Download the script divide_vert from
http://www.imagemagick.org/Usage/scripts/

This script divides images into a sequence blank and non-black row divisions. A -i option has it ignore blank divisions. As such to remove all blank rows use...

Code: Select all

  divide_vert -i input_image.png miff:- | convert - -append  gaps-removed.png
To replace blank rows with say 5 pixel gaps... use...

Code: Select all

   divide_vert -i - miff:- |\
      convert - -splice 0x5 -append \
                -gravity south -splice 0x5  gaps_replaced.png
YES divide_vert is a shell script and as such quite slow.

The divide_vert script was a proof of concept script whcih I hope to implement in a new 'segmentation' section of IM Examples. Along with 'morphological object labelling' and something like what Fred multicrop script provides. However I just don't have time to do any programing in IM at this time.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
vielhuber
Posts: 23
Joined: 2010-09-05T15:40:14-07:00
Authentication code: 8675308

Re: Possible?: Changing the "Line Height" of a Text in an Im

Post by vielhuber »

Hi!

Thanks for all answers and time.

I tried the script and indeeed it seems to work, but it is very very slow, as you mentioned.

Another idea is to use the liquid rescale thing http://www.imagemagick.org/Usage/resize/#liquid-rescale

Please inform me in this thread if you can release your script not as bash but in full speed!

Thanks in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Possible?: Changing the "Line Height" of a Text in an Im

Post by fmw42 »

this seems to work for me

convert before.jpg -liquid-rescale 100x30% before_lr_100x30.png

Note I tried this to get an estimate of the amount to scale, but it was an over estimate for some unknown reason with respect to -liquid-rescale

Stretch to full dynamic range, average all columns to one column, negate and threshold to black and white, get average graylevel

convert before.jpg -auto-level -scale 1x461! -negate -threshold 0 -format "%[fx:mean]" info:
0.466377
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Possible?: Changing the "Line Height" of a Text in an Im

Post by anthony »

As I like to say...
There are lots of ways to skin a cat, and what method you use depends
on what you want that skin for, and how messy you like the results!
The script I provided was a concept script. Their are probably ways to speed it up significantly. For example by scaling an image to one column of pixels will get you a nice average of all the values. If that value is white then that row can be removed. However that only works for black and white images.

A little extra color replacement work, can make this work for any single soild-color background.

Hmmmm.....

Yes... it works, and should work for any fixed solid color background! If it wasn't for the need to gather the 'height' of the image, or posibly collect the background color from the top-left corner pixel, it would not even need any temporary files at all!

Look at the script "divide_vert_bg" when it becomes available in IM Examples scripts area in an hour or two...
http://www.imagemagick.org/Usage/scripts/divide_vert_bg
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply