I am trying to write to a specific directory/folder on my drive when running an imagemagick bash script.
I have come so far that I can giev arguments to my script in order to use the argument values to create a new directory (folder) into where I want to output an imagemagick file. I manage to create a new directory using the passed in arguments to the mkdir command in my bash script. However, the script gets stuck at the end of the execution of the mkdir command and never moves on.
This results in my dir being created, but I never get the chance to write to a file inside my new dir. I can later find the created dir but there is nothing in it. I have tried two different ways to create my dir and both get stuck.
When I call the script in my terminal it does all the steps until it gets to the mkdir command and then my terminal freezes, like it is waiting for something to happen so it can move on - hitting ENTER multiple times doesn't help either ;) - and I don't get the feeling it is stuck in a loop.
I am running a mkdir command as a response to err msgs I received when trying to output to the output dir without having the output dir created before writing to it.
I'm running bash on OSX El Capitan and below are selected fragments of the script relevant to my problem.
#!/bin/bash
args=("$#")
employer=${args[2]}
position=${args[3]}
output_dir="${employer} - ${position}"
# mkdir -pv -m u=rwx "$output_dir"
[ -d "$output_dir" ] && echo "Output Directory Exists" || mkdir -pv -m u=rwx "$output_dir"
# imagemagick script where a new image file gets written to my new output_dir
# the final string is the output path of my new imagemagick image output
convert - $logo_path -gravity center -geometry 350x350+280+70 -composite -set
filename:dimensions '%wx%h'
"${output_dir}/LN-${employer}-${position}-%[filename:dimensions].png"
EDIT:
After the first comments and response I feel obliged to give you guys the full code, perhaps there is something for you there to find. =) Also, as I mention in my comment below, clearing up the shell scripting issues following ShellCheck's directions (as advised by #Cyrus in the comments) messed up the imagemagick commands and the output failed. Therefore I have reverted back to old code in order to correct the code step by step from there, while maintaining the desired output the old (and buggy, according to ShellCheck) code produced. See code below.
The changes I made while debugging with ShellCheck did not fix the main problem with the hanging mkdir command. I am still failing when creating a new dir inside my $folder_path, cause it never moves on to actually outputting the image with the last convert command.
Additional problems I ran into after debugging with ShellCheck was that $custpath doesn't work at all when I try to use it inside the convert commands, which is why I use the entire value of $custpath in the convert commands for now. I have tried to use $"{custpath[#]}" and $"{custpath[*]}" as ShellCheck suggests and none of them succeed in creating the output, because - guess what - using $custpath hangs the script.
What I am trying to do is to put the output image inside
$custpath$folder_path$output_dir folder.
Here is the entire script:
#!/bin/bash -x
# Version: ImageMagick 6.9.6-7 Q16 x86_64 2016-12-05, running on macOSX 10.11.6 El Capitan
# run the script in bash terminal like so:
# ./my_script.sh date1 date2 "employer" "position" "path to logo img"
# remember to chmod the file to be able to run it: chmod +x my_script.sh
# this script is partially debugged with http://www.shellcheck.net/
args=("$#")
echo
echo date1 = "${args[0]}"
echo date2 = "${args[1]}"
echo employer = "${args[2]}"
echo position = "${args[3]}"
echo logo_path = "${args[4]}"
custpath=($HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/)
echo custpath = "${custpath[#]}"
date1=${args[0]}
date2=${args[1]}
employer=${args[2]}
position=${args[3]}
logo_path=${args[4]}
# find the folder in the logo path
# see this article for how to replace a pattern from the end
# http://www.thegeekstuff.com/2010/07/bash-string-manipulation/
folder_path=${logo_path/%\/*//}
echo folder_path = $folder_path
output_dir="${employer} - ${position}"
echo output_dir = $output_dir
echo
convert \( -draw 'circle 108.5,101.5 159.5,160' -stroke "rgb(241,142,0)" -fill "rgb(241,142,0)" -size 1022x798 canvas:white -bordercolor black -border 1x1 -fill black -stroke black -draw 'rectangle 1,721 1022,798' -fill white -stroke none -background none -gravity south -page +93-11.5 -font /Library/Fonts/RobotoCondensed-Light.ttf -pointsize 35.5 label:'A Logo' -flatten \) $HOME/Dropbox/+dev/coding_images/imagemagick/a-logo-neg.png -geometry 207x+386+6.5 -composite miff:canvas0
convert -size 125x150 -background none -gravity center -stroke none -fill white -interline-spacing -7 -pointsize 33 -font /Library/Fonts/Roboto-Bold.ttf label:"Last\ndate\n${date1}/${date2}" miff:- |
composite -gravity center -geometry -402-300 - canvas0 miff:- |
convert - -size 255x150 -background none -gravity west -stroke none -fill black -kerning 0.5 -font /Library/Fonts/RobotoCondensed-Regular.ttf label:"${employer}" -geometry +33-102 -composite miff:- |
convert - -size 486x320 -background none -gravity west -stroke none -fill black -kerning 0.25 -interline-spacing -5 -font /Library/Fonts/Roboto-Regular.ttf caption:"${position}" -geometry +32+87 -composite miff:- |
# convert - $HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/$logo_path -gravity center -geometry 350x350+280+70 -composite -set filename:dimensions '%wx%h' "$HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/${folder_path}LN-${employer}-${position}-${date1}_${date2}-%[filename:dimensions].png"
# mkdir -pv -m u=rwx "$output_dir"
# [ -d "$output_dir" ] && echo "Output Directory Exists" || mkdir -pv -m u=rwx "$output_dir"
# cd "$output_dir"
# below is the final version of the output path I want to have, if only the $custpath variable worked
# convert - $logo_path -gravity center -geometry 350x350+280+70 -composite -set
# filename:dimensions '%wx%h'
# "${custpath}/${folder_path}/${output_dir}/LN-${employer}-${position}-%[filename:dimensions].png"
convert - $HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/"$logo_path" -gravity center -geometry 350x350+280+70 -composite -set filename:dimensions '%wx%h' $HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/"$folder_path"LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-'%[filename:dimensions]'.png
convert - $"{custpath[#]}"/"$logo_path" -gravity center -geometry 350x350+280+70 -composite -set filename:dimensions '%wx%h' $HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/"$folder_path"LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-'%[filename:dimensions]'.png
rm canvas0
# open $HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/"$folder_path"LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-%[filename:dimensions].png
open $HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/"$folder_path"LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-*.png
# remove output file when testing the result, remove this line when script is finished
# sleep 5
# rm LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-*.png
Your command:
convert - ...
tries to read an image from its standard input. So, it will hang forever unless you provide an image on standard input:
cat someImage.jpg | yourScript arg1 arg2
or name an image afterwards:
convert someImage.jpg ...
Maybe $logo_path already is the name of your image, in which case you would need:
convert "$logo_path" ...
By the way, #Jdamian's suggestion is a good one, in concrete terms, it means change your first line to:
#!/bin/bash -x
I solved the problem with the hanging mkdir by moving it to ~/.bash_profile and using a customized command for mkdir like so:
mk () {
case "$1" in /*) :;; *) set -- "./$1";; esac
mkdir -p "$1" #&& cd "$1"
}
# call the socmed function like so
# socmed day month "employer" "position" "path to logo"
function socmed {
args=("$#")
echo
echo date1 = "${args[0]}"
echo date2 = "${args[1]}"
echo employer = "${args[2]}"
echo position = "${args[3]}"
echo logo_path = "${args[4]}"
employer=${args[2]}
position=${args[3]}
logo_path=${args[4]}
folder_path=${logo_path/%\/*//}
custpath="$HOME/Dropbox/+ B-folder/A Folder/Gfx/Logos/"
output_dir="${employer} - ${position}"
mk "$custpath"/"$folder_path"/"$output_dir"
echo custpath = "$custpath"
echo folder_path = "$folder_path"
echo output_dir = "$output_dir"
echo
./full-tw.sh "${args[0]}" "${args[1]}" "${args[2]}" "${args[3]}" "${args[4]}"
./full-insta.sh "${args[0]}" "${args[1]}" "${args[2]}" "${args[3]}" "${args[4]}"
./full-ln.sh "${args[0]}" "${args[1]}" "${args[2]}" "${args[3]}" "${args[4]}"
./full-fb.sh "${args[0]}" "${args[1]}" "${args[2]}" "${args[3]}" "${args[4]}"
}
It doesn't matter that I have to shove the problem upwards one level, cause my intention was to use the bash profile anyway in order to have many images be created at the same time. =)
The script now looks like:
#!/bin/bash +x
# Version: ImageMagick 6.9.6-7 Q16 x86_64 2016-12-05, running on macOSX 10.11.6 El Capitan
# run the script in bash terminal like so:
# ./full-ln.sh 12 12 "Employer" "Position" "path to logo"
# remember to chmod the file to be able to run it: chmod +x full-ln.sh
# this script is partially debugged with http://www.shellcheck.net/
#
# this script was improved with the help of the nice ppl at StackOverflow
# http://stackoverflow.com/questions/41531443/mkdir-stuck-when-running-bash-script-for-imagemagick?noredirect=1#comment70275303_41531443
args=("$#")
# echo
# echo date1 = "${args[0]}"
# echo date2 = "${args[1]}"
# echo employer = "${args[2]}"
# echo position = "${args[3]}"
# echo logo_path = "${args[4]}"
# the new way for $custpath
custpath="$HOME/Dropbox/+ B-folder/A Folder/Gfx/Logos/"
# echo custpath = "$custpath"
# the old way below
# custpath=$HOME/Dropbox/+\ B-folder/A\ Folder/Gfx/Logos/
# echo custpath = "${custpath[#]}"
date1=${args[0]}
date2=${args[1]}
employer=${args[2]}
position=${args[3]}
logo_path=${args[4]}
# find the folder in the logo path
# see this article for how to replace a pattern from the end
# http://www.thegeekstuff.com/2010/07/bash-string-manipulation/
folder_path=${logo_path/%\/*//}
# echo folder_path = "$folder_path"
output_dir="${employer} - ${position}"
# echo output_dir = "$output_dir"
# echo
convert \( -draw 'circle 108.5,101.5 159.5,160' -stroke "rgb(241,142,0)" -fill "rgb(241,142,0)" -size 1022x798 canvas:white -bordercolor black -border 1x1 -fill black -stroke black -draw 'rectangle 1,721 1022,798' -fill white -stroke none -background none -gravity south -page +93-11.5 -font /Library/Fonts/RobotoCondensed-Light.ttf -pointsize 35.5 label:'A Folder Karriär' -flatten \) $HOME/Dropbox/+dev/coding_images/imagemagick/ah-karriar-neg.png -geometry 207x+386+6.5 -composite miff:canvas0
convert -size 125x150 -background none -gravity center -stroke none -fill white -interline-spacing -7 -pointsize 33 -font /Library/Fonts/Roboto-Bold.ttf label:"Sista\nansökan\n${date1}/${date2}" miff:- |
composite -gravity center -geometry -402-300 - canvas0 miff:- |
convert - -size 255x150 -background none -gravity west -stroke none -fill black -kerning 0.5 -font /Library/Fonts/RobotoCondensed-Regular.ttf label:"${employer} söker" -geometry +33-102 -composite miff:- |
convert - -size 486x320 -background none -gravity west -stroke none -fill black -kerning 0.25 -interline-spacing -5 -font /Library/Fonts/Roboto-Regular.ttf caption:"${position}" -geometry +32+87 -composite miff:- |
# failed at solving the making of a dir inside this script, moved that to bash_profile
# mkdir -pv -m u=rwx "$custpath"/"$folder_path"/"$output_dir"
# [ -d "$output_dir" ] && echo "Output Directory Exists" || mkdir -pv -m u=rwx "$output_dir"
convert - "$custpath"/"$logo_path" -gravity center -geometry 350x350+280+70 -composite -set filename:dimensions '%wx%h' "$custpath/$folder_path"/"$output_dir"/LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-'%[filename:dimensions]'.png
rm canvas0
echo "LinkedIn-image complete"
echo
# open "$custpath"/"$folder_path/$output_dir"/LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-*.png
# remove output file when testing the result, remove this line when script is finished
# sleep 5
# rm "$custpath"/"$folder_path/$output_dir"/LN-"${employer}"-"${position}"-"${date1}"_"${date2}"-*.png
Related
This command from ImageMagic creates an image writing the value of variable var in its center:
convert -size 512x512 xc:none -font "Free-Monospaced-Bold" -pointsize 180 -gravity center -draw 'text 0,0 '"$var"' ' "$var".png
While it works well for strings, it fails when the value of var is a number:
convert-im6.q16: non-conforming drawing primitive definition `text' # error/draw.c/DrawImage/3265.
Since variables in bash are untyped, there's no way to transform it into a string (command printf will leave it as it is, a number), so the point must be to change the text argument in the command.
How to generate the image when the variable is numeric (positive, negative and real numbers)?
While the documentation for -draw's text operator only says that the string to render needs to be quoted if it has spaces, it appears it also needs to be quoted if it's numeric. So...
-draw "text 0,0 '$var' "
You can also put a number in an image in Imagemagick using -annotate in place of -draw.
var=7
convert -size 512x512 xc:none -font "Free-Monospaced-Bold" -pointsize 180 -gravity center -annotate +0+0 "$var" "$var".png
Or
var=7
convert -size 512x512 xc:none -font "Free-Monospaced-Bold" -pointsize 180 -gravity center -annotate +0+0 $var $var.png
But safest to use double quotes
I'm trying to write quotes onto an image automatically. The script that I'm writing picks a random background and appends a quote on the top, and puts smaller on the bottom the quote from the person, with some padding.
The only issue, I'm not able to get the text to work properly. I'm trying to write centred text to a box on the top and then the caption of where it's from centred on text in the bottom, with some padding in between and on the sides. How can I achieve this properly?
Here is what I'm getting:
This is how I'd like to text to be arranged by filling these boxes centred: (note that I don't want the boxes drawn, just illustrating where the text ought to be)
My code:
#!/bin/bash
# Get the text
text="This is text that will occupy until a certain space. This will be long on here."
bottom="By Michael A. Leonetti"
echo $text
bgs="bgs"
image="${bgs}/$( ls "${bgs}" | sort -R | tail -$N | head -1 )"
echo "Using background image ${image}"
width=1024
height=768
usable_percent="0.95"
bottom_percent="0.1"
padding_percent="0.05"
usable_width=$( echo "${width}*${usable_percent}"|bc )
usable_height=$( echo "${height}*${usable_percent}"|bc )
bottom_height=$( echo "${bottom_percent}*${usable_height}"|bc )
padding_height=$( echo "${padding_percent}*${usable_height}"|bc )
text_width=$usable_width
text_height=$( echo "${usable_height}-${bottom_height}-${padding_height}"|bc )
convert \
\( \
-size "${text_width}x${text_height}" \
-background none \
-fill white \
-gravity north \
caption:"${text}" \
-size "${text_width}x${bottom_height}" \
-background none \
-fill white \
-gravity south \
caption:"${bottom}" \
\) \
-composite \
\( \
"${image}" \
-resize "${width}x${height}^" \
-gravity center \
-crop "${width}x${height}+0+0" \
-brightness-contrast -30x0 \
\) \
+swap \
-gravity south \
-composite \
+repage \
"${bottom}.png"
I have a directory (./img/) with a large number of images in it. I want to montage them with specific label for each file. I have a text file (label.txt) which has list of labels:
label1
label2
label3
label4
...
I use this command:
montage -label #label.txt -size 512x512 "./img/*.*[120x90]" -geometry +5+5 photo.png
But in result, all images are labeled by all lines of label.txt! How I can separate them, so each image will be labeled by relevant line of label.txt?
ImageMagick works by parsing the command line and executing the parameters left to right.
Let's say that I have the following images:
eh-banner-201606.png
eh-banner-201607.png
eh-banner-201608.png
eh-banner-201609.png
(these are some banners for a webstie, the quickest images I have at hand)
Now, if I want to construct a montage with distinct label I must do:
montage -font /usr/share/fonts/TTF/DejaVuSerif.ttf -size 512x512 \
-label 'first image' eh-banner-201606.png \
-label 'second image' eh-banner-201607.png \
-label 'third image' eh-banner-201608.png \
-label 'fourth image' eh-banner-201609.png \
-geometry +5+5 out.png
(the -font parameter is not strictly needed but I included it for pedantry)
This works as well with #labelfile:
montage -font /usr/share/fonts/TTF/DejaVuSerif.ttf -size 512x512 \
-label '#first.txt' eh-banner-201606.png \
-label '#second.txt' eh-banner-201607.png \
-label '#third.txt' eh-banner-201608.png \
-label '#fourth.txt' eh-banner-201609.png \
-geometry +5+5 out.png
Where each file will contain a single line (or several lines) that will be the caption for the next image on the command line.
Therefore you need a script to perform what you are after, for example:
#!/bin/bash
LABELS=${1:-./labels.txt}
IMAGES=""
readarray labels < "$LABELS"
i=0
for img in "$#"; do
IMAGES+=" -label '${labels[i]}' $img"
((i++))
done
echo montage -size 512x512 $IMAGES -geometry +5+5 out.png
montage -size 512x512 $IMAGES -geometry +5+5 out.png
You the can do (assuming the script name to be script.sh):
./script.sh label.txt ./img/*.*120x90
Caveat: The image files cannot have spaces or special shell characters (that would require a third level of quoting, which is incredibly error prone). Note that [ and ] are special shell characters depending on the context. If you need more complex handling it may be wiser to use Perl or Python.
I changed sample code represented by #grochmal and it works:
#!/bin/bash
LABELS=${1:-./labels.txt}
IMAGES=""
readarray labels < "$LABELS"
i=0
echo # is $#
#copy input argument array in InArg array:
for img in "$#"; do
InArg[$i]=$img
((i++))
done
#get dimensions of InArg array:
arraylength=${#InArg[#]}
#extract Input Image array from InArg array:
for (( i=1; i<${arraylength}; i++ ));
do
ImageArray[$i]=${InArg[$i]}
done
#create IMAGES from some elements of InArg array:
for (( i=1; i<${arraylength}; i++ ));
do
IMAGES+=" -label ${labels[i-1]} ${ImageArray[$i]}"
done
echo "IMAGES is $IMAGES"
echo montage -size 512x512 $IMAGES -font FreeMono-Bold-Oblique -pointsize 30 -geometry '120x90+5+5>' -tile 5x -frame 5 -shadow out.html
montage -size 512x512 $IMAGES -font FreeMono-Bold-Oblique -pointsize 30 -geometry '120x90+5+5>' -tile 5x -frame 5 -shadow out.html
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? 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. 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.
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 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
convert rose: -fill white \
-gravity NorthWest -annotate +0+0 "A" \
A.png
convert rose: -fill white \
-gravity SouthEast -annotate +0+0 "B" \
B.png
Based on the answers above, I wrote a small perl script to combine figures into labelled subfigures, which might be useful to others. It works for me on a Mac, but I didn't really test it.
Usage: pics2grid geometry output_file [-title title] [-bycols] input_files
where the geometry format is [# cols]x[# rows] and input_files accept wild cards. 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
#!/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
I could not find the exact reference to what I'm doing...
I have the following script that does not expand the variable inside the command:
#!/bin/bash
name="my name"
`convert -pointsize 250 -font /usr/share/fonts/truetype/msttcorefonts/impact.ttf -fill black -draw 'text 330,900 "$name"' tag.jpg name_my.jpg`
This results in an image that has the text $name instead of the content of name.
I actually need to read lines from a file and rund the command on each name so my real script is(has the same problem):
arr=(`cat names.txt`)
for (( i=0; i<${len}; i+=2 ));
do
`convert -pointsize 250 -font /usr/share/fonts/truetype/msttcorefonts/impact.ttf -fill black -draw 'text 330,900 "$(${arr[i]} ${arr[i+1]})"' tag.jpg name_${arr[i]}.jpg`
done
Your problem is single quotes ('') not backticks. Because $name is within them, it won't be expanded. Instead, you should use double quotes and you can escape the inner quotes like this:
`convert -pointsize 250 -font /usr/share/fonts/truetype/msttcorefonts/impact.ttf -fill black -draw "text 330,900 \"$name\"" tag.jpg name_my.jpg`
you have an escaping problem.
either use proper escaping with backslash, or make sure otherewise that the $args are not "protected" by single quotes.
e.g.
name="bla"
# using escape character \
value1="foo \"${name}\""
# putting single-quotes inside double-quotes
value2="foo '"${name}"'"
to better see what is going on, try to break down the problem into multiple smaller problems.
e.g. create the "draw" command with all expansions before using it in convert
name="my name"
draw="text 330, 900 '"${name}"'"
convert -pointsize 250 -fill black -draw "${draw}" tag.jpg name_my.jpg