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

时间:2015-06-20 17:06:33

标签: image imagemagick montage

我正在使用

montage *.tif output.tif

将多个图像合并为一个。我现在希望它们被编号(有时1,2,3 ......; somtimes A,B,C ......) 有哪些可能性来标记组合中的单个图像?是否可以选择将标签放在图片下方,例如左上角或右下角?

不幸的是,我无法弄清楚如何使用-label命令来实现这一目标。感谢您的任何建议。

3 个答案:

答案 0 :(得分:3)

如果您想投入更多精力,您可以获得更多控制权。如果你这样做,你可以在你蒙太奇时标记“on-the-fly”,而不是将它们全部保存,然后对它们进行蒙太奇。您还可以更轻松地控制每行图像数量的宽度:

#!/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

enter image description here

它利用ImageMagick miff格式(即多图像文件格式)来连接所有输出图像并将它们发送到stdin命令的montage

或者,您可以像这样更改脚本:

#!/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

获取

enter image description here

或许这......

#!/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

enter image description here

或者用信件......

#!/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

enter image description here

答案 1 :(得分:2)

  

在组合中标记单个图像有哪些可能性?

使用 for 循环迭代。

var arr=["once a month","Once a Month","every month","month" ,"Month","monthly","Once A Month"]

if(arr.indexOf(RepeatDay)!==-1){
     RepeatDay='monthly';
}
  

是否可以选择将标签放在图片下方,例如左上角或右下角?

尝试将-annotate-gravity

一起使用
for(var i=0;i<arr.length;i++){
    if(arr[i]===RepeatDay){
        RepeatDay="monthly";
        break;   //no need to check for further after a match is found
    }
}

A rose

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

Rose B

答案 2 :(得分:1)

基于上面的回答,我编写了一个perl小脚本,用于将图形组合成带标签的子图,这可能对其他人有用。它在Mac上对我有效,但我并未真正对其进行测试。

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

其中几何格式为[#cols] x [#行],并且input_files接受通配符。如果标题中包含空格,则标题必须用 引起来。

我将其粘贴在下面,但也可以从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