imagelayout - arrange images into a large image according to a layout
imagelayout(.py) [-w|--watch] [-s] [-o outputfile] [-h|--help] configfile [imagefile [imagefile ...]]
NOTE: Depending on how you installed the program, the command is either imagelayout.py or just imagelayout.
imagelayout arranges several images according to a predefined layout and produces a larger image consisting of the sub-images. It can optionally label the individual images. Additional features include the ability to crop and add borders to the images, to add a border and a title to the final image, and to add arbitrary labels and lines or arrows to the final image. The intended use of imagelayout is the creation of figures from sub-figures for articles and science publications. imagelayout is a command-line Python application guided by a configuration file configfile (provided as the only command line argument); it has no graphical interface or interactive features. Thus, it is suitable for use in scripts, Makefiles, and automated and reproducible workflows.
- -w, --watch
Run once, then watch the configuration file for changes and re-run whenever a
change is detected. This is useful for developing the configuration file and making adjustments. Tip: View the output image file with a viewer that also watches the image file and reloads it when it changes.qiv
for Linux systems is such a viewer, e.g.qiv -T out.png
will reloadout.png
as soon as it changes. - -s
Report the pixel sizes of input images and exit. - -o outputfile
Name of the output image file (optional) - -h, --help
Display short help message and exit. - configfile
The configuration file (yaml format) - imagefile(s)
Optionally, the image files to be used as inputs can be specified here.
The desired layout of the images can be defined as a combination of
horizontally and vertically joined images. Two keywords are used for
this in the configuration file. The hjoin
keyword defines a
horizontally joined image sequence, e.g.
layout:
hjoin: [A, B, C]
will result in the following image arrangement:
+---+---+---+
| A | B | C |
+---+---+---+
while the vjoin
keyword produces a vertical arrangement:
layout:
vjoin: [A, B, C]
will result in this arrangement:
+---+
| A |
+---+
| B |
+---+
| C |
+---+
More complex layouts can be defined by combining and nesting the
hjoin
and vjoin
specifications. For example,
hjoin:
- vjoin:
- hjoin: [A, B]
- C
- D
results in this layout:
+---+---+---+
| A | B | |
+---+---+ D |
| C | |
+-------+---+
Horizontally joined images will be resized to fit the same height (the highest image), and vertically joined images will be resized to fit the same width (the widest image). If the fixedsize option is used for an image, it will be padded rather than scaled.
The names of the input and output image file names can be specified in the configuration
file. However, to facilitate the use of imagelayout.py
in scripts
and Makefiles, the file names can also be provided on the command line.
Input file names can be provided on the command line after the
configuration file name. In this case, you can refer to them in the
configuration file as $1
, $2
, etc. For example, if the command
line is
imagelayout.py config.yaml image1.png image2.jpg image3.png
then you can refer to the three input image files in the configuration file as follows:
images:
A: $1
B: $2
C: $3
To specify the output file name on the command line, the -o
option
can be used. In this case, you can refer to it in the configuration
file as any string starting with a $
(a single $
is enough). For
example, if the command line is
imagelayout.py -o output.png config.yaml
then you refer to the output file in the configuration file as follows:
outputfile: $
You can use special file names of the format BLANK-
Wx
H, e.g.
BLANK-640x480
. In this case, a blank image will be generated by
imagelayout.py
with the given width and height. The color of the
blank image will be determined by the paddingcolor
keyword (or white
by default). The blank images can be used as placeholders. For
example, if you have 5 images and you want to arrange them into a 3x2
grid and leave the bottom right cell empty then you can use the
following configuration:
images:
A: A.png
B: B.png
C: C.png
D: D.png
E: E.png
blank: BLANK-640x480
layout:
vjoin:
- hjoin: [A, B, C]
- hjoin: [D, E, blank]
The resulting image will look like this:
+---+---+---+
| A | B | C |
+---+---+---+
| D | E | |
+---+---+---+
By default, the identifiers of the blank images will not be displayed in the final image.
The only mandatory command line argument to imagelayout is the configuration file configfile, which should be written as a YAML format file, and contains all information about the input/output files, the layout, and the requested operations.
The two main mandatory parts of the configuration file are the images section and the layout section; in addition the output file must also be specified using the outputfile keyword. The simplest configuration file may look something like this:
images:
A: imageA.png
B: imageB.png
outputfile: out.png
layout:
hjoin: [A, B]
This will produce the file out.png
consisting of imageA.png
and
imageB.png
joined horizontally. If the
labels:
add: yes
lines are added then each image will be labeled in the top left corner with the letters A and B, respectively.
The configuration file may contain numerous other keywords and options to specify image labels, font names, font sizes and colors, cropping images or adding borders to them, adding a title, adding arbitrary labels, lines, and arrows to the final image. The detailed list of keywords and options follows.
Note on font names: On Linux, the system fonts may be listed using the
fc-list
command. On Windows, look in the \Windows\Fonts
directory. Use
the font file names without the extension (e.g. .ttf
) as font names.
For example, bold Arial is specified in Linux as Arial_Bold
(assuming the msttcorefonts
package is installed), but on
Windows as arialbd
(as the \Windows\Fonts
directory contains the
arialbd.ttf
file).
-
inputdir: directoryname
Input directory; input image files will be loaded from here -
images:
Input images are specified here. Each file must have an identifier, and either a single file name or a number of parameters. Note: A file name like$1
,$2
, etc. indicates that the file name should be taken from the command line (see manual). If the file name isBLANK-
Wx
H, e.g.BLANK-640x480
then a blank image of the given size will be generated; this can be used as a placeholder; identifiers of blank images will not be displayed as labels.- imageid: filename
An identifier and a filename. If you don't want to specify any further parameters for the image other than the filename then this form can be used. Otherwise, see below. - imageid:
An identifier for an image- file: filename
Name of the image file - label: label
A label for the image. A text string if you don't want to specify any parameters such as position, font, color, etc; in this case the parameters specified in the toplevel labels option will be used. If you want to specify further parameters here then provide a mapping (see next line). Optional; if omitted then the image identifier will be used for labeling. - label:
A label for the image along with parameters such as position, font, etc. These parameters will override those given in the toplevel labels option.- text: labeltext
Text of the label; if omitted then the image identifier will be used. - fontname: fontname
Name of a truetype font available on the system. Example:Arial_Bold
. - fontsize: size
Font size in pixels. - fontcolor: colorspec
Font color. A color name likered
, an RGB color specified as#rrggbb
orrgb(red, green, blue)
with values between 0 and 255 or percentages, or a HSL color ashsl(hue, saturation%, lightness%)
. - pos: (
top
|center
|bottom
)-
(left
|center
|right
)
Position of the label relative to the image. A combination of the words (top
,center
,bottom
) and (left
,center
,right
), separated with a dash (no space in between!). Default:top-left
. - offset: [ xoffset, yoffset ]
Offset, in pixels, for the label, relative to the position defined in pos. Default:[0, 0]
- text: labeltext
- fixedsize:
no
|yes
Ifyes
, the image will never be resized during the layout (it may still be resized along with the final image if the finalwidth or finalheight option is used). Instead, padding will be used (either on the top and bottom or on left and right of the image) to fit the image to its neighbors. The color of the padding is determined by the paddingcolor toplevel parameter (default is white). - autocrop:
no
|yes
Ifyes
, the image will be autocropped using the color of the top-left pixel. - crop: width | [ xwidth, ywidth ] | [ leftwidth, topwidth, rightwidth, bottomwidth ]
Crop the image on all four sides. A single number specifies the width of a uniform crop. Two numbers refer to the left/right and top/bottom crop widths. If four numbers are given, the crop widths of all four sides can be specified separately. Optional. - border:
Optionally add a border to the image.- size: width | [ xwidth, ywidth ] | [ leftwidth, topwidth, rightwidth, bottomwidth ]
Border size. A single number, or a list of 2 or 4 numbers. A single number specifies the width of a uniform border. Two numbers refer to the left/right and top/bottom widths of the border. If four numbers are given, the widths of the border on all four sides can be specified separately. If not specified then the size defined under the top-level border property will be used; or zero if there is no top-level border property. - color: colorspec
Color of the border. A color name likered
, an RGB color specified as#rrggbb
orrgb(red, green, blue)
with values between 0 and 255 or percentages, or a HSL color ashsl(hue, saturation%, lightness%)
. Default: white.
- size: width | [ xwidth, ywidth ] | [ leftwidth, topwidth, rightwidth, bottomwidth ]
- file: filename
- imageid: ...
Any number of further images can be specified.
- imageid: filename
-
layout:
The layout of the output image is specified here. It can be omitted if there is only one input image.-
vjoin | hjoin:
Join the following images vertically (vjoin) or horizontally (hjoin). Must be followed by a list of the images to join. List elements can be further hjoin/vjoin lists and individual images. The list can be specified either on the same line in bracket notation (e.g.vjoin: [A, B, C]
) or on separate lines using the dash notation, e.g.vjoin: - A - B - C
-
- vjoin: | hjoin: | imageid
List element: another list of images to join vertically or horizontally, or an image identifier for an individual image. -
...
Images and vjoin/hjoin lists can be arbitrarily combined and nested. Example:vjoin: - hjoin: [A, B, C] - hjoin: - D - vjoin: [E, F]
-
-
-
outputfile: filename
Name of the output file relative to the current directory. The extension will determine the image file type. See Pillow documentation for the available file formats. Note: If the file name starts with a$
(it can be a single$
character) then it is taken from the command line-o
option. -
finalwidth: size
The final width of the output image after joining the individual images. The image will be resized to fit this value, retaining the aspect ratio unless finalheight is also provided. Note that this is before the title and the globalborder are added. Optional. If not given, and finalheight is also omitted, the image will not be resized. -
finalheight: size
The final height of the output image after joining the individual images. The image will be resized to fit this value, retaining the aspect ratio unless finalwidth is also given. Note that this is before the title and the globalborder are added. Optional. If not given, and finalwidth is also omitted, the image will not be resized. -
resizemethod:
nearest
|bilinear
|bicubic
|lanczos
Which algorithm to use to resize the individual images. Default:nearest
. -
pixelscaling: scale | [ xscale, yscale ]
Apply this scaling factor to all pixel sizes. Default: 1. This parameter allows one to easily resize the output image without having to separately adjust the font sizes, border sizes, offsets, line coordinates, etc. For example,pixelscale: 0.5
will result in a half-size image,pixelscale: 2.0
in a double-size image. -
autocrop:
no
|yes
If set toyes
, all individual images will be autocropped. This can be overridden with the autocrop parameter for any individual image. -
paddingcolor: colorspec
Color to use for the padding if the fixedsize option is used for any image. This will also be the background color of the whole image, which will become visible if any of the input images has transparency or an alpha channel. Default: white. -
border:
Draw a border around around each individual image before joining them. The border parameters defined here will be used as defaults, and can be overridden for each indivual image.- size: width | [ xwidth, ywidth ] | [ leftwidth, topwidth, rightwidth, bottomwidth ]
Border size. A single number, or a list of 2 or 4 numbers. A single number specifies the width of a uniform border. Two numbers refer to the left/right and top/bottom widths of the border. If four numbers are given, the widths of the border on all four sides can be specified separately. Default: 10. - color: colorspec
Color of the border. A color name likered
, an RGB color specified as#rrggbb
orrgb(red, green, blue)
with values between 0 and 255 or percentages, or a HSL color ashsl(hue, saturation%, lightness%)
. Default: white.
- size: width | [ xwidth, ywidth ] | [ leftwidth, topwidth, rightwidth, bottomwidth ]
-
labels:
Specify how labels should be added to the individual images. These settings can be overridden for each individual image.- add:
yes
|no
Whether to add labels. Default: no. - fontname: fontname
Name of a truetype font available on the system. Example:Arial_Bold
. Default: FreeSans. - fontsize: size
Font size in pixels. Default: 32 - fontcolor: colorspec
Font color. A color name likered
, an RGB color specified as#rrggbb
orrgb(red, green, blue)
with values between 0 and 255 or percentages, or a HSL color ashsl(hue, saturation%, lightness%)
. Default: black. - pos: (
top
|center
|bottom
)-
(left
|center
|right
)
Position of the label relative to the image. A combination of the words (top
,center
,bottom
) and (left
,center
,right
), separated with a dash (no space in between!). Default:top-left
. - offset: [ xoffset, yoffset ]
Offset, in pixels, for the label, relative to the position defined in pos. Default:[0, 0]
- add:
-
globallabels:
Add arbitrary labels to the final image. Note: they will be added before adding the globalborder.- fontname: fontname
Name of a truetype font available on the system. Example:Arial_Bold
. Optional, can be overridden for each label. - fontsize: size
Font size in pixels. Optional, can be overridden for each label. - fontcolor: colorspec
Font color. A color name likered
, an RGB color specified as#rrggbb
orrgb(red, green, blue)
with values between 0 and 255 or percentages, or a HSL color ashsl(hue, saturation%, lightness%)
. Optional, can be overridden for each label. - labellist:
List the labels.- -
Specify a label- text: labeltext
Text of the label. To specify a multi-line label, put it in double quotes and use\n
to indicate line breaks, e.g."First line\nsecond line"
. Use the align property to specify the justification of lines. - coords: [ x, y ]
Coordinates to place the label at. - fontname: fontname
Name of a truetype font available on the system. Example:Arial_Bold
. Optional. - fontsize: size
Font size in pixels. Optional. - fontcolor: colorspec
Font color. A color name likered
, an RGB color specified as#rrggbb
orrgb(red, green, blue)
with values between 0 and 255 or percentages, or a HSL color ashsl(hue, saturation%, lightness%)
. Optional. - align:
left
|center
|right
Justification of lines in multi-line labels.
- text: labeltext
- -
Another label.- ...
Arbitrary number of further labels can be specified.
- ...
- -
- fontname: fontname
-
title:
To add a title to the final image. It will always be placed top-center in a separate title bar.- add:
no
|yes
Whether to add a title. Default: no. - text: title
Title text, any string. - fontname: fontname
Name of a truetype font available on the system. Example:Arial_Bold
. Default: FreeSans. - fontsize: size
Font size in pixels. Default: 36 - fontcolor: colorspec
Font color. Default: black. A color name likered
, an RGB color specified as#rrggbb
orrgb(red, green, blue)
with values between 0 and 255 or percentages, or a HSL color ashsl(hue, saturation%, lightness%)
. - bgcolor: color
Background color for the title bar. Default: white. A color name likered
, an RGB color specified as#rrggbb
orrgb(red, green, blue)
with values between 0 and 255 or percentages, or a HSL color ashsl(hue, saturation%, lightness%)
. - height: size
Height of the title bar in pixels. The title will be placed in the middle of the title bar. Default: 1.1*fontsize
- add:
-
lines:
Add arbitrary lines or arrows to the final image. Note: they will be added before the globalborder.- width: width
Line width. Optional, can be ovverridden for each line. Default: 3. - color: colorspec
Line color. A color name likered
, an RGB color specified as#rrggbb
orrgb(red, green, blue)
with values between 0 and 255 or percentages, or a HSL color ashsl(hue, saturation%, lightness%)
. - linelist:
List the lines.- -
- fromto: [ x1, y1, x2, y2 ]
Coordinates of start and end of line. Mandatory. Further line segments can be specified by continuing the list with x3, y3, x4, y4, ... - arrowsize: length
Length of the small lines forming the arrowhead. Default: 0. If not provided or zero, no arrow will be drawn. Optional. - width: width
Line width. Optional. - color: colorspec
Line color. A color name likered
, an RGB color specified as#rrggbb
orrgb(red, green, blue)
with values between 0 and 255 or percentages, or a HSL color ashsl(hue, saturation%, lightness%)
.
- fromto: [ x1, y1, x2, y2 ]
- -
Any number of further lines can be defined- ...
Another line specification
- ...
- -
- width: width
-
globalborder:
Draw a border around the final image. It will be added after adding the title.- size: width | [ xwidth, ywidth ] | [ leftwidth, topwidth, rightwidth, bottomwidth ]
Border size. A single number, or a list of 2 or 4 numbers. A single number specifies the width of a uniform border. Two numbers refer to the left/right and top/bottom widths of the border. If four numbers are given, the widths of the border on all four sides can be specified separately. - color: colorspec
Color of the border. A color name likered
, an RGB color specified as#rrggbb
orrgb(red, green, blue)
with values between 0 and 255 or percentages, or a HSL color ashsl(hue, saturation%, lightness%)
.
- size: width | [ xwidth, ywidth ] | [ leftwidth, topwidth, rightwidth, bottomwidth ]
imagelayout.py is a Python 3.x app and requires the following modules to be installed:
The original imagelayout.py is hosted at github, and licensed as GPL-3.0.