Using pango with convert

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
lastchansen
Posts: 5
Joined: 2019-01-19T18:09:54-07:00
Authentication code: 1152

Using pango with convert

Post by lastchansen »

I have been trying to get small script working over the last few hrs and this is what I came up with:

Code: Select all

#!/bin/bash
notes=$(cat /home/chansen/Nextcloud/notes.md)
convert -geometry x1080\
	-fill '#00000050'\
	-draw "rectangle 50,50 530,970"\
	-pointsize 20 \
	-fill '#ffff99'\
	-draw "text 70,100 '$notes'" \
	/home/chansen/.config/i3/wall /home/chansen/.config/i3/wall2 && feh --bg-scale /home/chansen/.config/i3/wall2
This takes my notes.md and prints it to my wallpaper. Screenshot: https://postimg.cc/HVcNMXxd. The imagemagic documentation https://www.imagemagick.org/Usage/text/#pango_markup has a section about using pango, but no matter how I add the pango:@ I get an error which is related to pango:@ not being used properly - I think. I've also tried with .txt instead of .md, but that does not solve the issue.

The example from the imagemagic docs work fine:

Code: Select all

convert pango:@pango_test.txt pango_test.png
With a txt-file with the following pango-markup:

Code: Select all

# <span color='#ff00ff'>lol</span>
- lalala
- lalala
# <span color='#cc66cc'>Work
- Diz iz work!</span>
# <span color='#00ff00'>Fun and games</span>
Could anyone give me a hint on how to get further with my script? My guess is that pango can't be used in my case with variables or with other pictures, but can only be used in the case described at the imagemagic documentation.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using pango with convert

Post by snibgo »

What version of IM?

Your syntax is bad. You should read an image, then process it, then write it. You process the image (eg "-draw") before you have read it.

Your command doesn't contain "pango:" Therefore you are not using pango. Even if your text contains pango markup, it is merely text that is drawn.
lastchansen wrote:I get an error which is related to pango:@ not being used properly - I think.
Well, that means you have an error. I think. If you say exactly what error message you get, perhaps we can be more helpful.
snibgo's IM pages: im.snibgo.com
lastchansen
Posts: 5
Joined: 2019-01-19T18:09:54-07:00
Authentication code: 1152

Re: Using pango with convert

Post by lastchansen »

Sorry :) I'll try to be more clear.
What version of IM?
Version: 7.0.8.23-1
Your syntax is bad. You should read an image, then process it, then write it. You process the image (eg "-draw") before you have read it.
Yeah, I know my syntax is bad but the script currently works flawlessly as intended. The only thing I can't get working is pango-markup. If I change my script to make it read "wall.png" before "-draw" I get this:

Code: Select all

'-' for standard input or output.
./walltext: line 12: -geometry: command not found
Your command doesn't contain "pango:" Therefore you are not using pango. Even if your text contains pango markup, it is merely text that is drawn.
I know :) I posted the script in which I want to use pango, however, I don't know how to include pango. I have tried different solutions and I can't get it to work. I seem to get an error depending on what I try. Therefore, I guessed the error would be irrelevant if I asked where "pango:" should be placed in my script. I guess I was wrong. Here is an example:

Code: Select all

#!/bin/bash

notes=$(cat /home/chansen/pango_test.txt)

	
convert	-geometry x1080\
	-fill '#00000050'\
	-draw "rectangle 50,50 530,970"\
	-pointsize 20 \
	-fill '#ffff99'\
	-draw "text 70,100 'pango:@$notes'" \
	wall.png wall2.png

Code: Select all

convert: non-conforming drawing primitive definition `-' @ error/draw.c/RenderMVGContent/4361.
lastchansen wrote:I get an error which is related to pango:@ not being used properly - I think.
Well, that means you have an error. I think. If you say exactly what error message you get, perhaps we can be more helpful.
Thank you for trying to help me :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using pango with convert

Post by fmw42 »

Is pango installed as a delegate in your ImageMagick. Mine is installed via cairo and lists cairo when I do

Code: Select all

convert -version
Version: ImageMagick 6.9.10-23 Q16 x86_64 2019-01-13 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gslib gvc jbig jng jp2 jpeg lcms lqr ltdl lzma openexr png ps raw rsvg tiff webp x xml zlib
lastchansen
Posts: 5
Joined: 2019-01-19T18:09:54-07:00
Authentication code: 1152

Re: Using pango with convert

Post by lastchansen »

I use pango several other places and with the simple example, pango works just fine with imagemagick :)

Code: Select all

[chansen@Georg ~]$ pacman -Qi imagemagick 
Name            : imagemagick
Version         : 7.0.8.23-1
Description     : An image viewing/manipulation program
Architecture    : x86_64
URL             : https://www.imagemagick.org/
Licenses        : custom
Groups          : None
Provides        : None
Depends On      : libmagick=7.0.8.23-1
Optional Deps   : imagemagick-doc: manual and API docs
Required By     : a2ps  python-pywal
Optional For    : feh  neofetch
Conflicts With  : None
Replaces        : None
Installed Size  : 491,00 KiB
Packager        : Antonio Rojas <arojas@archlinux.org>
Build Date      : 2019-01-04T20:11:21 CET
Install Date    : 2019-01-07T00:38:53 CET
Install Reason  : Explicitly installed
Install Script  : No
Validated By    : Signature

[chansen@Georg ~]$ pacman -Ss pango
extra/libtiger 0.3.4-5
    A rendering library for Kate streams using Pango and Cairo
extra/pango 1:1.42.4-1 [installed]
    A library for layout and rendering of text
extra/pango-perl 1.227-9
    Perl bindings for Pango
extra/pangomm 2.42.0-1 [installed]
    C++ bindings for Pango
extra/pangomm-docs 2.42.0-1
    C++ bindings for Pango (documentation)
extra/pangox-compat 0.0.2+2+gedb9e09-3
    X Window System font support for Pango
extra/sdl_pango 0.1.2-7
    Pango SDL binding
community/haskell-pango 0.13.5.0-10
    Binding to the pango library for Gtk2Hs.
community/paps 0.7.0-1
    Converts Unicode text to postscript and pdf using pango
community/ruby-pango 3.3.1-1
    Ruby/Pango is a Ruby binding of pango-1.x.
multilib/lib32-pango 1:1.42.4-1
    A library for layout and rendering of text (32-bit)
[chansen@Georg ~]$ 
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using pango with convert

Post by snibgo »

I wouldn't use "-geometry" to resize an image, and I don't know what you want to do with your input image "wall". But this command:

Code: Select all

#!/bin/bash

convert \
  pango:@ptest.txt \
  -geometry x1080 \
  -fill '#00000050' \
  -draw "rectangle 50,50 530,970" \
  -pointsize 20 \
  -fill '#ffff99' \
  ptest_out.png
... with the file ptest.txt:

Code: Select all

# <span color='#ff00ff'>lol</span>
- lalala
- lalala
# <span color='#cc66cc'>Work
- Diz iz work!</span>
# <span color='#00ff00'>Fun and games</span>
... makes this result:
Image
I write to JPG purely to keep the size down for the web.
snibgo's IM pages: im.snibgo.com
lastchansen
Posts: 5
Joined: 2019-01-19T18:09:54-07:00
Authentication code: 1152

Re: Using pango with convert

Post by lastchansen »

Thank you so much for the reply :) Imagemagick is so cool, but sometimes there seem to be too many features.

The first "wall" is my wallpaper, the second is the output onto which the text is printed. I have set up a cronjob to update the image every 30 minutes as I often add/remove notes from the notes.md. Moreover I use ranger, which I have set to change wallpaper and run the script on the fly. I used `-geometry` since some of my wallpapers are 4k, which makes the notes _really_ small. I don't know if there is a better way to do this?

So, your suggested solution is great, but as seen in my screenshot, I need the text to be printed to the wallpaper and not a new image :) If this is not possible, then I'll dig around for another solution.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using pango with convert

Post by snibgo »

"pango:" creates a new image. This can have a transparent background so the result can be composited over another image, like this:

Code: Select all

convert \
  -background None \
  pango:@ptest.txt \
  -resize 267x233 \
  -fill '#00000050' \
  -draw "rectangle 50,50 530,970" \
  toes.png \
  +swap \
  -compose Over -composite \
  -quality 40 ptest_out2.jpg
Image
snibgo's IM pages: im.snibgo.com
lastchansen
Posts: 5
Joined: 2019-01-19T18:09:54-07:00
Authentication code: 1152

Re: Using pango with convert

Post by lastchansen »

Omg :D

That's exactly what I needed.

Thank you so much!
Post Reply