Caption failed adding text from file to 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
lindylex
Posts: 23
Joined: 2014-02-27T16:36:22-07:00
Authentication code: 6789

Caption failed adding text from file to image

Post by lindylex »

I am trying to place the contents of a text file on top of a image. I am using caption because the documentation said it will add the necessary line breaks to match the dimension of the image.

This is from the on-line documentation.

http://www.imagemagick.org/Usage/text/#label :

"Best Fit Caption
As of IM v6.3.2, if you provide both the width and the height of the final image, but do not define the "-pointsize" of the font (or turn off the pointsize with "+pointsize"), IM will attempt to automatically adjust the size of the font so as to best fill the "-size" of the image you requested. "

Code: Select all

cat ./temp.txt | convert -background lightblue -fill blue -font /usr/share/fonts/truetype/freefont/FreeMonoBold.ttf -size 1000x1000 caption:@- cf.png
Error :

Code: Select all

convert-im6.q16: not authorized `@-' @ error/property.c/InterpretImageProperties/3516.
convert-im6.q16: no images defined `caption.png' @ error/convert.c/ConvertImageCommand/3258.
lindylex
Posts: 23
Joined: 2014-02-27T16:36:22-07:00
Authentication code: 6789

Re: Caption failed adding text from file to image

Post by lindylex »

] SOLUTION SOLVED [

convert -background lightblue -fill blue -font /usr/share/fonts/truetype/freefont/FreeMonoBold.ttf -size 500x500 caption:"$(cat temp.txt)" -bordercolor lightblue -border 10 cf.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Caption failed adding text from file to image

Post by fmw42 »

You can change your policy.xml if you do not care about outside security issues.

You can also do

Code: Select all

text=`cat ./temp.txt`
convert -background lightblue -fill blue -font /usr/share/fonts/truetype/freefont/FreeMonoBold.ttf -size 500x500 caption:"$text" -bordercolor lightblue -border 10 cf.png
lindylex
Posts: 23
Joined: 2014-02-27T16:36:22-07:00
Authentication code: 6789

Re: Caption failed adding text from file to image

Post by lindylex »

fmw42 wrote: 2017-03-09T00:26:22-07:00 You can change your policy.xml if you do not care about outside security issues.

You can also do

Code: Select all

text=`cat ./temp.txt`
convert -background lightblue -fill blue -font /usr/share/fonts/truetype/freefont/FreeMonoBold.ttf -size 500x500 caption:"$text" -bordercolor lightblue -border 10 cf.png
fmw42, thanks for the response. I love your solution.
lindylex
Posts: 23
Joined: 2014-02-27T16:36:22-07:00
Authentication code: 6789

Re: Caption failed adding text from file to image

Post by lindylex »

I am trying to overlay text onto this gradient and it is not working. It creates two images and none have the text caption.

Code: Select all

convert -size 1024x512 xc: +size xc:"#fb35fc" xc:"#603edb" \
          -fx 'ar=hypot( i/w-.8, j/h-.3 )*4;
               br=hypot( i/w-.3, j/h-.7 )*4;
               u[1]*br/(ar+br) + u[2]*ar/(ar+br)' \
-fill '#ffffff' -font /usr/share/fonts/truetype/freefont/FreeMonoBold.ttf caption:"$(cat ./temp.txt)" -border 10\
          i1.png
lindylex
Posts: 23
Joined: 2014-02-27T16:36:22-07:00
Authentication code: 6789

Re: Caption failed adding text from file to image

Post by lindylex »

How do I pad the text without using the border command? I noticed the gradient does not cover the border.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Caption failed adding text from file to image

Post by snibgo »

lindylex wrote:It creates two images and none have the text caption.
You create three images, all with "xc:". Then "-fx" modifies the first and removes the others, so you have one image. Then you add a secoond image with "caption:". So your output has two images.

The second image contains your caption. But the text is small, and white on a white background.
snibgo's IM pages: im.snibgo.com
lindylex
Posts: 23
Joined: 2014-02-27T16:36:22-07:00
Authentication code: 6789

Re: Caption failed adding text from file to image

Post by lindylex »

snibgo, what you describe is what is happening. Can I do this all in one command, add the caption to the gradient?
snibgo wrote: 2017-03-18T07:14:48-07:00
lindylex wrote:It creates two images and none have the text caption.
You create three images, all with "xc:". Then "-fx" modifies the first and removes the others, so you have one image. Then you add a secoond image with "caption:". So your output has two images.

The second image contains your caption. But the text is small, and white on a white background.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Caption failed adding text from file to image

Post by fmw42 »

What are you expecting the output to look like? Please describe in detail regarding the size of the 3 images and how they should look when combined?

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at viewtopic.php?f=1&t=9620

For novices, see

viewtopic.php?f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Caption failed adding text from file to image

Post by snibgo »

I don't know what you want. The following makes a graduated image, a text image with transparent background, composites the text over the graduation, and puts a gray border around the result.

Code: Select all

convert \
  -size 1024x512 xc: \
  +size xc:"#fb35fc" xc:"#603edb" \
  -fx 'ar=hypot( i/w-.8, j/h-.3 )*4;
       br=hypot( i/w-.3, j/h-.7 )*4;
       u[1]*br/(ar+br) + u[2]*ar/(ar+br)' \
  -fill '#ffffff' \
  -background None -gravity Center -pointsize 50 \
  caption:"Hello world" \
  -compose Over -composite \
  -border 10 \
  i2.png
It uses "-fx" so is very slow.
snibgo's IM pages: im.snibgo.com
lindylex
Posts: 23
Joined: 2014-02-27T16:36:22-07:00
Authentication code: 6789

Re: Caption failed adding text from file to image

Post by lindylex »

This is the closes I can get to making this happen in one line.

Code: Select all

convert -size 1000x1000 xc: +size xc:none -fill '#f5f21f' -font /usr/share/fonts/truetype/freefont/FreeMonoBold.ttf -size 1024x512 caption:"$(cat ./temp.txt)" xc:"#fb35fc" xc:"#603edb" \
          -fx 'ar=hypot( i/w-.8, j/h-.3 )*4;
               br=hypot( i/w-.3, j/h-.7 )*4;
               u[1]*br/(ar+br) + u[2]*ar/(ar+br)' \			
          test.png
I did it using three commands.

Code: Select all

] Make Purple Gradient [

convert -size 1000x1000 xc: +size xc:"#fb35fc" xc:"#603edb" \
          -fx 'ar=hypot( i/w-.8, j/h-.3 )*4;
               br=hypot( i/w-.3, j/h-.7 )*4;
               u[1]*br/(ar+br) + u[2]*ar/(ar+br)' \
          i1.png

] Transparent [

convert -background 'rgba(0,0,0,0)' -fill '#ffffff' -font /usr/share/fonts/truetype/freefont/FreeMonoBold.ttf -size 950x950 caption:"$(cat ./temp.txt)" p1.png

] Overlay Text On Image [

convert  i1.png  p1.png -gravity center -composite r.png
This is the image.

https://unsee.cc/netupido/

ImageMagick version

Code: Select all

Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Caption failed adding text from file to image

Post by fmw42 »

You need to use parenthesis processing to separate your commands. see http://www.imagemagick.org/Usage/basics/#parenthesis

Code: Select all

convert \
\( -size 1000x1000 xc: +size xc:"#fb35fc" xc:"#603edb" \
    -fx 'ar=hypot( i/w-.8, j/h-.3 )*4;
    br=hypot( i/w-.3, j/h-.7 )*4;
    u[1]*br/(ar+br) + u[2]*ar/(ar+br)' \) \
\( -size 950x950 -background 'rgba(0,0,0,0)' -fill '#ffffff' \
    -font /usr/share/fonts/truetype/freefont/FreeMonoBold.ttf \
    caption:"$(cat ./temp.txt)" \) \
-gravity center -composite r.png
or easier using directional gradient. See http://www.imagemagick.org/script/gradient.php

Code: Select all

convert \
\( -size 1000x1000 -define gradient:direction=southwest \
    gradient:"#fb35fc-#603edb" \) \
\( -size 950x950 -background none -fill white \
    -font /usr/share/fonts/truetype/freefont/FreeMonoBold.ttf \
     caption:"$(cat ./temp.txt)" \) \
-gravity center -composite r2.png
But since you did not provide your text file, it is hard to test the text wrapping. If you have line breaks in your text file, then things will not look correct.

You may also want to trim your text image result to its bounding box, since your text may not fill the entire height of the image.
lindylex
Posts: 23
Joined: 2014-02-27T16:36:22-07:00
Authentication code: 6789

Re: Caption failed adding text from file to image

Post by lindylex »

fmw42, thanks your second solution run so fast and does exactly what I need.
Post Reply