简体   繁体   English

使用 ImageMagick 蒙太奇编号/标记图像(例如 1、2、3 或 A、B、C)

[英]Numbering/Labeling images (eg 1,2,3 or A,B,C) with ImageMagick montage

I'm using我正在使用

montage *.tif output.tif

to combine several images to one.将多张图像合二为一。 I would now like them to be numbered (sometimes 1,2,3 …; somtimes A,B,C …) What possibilities are there to label the single images in the combined?我现在希望对它们进行编号(有时是 1、2、3 ……;有时是 A、B、C……)有什么可能性可以在组合中标记单个图像? Is there an option to put the label not underneath the picture but eg in the upper left or lower right corner?是否可以选择不将标签放在图片下方,而是将标签放在左上角或右下角?

Unfortunately I could't really figure out how to use the -label command to achieve that.不幸的是,我无法真正弄清楚如何使用 -label 命令来实现这一目标。 Thanks for any suggestions.感谢您的任何建议。

If you want to invest a little more effort you can have more control.如果你想投入更多的努力,你可以拥有更多的控制权。 If you do it like this, you can label the images "on-the-fly" as you montage them rather than having to save them all labelled and then montage them.如果您这样做,您可以蒙太奇时“即时”标记图像而不必保存所有标记然后蒙太奇。 You can also control the width, in terms of number of images per line, more easily:您还可以更轻松地控制每行图像数量的宽度:

#!/bin/bash
number=0
for f in *.tif; do
   convert "$f" -gravity northwest -annotate +0+0 "$number" miff:-
   ((number++))
done | montage -tile x3 - result.png

在此处输入图片说明

It takes advantage of ImageMagick miff format, which means Multiple Image File Format, to concatenate all the output images and send them into the stdin of the montage command.它利用 ImageMagick miff格式,即多图像文件格式,连接所有输出图像并将它们发送到montage命令的stdin中。

Or, you can change the script like this:或者,您可以像这样更改脚本:

#!/bin/bash
number=0
for f in *.tif; do
   convert "$f" -gravity northwest -fill white -annotate +0+0 "$number" miff:-
   ((number++))
done | montage -tile 2x - result.png

to get要得到

在此处输入图片说明

Or maybe this...或者也许这...

#!/bin/bash
number=0
for f in *.tif; do
   convert "$f" -gravity northwest -background gray90 label:"$number" -composite miff:-
   ((number++))
done | montage -tile 2x - result.png

在此处输入图片说明

Or with letters...或者用字母...

#!/bin/bash
number=0
letters="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for f in *.tif; do
   label=${letters:number:1}
   convert "$f" -gravity northwest -background gray90 label:"$label" -composite miff:-
   ((number++))
done | montage -tile 2x - result.png

在此处输入图片说明

What possibilities are there to label the single images in the combined?有什么可能性可以在组合中标记单个图像?

Iterate with a for loop.使用for循环进行迭代。

for INDEX in {A,B,C}; do
    convert ${INDEX}.jpg labeled_${INDEX}.jpg
done

Is there an option to put the label not underneath the picture but eg in the upper left or lower right corner?是否可以选择不将标签放在图片下方,而是将标签放在左上角或右下角?

Try using the -annotate with -gravity尝试使用-annotate-gravity

convert rose: -fill white \
        -gravity NorthWest -annotate +0+0 "A" \
        A.png

一朵玫瑰

convert rose: -fill white \
        -gravity SouthEast -annotate +0+0 "B" \
        B.png

玫瑰B

Based on the answers above, I wrote a small perl script to combine figures into labelled subfigures, which might be useful to others.根据上面的答案,我编写了一个小的 perl 脚本来将数字组合成标记的子图,这可能对其他人有用。 It works for me on a Mac, but I didn't really test it.它在 Mac 上对我有用,但我没有真正测试过它。

Usage: pics2grid geometry output_file [-title title] [-bycols] input_files

