无法使用ImageMagick使文本适合图像 [英] Can't fit text to image with ImageMagick

查看:53
本文介绍了无法使用ImageMagick使文本适合图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使文本适合图像.我的图片有不同的尺寸,所以我无法设置恒定的磅值.

I need to fit my text to image. My image have different sizes so i can't set constant pointsize.

我的命令看起来像这样

convert 
    -fill white
    -font Winter Calligraphy
    -size `${options.width}x${options.height}`
    label: KJHGFD
    test.gif

在输出中,您可以在图片上方看到裁剪的部分.

on output you can see cropped part on top of picture.

输出:

  • 我只有这种字体有问题,其他字体效果很好.
  • 我尝试在顶部添加白色边框.不幸的是,这只会将损坏的文本移到底部.
  • 我无法更改区域大小.
  • 文本必须填充尽可能多的空间.
  • 我需要使用冬季书法字体

推荐答案

这里是获取所需结果的一种模糊方法.步骤如下:

Here is a slightly kludgy way of getting the result you want. Here are the steps:

  • 首先,使用标题:获取 ImageMagick ,告诉您它将用于填充文本框并提取该信息的磅值

  • First, use caption: to get ImageMagick to tell you the pointsize it would use to fill your text box and extract that info

创建一个画布,其宽度和高度是您真正想要的画布的两倍,并在其中绘制文本-它势必会适合您!

Create a canvas twice as wide and twice as tall as the one you really want and draw your text in the middle of that - it is bound to fit!

现在修剪掉文本周围的多余背景,以便您拥有包含文本的绝对最小边界框

Now trim away the extraneous background around the text so you have the absolute minimum bounding box to contain the text

将结果调整为所需大小.

Resize the result to your desired size.

#!/bin/bash

# Width, height and text
w=600
h=150
text="KJHGFD"

# Get pointsize ImageMagick thinks is good
pointsize=$(convert -gravity center -background black -fill white -size ${w}x${h} \
    -font "Winter Calligraphy.ttf" caption:"$text" -format "%[caption:pointsize]" info:)

echo ImageMagick likes pointsize: $pointsize

# So draw text in that size on larger canvas, trim to bounds of letters and resize to desired size
wb=$((w*2))
hb=$((h*2))
convert -gravity center -fill white -size ${wb}x${hb} xc:black \
    -font "Winter Calligraphy.ttf" -pointsize $pointsize -annotate 0 "$text" \
    -trim +repage -resize ${w}x${h}\! result.png

这篇关于无法使用ImageMagick使文本适合图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