snibgo wrote: ↑2019-10-02T05:48:03-07:00If the caption image is a single line, you want to trim it. Correct?
Kind of. Before you suggested
-trim I was thinking I'd have to set a batch variable (eg: text-single-line=1) which would conditionally change
-caption to
-label instead, if a string wasn't longer than the maximum width (ie: a single line). That way it would be a 'caption' if longer than the
-size width value and changed to
-label if shorter than that width.
snibgo wrote: ↑2019-10-02T05:48:03-07:00How about this (Windows BAT syntax)
We create a short caption image to get the height of a single line, then the real caption image. We compare the heights, seting DELWHICH to 1 if the "real" heights is larger or the same. We remove the single-line image and create another, a trimmed version of the real image. We then remove one of these images. The result is what you want.
Sounds promising though the issue I'm seeing when testing
trim sadly is that the little extra of whitespace around the characters it removes after it's applied affects the alignment of the text differently in the final output than the non-trimmed version. Example (upper: your sample, below: the IM commands from the previous topic with the height test added):
The batch script for testing this (the
rem before the
set MYCAPT line comments out the line, can be removed to enable that particular variable):
Code: Select all
@echo off
setlocal enabledelayedexpansion
rem set MYCAPT=My much longer string zzlorem ipsum etc and so on and so forth
set MYCAPT=My much
magick ^
-background rgba(51,153,255,0.7) -fill rgba(0,0,0,1) -font "Segoe-UI-Semibold" -pointsize "15" ^
( caption:"MIyg" ) ^
( -size 150x caption:"%MYCAPT%" ) ^
-set option:DELWHICH "%%[fx:u.h<v.h?1:0]\n" ^
-delete 0 ^
( +clone -trim ) ^
-delete %%[DELWHICH] ^
-gravity southeast -splice 6x4 ^
-gravity northwest -splice 6x2 ^
^( +clone ^
-alpha extract ^
+write mpr:ALP ^
+delete ^
^) ^
^( +clone ^
-alpha off ^
-fill White -colorize 100 ^
-draw " fill black polygon 0,0 0,2 2,0 fill white circle 2,2 2,0" ^
^( +clone -flip ^) -compose Multiply -composite ^
^( +clone -flop ^) -compose Multiply -composite ^
mpr:ALP ^
-compose Multiply -composite ^
^) ^
-alpha off ^
-compose CopyOpacity -composite ^
output-test.png
pause
endlocal
Perhaps there's a way to solve the trim whitespace issue? Otherwise is there a way this IM height check could be adapted to conditionally change the
-caption to a
-label if it detects the text is a single line? As being a 'label' ignores the 'size' width which would be ideal, since so far the commands don't require creating a temp file.