where the geometry format is [# cols]x[# rows] and input_files accept wild cards.其中几何格式为 [# cols]x[# rows] 并且 input_files 接受通配符。 If your title contains spaces, the title must be quoted and the spaces escaped.如果您的标题包含空格,则必须引用标题转义空格。

I paste it below, but it can also be downloaded from http://endress.org/progs.html#subfigs我把它贴在下面,但也可以从http://endress.org/progs.html#subfigs下载

#!/usr/bin/perl -l

use strict;
use warnings;
use autodie;
use Getopt::Long 'HelpMessage';

### Process options and arguments ###
if (@ARGV < 3){
  HelpMessage(1);
}

if (($ARGV[0] !~ /^\d+x/) && ($ARGV[0] !~ /x\d+$/)){
  HelpMessage(1);
}

my $geometry = shift @ARGV;

my $output_file = shift @ARGV;

my $title = "";
my $bycols = "";

GetOptions ('bycols' => \$bycols,
        'title=s' => \$title,
        'help'     =>   sub { HelpMessage() },
       ) or HelpMessage(1);


# Wildcards are automatically expanded in @ARGV, but just make sure that they are
my @input_files = map { glob } @ARGV;

$title = "-title $title"
  if ($title);

if ($bycols){
  die "When option -bycols is set, a fully specified geometry is needed."
    unless ($geometry =~ /^\d+x\d+$/);
 }


 @input_files = reorder_input_files (\@input_files, $geometry)
   if ($bycols);

### Define the labels for the subfigures. If you want different figures, change it here. ###
my @labels = "a".."z";
@labels = @labels[0..$#input_files];
@labels = reorder_input_files (\@labels, $geometry)
   if ($bycols);


my $pic_data;

foreach my $f (@input_files){
  # Pictures are combined by rows
  my $lab = shift @labels;
  $pic_data .= `convert \"$f\" -pointsize 48 -font Arial-Regular -gravity northwest -annotate +20+10 \'$lab\' \\
                -bordercolor black -border 1 \\
                 miff:-`;

}

open (OUT,  "| montage -tile $geometry -geometry +0+0 -background white -pointsize 60 -font Arial-Regular $title - $output_file");
print OUT $pic_data;
close (OUT);

sub reorder_input_files {

  my ($input_files, $geometry) = @_;

  my ($ncols, $nrows) = split (/x/, $geometry);

  my @rows = ([]) x $nrows;

  foreach my $i (0..$#$input_files){

    my @tmp_array = @{$rows[$i % $nrows]};
    push (@tmp_array, $input_files->[$i]);
    $rows[$i % $nrows] = \@tmp_array;

  }

  my @reordered_files = ();

  map {push (@reordered_files, @$_)} @rows;

  return (@reordered_files);

}

=head1 NAME

pics2grid - arrange pictures on a grid of sub-figures and number the individual pictures with letters

=head1 SYNOPSIS

  pics2grid geometry output [-title title] [-bycols] inputfiles

  The inputfiles argument accepts wild cards.

  Geometry format: [\# cols]x[\# rows]. 
                   Unless you set the option -bycols, specifying either 
                   the number of rows or of columns is sufficient. 

  Options:
    --title,-t      Title. If your title contains spaces, the title needs 
                    to be quoted *and* the spaces need to be escaped. 
    --bycols,-b     Arrange and number pictures by columns rather than rows
    --geometry,-g   Not yet implemented
    --help,-h       Print this help message

  Examples:
    # Create grid with 3 columns and 2 rows in which images are arranged by *rows*
    pics2grid.pl 3x2 output.pdf input1.png input2.png input3.png\
                                input4.png input5.png input6.png

    # Create grid with 2 columns and 3 rows in which images are arranged by *columns*
    pics2grid.pl 2x3 output.pdf -bycols input1.png input2.png input3.png\
                                        input4.png input5.png input6.png

    # Same as above but with a title including spaces. 
    # Note that the title needs to be quoted *and* the space needs to be escaped 
    # (i.e., put \ in front of the space)
    pics2grid.pl 2x3 output.pdf -bycols -title "My\ title" input1.png\
                 input2.png input3.png input4.png input5.png input6.png

    # Create grid with 4 columns of all png files in the current directory. Images 
    # are arranged by *columns*.
    # It will stop labeling the subfigures for more than 26 images
    pics2grid.pl 4x output.pdf *.png

=head1 VERSION

0.01

=cut

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM