In this article I'll demonstrate how we can convert the output of commands we type on the Linux command line to images using ImageMagick software. This software allows users to create and modify images. It's generally meant for use in an X windows environment but we'll be using it purely on the command line for the purposes of this article.
The ImageMagick software is not installed on Linux distributions by default but is available in the base repo so you don't have to search around for it.
Let's begin by installing the package.
[root@walk ~]# yum search ImageMagick
Loaded plugins: fastestmirror, security
Determining fastest mirrors
epel/metalink | 5.7 kB 00:00
* base: ftp.iitm.ac.in
* epel: ftp.yz.yamagata-u.ac.jp
* extras: ftp.iitm.ac.in
* jpackage-generic: sunsite.informatik.rwth-aachen.de
* updates: ftp.iitm.ac.in
base | 3.7 kB 00:00
epel | 4.3 kB 00:00
epel/primary_db | 5.9 MB 01:15
extras | 3.4 kB 00:00
jpackage-generic | 1.9 kB 00:00
spacewalk | 2.1 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 3.1 MB 00:12
===================================================== N/S Matched: ImageMagick =====================================================
ImageMagick-c++.i686 : ImageMagick Magick++ library (C++ bindings)
ImageMagick-c++.x86_64 : ImageMagick Magick++ library (C++ bindings)
ImageMagick-c++-devel.i686 : C++ bindings for the ImageMagick library
ImageMagick-c++-devel.x86_64 : C++ bindings for the ImageMagick library
ImageMagick-devel.i686 : Library links and header files for ImageMagick app development
ImageMagick-devel.x86_64 : Library links and header files for ImageMagick app development
ImageMagick-doc.x86_64 : ImageMagick html documentation
ImageMagick-perl.x86_64 : ImageMagick perl bindings
GraphicsMagick.i686 : An ImageMagick fork, offering faster image generation and better quality
GraphicsMagick.x86_64 : An ImageMagick fork, offering faster image generation and better quality
ImageMagick.i686 : An X application for displaying and manipulating images
ImageMagick.x86_64 : An X application for displaying and manipulating images
php-magickwand.x86_64 : PHP API for ImageMagick
php-pecl-imagick.x86_64 : Provides a wrapper to the ImageMagick library
Name and summary matches only, use "search all" for everything.
[root@walk ~]# yum install ImageMagick -y
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: ftp.iitm.ac.in
* epel: ftp.yz.yamagata-u.ac.jp
* extras: ftp.iitm.ac.in
* jpackage-generic: sunsite.informatik.rwth-aachen.de
* updates: ftp.iitm.ac.in
Resolving Dependencies
--> Running transaction check
---> Package ImageMagick.x86_64 0:6.7.2.7-6.el6 will be installed
--> Processing Dependency: libwmflite-0.2.so.7()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libwmf-0.2.so.7()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: librsvg-2.so.2()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libltdl.so.7()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libImath.so.6()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libIlmThread.so.6()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libIlmImf.so.6()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libIex.so.6()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libHalf.so.6()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Running transaction check
---> Package OpenEXR-libs.x86_64 0:1.6.1-8.1.el6 will be installed
---> Package ilmbase.x86_64 0:1.0.1-6.1.el6 will be installed
---> Package librsvg2.x86_64 0:2.26.0-14.el6 will be installed
--> Processing Dependency: libgsf >= 1.6.0 for package: librsvg2-2.26.0-14.el6.x86_64
--> Processing Dependency: libgsf-1.so.114()(64bit) for package: librsvg2-2.26.0-14.el6.x86_64
--> Processing Dependency: libcroco-0.6.so.3()(64bit) for package: librsvg2-2.26.0-14.el6.x86_64
---> Package libtool-ltdl.x86_64 0:2.2.6-15.5.el6 will be installed
---> Package libwmf.x86_64 0:0.2.8.4-25.el6_7 will be installed
---> Package libwmf-lite.x86_64 0:0.2.8.4-25.el6_7 will be installed
--> Running transaction check
---> Package libcroco.x86_64 0:0.6.2-5.el6 will be installed
---> Package libgsf.x86_64 0:1.14.15-5.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================================================
Package Arch Version Repository Size
====================================================================================================================================
Installing:
ImageMagick x86_64 6.7.2.7-6.el6 base 1.9 M
Installing for dependencies:
OpenEXR-libs x86_64 1.6.1-8.1.el6 base 197 k
ilmbase x86_64 1.0.1-6.1.el6 base 72 k
libcroco x86_64 0.6.2-5.el6 base 100 k
libgsf x86_64 1.14.15-5.el6 base 116 k
librsvg2 x86_64 2.26.0-14.el6 base 140 k
libtool-ltdl x86_64 2.2.6-15.5.el6 base 44 k
libwmf x86_64 0.2.8.4-25.el6_7 base 132 k
libwmf-lite x86_64 0.2.8.4-25.el6_7 base 51 k
Transaction Summary
====================================================================================================================================
Install 9 Package(s)
Total download size: 2.7 M
Installed size: 9.8 M
Downloading Packages:
(1/9): ImageMagick-6.7.2.7-6.el6.x86_64.rpm | 1.9 MB 00:07
(2/9): OpenEXR-libs-1.6.1-8.1.el6.x86_64.rpm | 197 kB 00:03
(3/9): ilmbase-1.0.1-6.1.el6.x86_64.rpm | 72 kB 00:00
(4/9): libcroco-0.6.2-5.el6.x86_64.rpm | 100 kB 00:00
(5/9): libgsf-1.14.15-5.el6.x86_64.rpm | 116 kB 00:00
(6/9): librsvg2-2.26.0-14.el6.x86_64.rpm | 140 kB 00:01
(7/9): libtool-ltdl-2.2.6-15.5.el6.x86_64.rpm | 44 kB 00:00
(8/9): libwmf-0.2.8.4-25.el6_7.x86_64.rpm | 132 kB 00:00
(9/9): libwmf-lite-0.2.8.4-25.el6_7.x86_64.rpm | 51 kB 00:00
------------------------------------------------------------------------------------------------------------------------------------
Total 111 kB/s | 2.7 MB 00:25
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Installing : ilmbase-1.0.1-6.1.el6.x86_64 1/9
Installing : libwmf-lite-0.2.8.4-25.el6_7.x86_64 2/9
Installing : libwmf-0.2.8.4-25.el6_7.x86_64 3/9
Installing : OpenEXR-libs-1.6.1-8.1.el6.x86_64 4/9
Installing : libtool-ltdl-2.2.6-15.5.el6.x86_64 5/9
Installing : libgsf-1.14.15-5.el6.x86_64 6/9
Installing : libcroco-0.6.2-5.el6.x86_64 7/9
Installing : librsvg2-2.26.0-14.el6.x86_64 8/9
Installing : ImageMagick-6.7.2.7-6.el6.x86_64 9/9
Verifying : libwmf-lite-0.2.8.4-25.el6_7.x86_64 1/9
Verifying : librsvg2-2.26.0-14.el6.x86_64 2/9
Verifying : ImageMagick-6.7.2.7-6.el6.x86_64 3/9
Verifying : libwmf-0.2.8.4-25.el6_7.x86_64 4/9
Verifying : libcroco-0.6.2-5.el6.x86_64 5/9
Verifying : OpenEXR-libs-1.6.1-8.1.el6.x86_64 6/9
Verifying : libgsf-1.14.15-5.el6.x86_64 7/9
Verifying : libtool-ltdl-2.2.6-15.5.el6.x86_64 8/9
Verifying : ilmbase-1.0.1-6.1.el6.x86_64 9/9
Installed:
ImageMagick.x86_64 0:6.7.2.7-6.el6
Dependency Installed:
OpenEXR-libs.x86_64 0:1.6.1-8.1.el6 ilmbase.x86_64 0:1.0.1-6.1.el6 libcroco.x86_64 0:0.6.2-5.el6
libgsf.x86_64 0:1.14.15-5.el6 librsvg2.x86_64 0:2.26.0-14.el6 libtool-ltdl.x86_64 0:2.2.6-15.5.el6
libwmf.x86_64 0:0.2.8.4-25.el6_7 libwmf-lite.x86_64 0:0.2.8.4-25.el6_7
Complete!
[root@walk ~]#
With the package and it's dependencies installed let's query the rpm to get some information about the package.
[root@walk ~]# rpm -qa | grep -i Image
genisoimage-1.1.9-12.el6.x86_64
ImageMagick-6.7.2.7-6.el6.x86_64
[root@walk ~]# rpm -qi ImageMagick-6.7.2.7-6.el6.x86_64
Name : ImageMagick Relocations: (not relocatable)
Version : 6.7.2.7 Vendor: CentOS
Release : 6.el6 Build Date: Wed 22 Mar 2017 04:54:38 PM IST
Install Date: Sat 26 Aug 2017 12:35:53 AM IST Build Host: c1bm.rdu2.centos.org
Group : Applications/Multimedia Source RPM: ImageMagick-6.7.2.7-6.el6.src.rpm
Size : 7321917 License: ImageMagick
Signature : RSA/SHA1, Thu 23 Mar 2017 08:28:57 PM IST, Key ID 0946fca2c105b9de
Packager : CentOS BuildSystem <http://bugs.centos.org>
URL : http://www.imagemagick.org/
Summary : An X application for displaying and manipulating images
Description :
ImageMagick is an image display and manipulation tool for the X
Window System. ImageMagick can read and write JPEG, TIFF, PNM, GIF,
and Photo CD image formats. It can resize, rotate, sharpen, color
reduce, or add special effects to an image, and when finished you can
either save the completed work in the original format or a different
one. ImageMagick also includes command line programs for creating
animated or transparent .gifs, creating composite images, creating
thumbnail images, and more.
ImageMagick is one of your choices if you need a program to manipulate
and display images. If you want to develop your own applications
which use ImageMagick code or APIs, you need to install
ImageMagick-devel as well.
The ImageMagick software provides the convert command which I found very diverse because it allows the user to draw, crop and resize images along with many other things. You can get more information about it by going through the man page.
[root@walk ~]# which convert
/usr/bin/convert
[root@walk ~]#
I intended to use the convert command along with the label feature to pipe out the STDOUT of a command run on the shell to draw that text on an image.
I tried hard to get this to work but I kept getting the below error while testing.
After a lot of frantic searching I came to know that using the labels feature has been deliberately disabled in RedHat Linux/CentOS systems. I cannot comment on the authenticity of this but after reading it I began to look for alternatives to achieve the desired outcome with convert and without using label.
Finally after a lot of searching I came up with this one liner that did the job:
convert -size 1000x600 -gravity Northwest xc:khaki -font Courier-Bold -fill black -pointsize 18 -draw "text 5,15 '$(cat /etc/passwd)'" passwd.png
This command generated the following passwd.png image in the current working directory of my CenOS system.
Let's deconstruct the above convert command options:
-size option determines the size of the image.
-gravity option determines the position of text on the image.
-xc option decides the image background or the canvas color.
-font option determines the font type of the text being drawn on the image.
-fill option decides the text color.
-pointsize determines the font size of the text being drawn.
-draw option provides the text to be drawn on the image.
For this demonstration I've done a command substitution of the "fdisk -l" command output to serve as the text to be drawn on the image.
The final parameter passwd.png is the name we'd like for the resulting image.
We can use the list flag with the convert keyword to view the available options for the various flag that we use.
For example:
[root@walk ~]# convert -list font | less
[root@walk ~]#
[root@walk ~]# convert -list gravity
None
Center
East
Forget
NorthEast
North
NorthWest
SouthEast
South
SouthWest
West
Static
Let's take another example changing a few of the options. Let's create an image with the output of "cat /etc/passwd" command.
convert -size 1000x600 -gravity Northwest xc:khaki -font Courier-Bold -fill black -pointsize 18 -draw "text 5,15 '$(cat /etc/passwd)'" passwd.png
This time I modified the image size, canvas color and font size along with the text to be drawn as I anticipated the output to be larger thereby requiring a larger canvas to display the text.
If we could use labels then the label feature would autosize the text for us to fit on the image.
We can easily change the image format by simply changing the extension of the final image to be created.
To validate, let's create a JPEG image of the df -h command output.
[root@walk ~]# convert -size 700x200 -gravity Northwest xc:white -font Courier-Bold -fill blue -pointsize 18 -draw "text 5,15 '$(df -h)'" df.jpeg
This command generates the below image:
I hope you found this article useful and perhaps ImageMagick gave you some interesting ideas.