0

how can I add some text "before" an existing image using imagemagick? That means I have an image (a signature) and I would like to add some text (the current date) before the image, on the left side (not above or bottom). How can I do that? Ive tried it with append, but that havent worked for me :-(

Here is one exemple signature

And here it is how it should looks afterwards with the current date

Ive tried it with this code so far:

convert signature.png label:21.10.2019 -gravity West +append signature-with-date.png

Now Ive tried it with other order:

convert -gravity west label:21.10.2019 signature.png +append signature-with-date.png

But there is only white space before the image added, no text is shown

Ive tried it now with:

magick \( -size 400x -background white -gravity west label:21.10.2019 \) signature.png +append signature-with-date.png

Still not working

as you can see here

Christian
  • 11
  • 2
  • 1
    Please show the image you started with and how it is supposed to look afterwards - also, according to StackOverflow rules, you are supposed to show the code you have tried. – Mark Setchell Oct 21 '19 at 15:40
  • Hi Mark, Ive added two exemple images (before & afterwards) and also my code. – Christian Oct 21 '19 at 16:13
  • Change the order... `convert -gravity west label:21.10.2019 signature.png +append signature-with-date.png` – Mark Setchell Oct 21 '19 at 16:19
  • Or load in the same order but swap before appending... `convert signature.png label:21.10.2019 -gravity West +swap +append signature-with-date.png` – Mark Setchell Oct 21 '19 at 16:20
  • Ive tried it, but there is no text, only white space was added on the left side. :-/ – Christian Oct 21 '19 at 16:26
  • What did you try? I made 2 suggestions. Please check the size of the input image and the size of the output image too to see if they are the same. – Mark Setchell Oct 21 '19 at 16:28
  • Ive tried the first suggestion. Ive updated the question with the result. Image size of the ouput file is now 641 × 181 Pixel (before 580 × 181 Pixel), but no text there :-/ – Christian Oct 21 '19 at 16:37
  • Try fixing a size for the label `convert -gravity west -size 200x181 label:21.10.2019 signature.png +append signature-with-date.png` – Mark Setchell Oct 21 '19 at 16:48
  • No the image size is 780 × 181 Pixel but still no text added – Christian Oct 21 '19 at 16:56
  • Try `magick -size 400x181 xc:red signature.png +append result.png` to see if you append a red block at least. – Mark Setchell Oct 21 '19 at 20:27
  • Then try your previous command but using double quotes around the label, i.e. `label:"21.10.2019"` as Fred suggested. – Mark Setchell Oct 21 '19 at 20:30
  • Then try `identify -list font` to see if you have any fonts installed, if not, see here... https://stackoverflow.com/a/24701602/2836621 – Mark Setchell Oct 21 '19 at 20:33
  • Thanks Mark, Ive tried `identify -list font` and have taken one font from the "System fonts" there. So now its working fine with the following code `convert -size 300x -background white -gravity west -font NimbusSans-Regular label:"21.10.2019" signature.png +append signature-with-date.png` Thanks. – Christian Oct 21 '19 at 21:23
  • but I have now a new issue, when I try afterwards the following `magick signaturewithdate.png -resize 400x25 -transparent white -page A4+300+200 stamp.pdf` then in the stamp.pdf I have the date (21.10.2019) twice? I dont know why :-/ – Christian Oct 21 '19 at 22:21

1 Answers1

1

This works just fine for me on Imagemagick 6.9.10.68 Q16 Mac OSX.

Unix syntax:

convert \( -size 400x -background white -gravity west label:"21.10.2019" \) signature.png +append signature-with-date.png


Windows syntax:

convert ( -size 400x -background white -gravity west label:"21.10.2019" ) signature.png +append signature-with-date.png


enter image description here

What is your Imagemagick version and platform?

If on Imagemagick 7, use magick in place of convert.

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Not sure why you have included the parentheses here? Normally I would only do that to make sure an operator doesn't get applied to some secondary image I don't want it to affect, but as you only have a single image loaded, I would have thought they were unnecessary? – Mark Setchell Oct 21 '19 at 19:49
  • I dont know why, but it doesnt work out for me :-( (tested with both magick and convert) I have posted the result in my question, please take a look there. my version is ImageMagick 7.0.8-68, OS: ‪CentOS Linux 7.7.1908 (Core)‬ – Christian Oct 21 '19 at 20:09
  • Add a specific font to your command and see if that works. Perhaps your OS does not have a defined default font. `convert \( -size 400x -background white -gravity west -font arial label:"21.10.2019" \) signature.png +append signature-with-date.png`. If that does not work. Use the path to some font that you have on your system. What do you get from magick -version for you delegates? Does it include freetype and fontconfig? If not, add them and recompile. – fmw42 Oct 21 '19 at 20:27
  • Here is the result of -magick version: Version: ImageMagick 7.0.8-68 Q16 x86_64 2019-10-07 https://imagemagick.org Copyright: © 1999-2019 ImageMagick Studio LLC License: https://imagemagick.org/script/license.php Features: Cipher DPC HDRI Modules OpenMP(3.1) Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gslib gvc heic jbig jng jp2 jpeg lcms ltdl lzma openexr pangocairo png ps raqm raw rsvg tiff webp wmf x xml zlib – Christian Oct 21 '19 at 20:53
  • Tried it with `magick \( -size 400x -background white -gravity west -font Helvetica label:"21.10.2019" \) signature.png +append signature-with-date.png` no other result than the one if posted above, no text. – Christian Oct 21 '19 at 20:55
  • Do you have Helvetica on your system and does the Imagemagick type.xml file include it by name. Otherwise, if you have it, try using -font path_to/helvetica.ttf. Replace path_to with your actual full path to the font and use the actual suffix, typically .ttf, but it could be otherwise. Try `magick \( -size 400x -background white -gravity west -font full_path_to helvetica.ttf -fill black label:"21.10.2019" \) signature.png +append signature-with-date.png` – fmw42 Oct 21 '19 at 22:04
  • It now works with `convert -size 400x -background white -gravity west -font NimbusSans-Regular label:"21.10.2019" signature.png +append signature-with-date.png` Ive tried `identify -list font` and have taken one font from the listed "System fonts" there (NimbusSans-Regular). but I have now a new issue, when I try afterwards the following `magick signaturewithdate.png -resize 400x25 -transparent white -page A4+300+200 stamp.pdf` then in the stamp.pdf I have the date (21.10.2019) twice? I dont know why :-/ – Christian Oct 21 '19 at 22:25
  • -page only works with -flatten or -layers merge or similar. It does nothing by itself. Try `magick signaturewithdate.png -resize 400x25 -transparent white -page A4+300+200 -background none -flatten +repage PNG:- | magick - stamp.pdf`. If that is not what you want, then post a new question. Note if I write directly to PDF, it does not work for me on IM 7.0.8.68 Q16 Mac OSX, but with the pipe, I do get a result. But one gets a white background when displaying the PDF. This is typical of PDF files. It will not show the transparency until you render as `magick -backgrouknd none stamp.pdf x.png` – fmw42 Oct 21 '19 at 23:36
  • You get the date twice, because you have put the date as text into the meta data of the image. You can see that by identify -verbose signaturewithdate.png. It shows: `label: 21.10.2019 label:pointsize: 78.75` – fmw42 Oct 21 '19 at 23:43
  • What I want to do after putting the date before the signature is adding the signature with the date to a pdf using pdftk. Adding it as a layer to the pdf works fine with pdftk, but I have the date twice in the stamp pdf. I only want it there one time before the signature, so I have to remove it from the meta date of the image? How I can do that? – Christian Oct 22 '19 at 00:07
  • Ive found `-strip` which is working. Is there a way to make the converted stamp.pdf sharper? It has lost much sharpness with `magick signaturewithdate.png -resize 400x25 -transparent white -page A4+335+180 -quality 100 stamp.pdf` :-/ – Christian Oct 22 '19 at 00:41
  • Why do you resize to 400x25 (height is very small), if you want higher quality? But if you must, either add `-density 10 ` not `-quality 100` or when you display or convert the pdf to view, add -density 300. – fmw42 Oct 22 '19 at 01:59
  • I must resize it to 400x25 because the place on the pdf where I want to put the signature with the date allows only a height of 25 px (and a maximum length of 400 px). So therefore the signature with date on the stamp.pdf must have this maximum size :-/ Where do you mean I must add `-density 10` (isnt that too low?) and where `-density 300`? – Christian Oct 22 '19 at 12:10
  • PDF files have no size in pixels. They are vector files that may contain an image. The pixel size depends upon how you rasterize it to view it. Please post another question and post an example image into which you want to put the signature, so that we can understand your problem better. Are you trying to put the signature in a raster image such as PNG or JPG and then convert to PDF. Or are you trying to put the signature into a PDF file? – fmw42 Oct 22 '19 at 16:33
  • Ive posted a new question here: https://stackoverflow.com/questions/58513753/adding-current-date-to-a-signature-image-and-adding-it-into-an-existing-pdf Can you please take a look there, Ive described there what I want to do. Thanks. – Christian Oct 23 '19 at 00:04