我正在嘗試創建一堆非常簡單的影像,只是一個白色背景上的字串。
我希望字串位于影像中的隨機位置,圍繞中心,以便整個字串可見(即沒有隱藏在影像限制之外)。
字串是自動生成的,所以我事先不知道它們的長度,我只知道它們會在給定的長度范圍內(6 - 15 個字符)。
為此,我正在使用 imagemagick 的轉換,這就是我得到的 atm:
convert -background "#ffffff" -size 800x480 -fill "#000000" -pointsize 72 -gravity Forget label:'mytext' output.png
這呈現了這一點:

gravity 似乎會影響標簽的位置,但沒有隨機選項。
uj5u.com熱心網友回復:
更新的答案
好的,我認為您可以繪制文本并將其修剪到其生命的一英寸以內,然后用 50px 的白色填充它并將其隨機放置在您的最終畫布上:
#!/bin/bash
bd=50 # 50px white space around letters
ps=72 # pointsize of letters
fh=480 # final height
fw=800 # final width
tmp="/tmp/$$.mpc" # temporary file
# Generate the text with padding all round so it can't touch edges and get its size
read w h < <(magick -pointsize $ps label:"$1" -trim -bordercolor white -border $bd -write "$tmp" -format "%w %h" info:)
echo "DEBUG: Text requires ${w}x${h} px"
# Work out random x,y offset from top-left where to place text
((x=RANDOM%(fw-w)))
((y=RANDOM%(fh-h)))
magick -size ${fw}x${fh} xc:white "$tmp" -geometry " ${x} ${y}" -composite result.png
然后您將其另存為doit并使其可執行:
chmod x doit
然后呼叫它:
./doit "Your text"
DEBUG: Text requires 381x153 px
result.png

這是一個 100 幀的影片:

原始答案
這是一個基本模板,可在距影像中心 50 20 的偏移量處繪制文本:
magick -size 640x480 xc:white -fill black -pointsize 72 -gravity center -annotate 50 20 "My Text" result.png

You can then randomize the X Y coordinates (also using -X-Y if you want the position offset above and to left of the centre-point instead of below and right) using your shell. Something like this:
for ((i=0;i<20;i )) ; do
((X=RANDOM
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/419552.html
標籤:
上一篇:有沒有辦法將Microsoft.Identity.Web用于外部登錄按鈕,而不依賴于Microsoft身份驗證?
