Imagemagick转换:无法获取-pointsize以使用文本注释的计算值

时间:2015-08-04 14:13:32

标签: imagemagick imagemagick-convert

我正在使用ImageMagick对任意用户图像进行自动水印,使用'convert'二进制文件(6.9.1,Mac OS)。

我尝试了很多东西,包括以下两种方法:

convert image.jpg -pointsize %[fx:w*0.05] -annotate 0 'Pointsize should be: %[fx:int(w*0.05)]' out.jpg

convert image.jpg -set value %[fx:w*0.05] -pointsize %[value] -annotate 0 'Pointsize should be: %[value]' out.jpg

在这两种情况下,计算值都显示在文本标签中,但在pointsize设置中不起作用。我已经确认,在设置静态值时,pointsize选项本身可以正常工作,例如

convert image.jpg -pointsize 30 -annotate 0 'Pointsize should be: 30' out.jpg

我完全偏离了这里吗?我不知道还有什么可以尝试的。

由于

西蒙

2 个答案:

答案 0 :(得分:4)

Mark Setchell拥有最佳答案/解决方案。 ImageMagick不会扩展传递给-pointsize的变量工件,并且默认为1 (你应该会看到一个非常非常小的涂抹,这将是你的文字渲染@ 1pt。)

这是一个黑客

使用+pointsize选项,或者甚至不包括任何-pointsize参数,将调用ImageMagick的caption:协议来动态调整点大小以适合父MBR(最小边界矩形)。

使用-size重载-set option:size参数,这将扩展&尊重%[fx:...]变量。

convert wizard: -set 'option:size' '%[fx:w*0.5]x%[fx:h*0.5]' \
        +pointsize  caption:'This is dynamic pointsize' \
        -composite output.png

dynamic point size

但你真的应该做马克建议的事情!

答案 1 :(得分:1)

这很有效,但它使用shell时有点麻烦和丑陋,并调用第二个进程(ImageMagick的identify)并且必须重复图像名称......

convert image.jpg                                               \
  -pointsize $(identify -format "%[fx:int(w*0.05)]" image.jpg)  \
  -gravity center -annotate 0 "Some Text" out.jpg

enter image description here

尽管如此,它可能会让你走到我,或者有人聪明(@emcconville我在看着你),找出如何正确地做到这一点: - )

注意:

我可以看到我可以使用以下方法将我自己的唯一命名变量正确设置为宽度的5%:

convert image.jpg -set option:phnqz '%[fx:int(w*0.05)]' -verbose info:

Image: image.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 200x200+0+0
  Units: Undefined
  Type: Palette
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8/1-bit
  Channel depth:
    red: 1-bit
    green: 1-bit
    blue: 1-bit
  Channel statistics:
    Pixels: 40000
    Red:
      min: 255 (1)
      max: 255 (1)
      mean: 255 (1)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
      entropy: nan
    Green:
      min: 255 (1)
      max: 255 (1)
      mean: 255 (1)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
      entropy: nan
    Blue:
      min: 0 (0)
      max: 0 (0)
      mean: 0 (0)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
      entropy: nan
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 170 (0.666667)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
      entropy: nan
  Colors: 1
  Histogram:
     40000: (255,255,  0) #FFFF00 yellow
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 200x200+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 92
  Orientation: Undefined
  Properties:
    date:create: 2015-08-04T17:14:55+01:00
    date:modify: 2015-08-04T17:14:55+01:00
    fx:int(w*0.05): 10
    jpeg:colorspace: 2
    jpeg:sampling-factor: 1x1,1x1,1x1
    signature: 0b798b7a624ba8417c5249dc58a50fa18c931dbde7fd60227bf932d5de228aba
  Artifacts:
    filename: image.jpg
    phnqz: 10                               <--- *** HERE IT IS ***
    verbose: true
  Tainted: False
  Filesize: 757B
  Number pixels: 40K
  Pixels per second: 0B
  User time: 0.000u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.9.1-9 Q16 x86_64 2015-08-03 http://www.imagemagick.org