0

I have a folder of images. I want to iterate through the folder, apply the same ImageMagick convert function to each file, and save the output to a separate folder.

The way I'm doing it currently is this:

#!/bin/bash

mkdir "Folder2"
for f in Folder1/*.png
do
    echo "convert -brightness-contrast 10x60" "Folder1/$f" "Folder2/"${f%.*}"_suffix.png"
done

Then I copy and paste that terminal output into a new bash script that ends up looking like this:

#!/bin/bash

convert -brightness-contrast 10x60 Folder1/file1.png Folder2/file1_suffix.png
convert -brightness-contrast 10x60 Folder1/file2.png Folder2/file2_suffix.png
convert -brightness-contrast 10x60 Folder1/file3.png Folder2/file3_suffix.png

I tried to write a single bash script for this task but there was some weirdness with the variable handling, and this two-script method got me what I needed ...but I suspect there's an easier/simpler way and possibly even a one-line solution.

dd23
  • 17
  • 6
  • For any future people looking at this, I found this blog post very helpful for doing basic things: http://www.brianlinkletter.com/process-images-for-your-blog-with-imagemagick/ And this repository of ImageMagick scripts is excellent: http://www.fmwconcepts.com/imagemagick/index.php – dd23 Feb 27 '18 at 03:00

3 Answers3

0

Check this answer (and the few in the question) out:

You can use it in your example :

find Folder1 -name "*.png" | sed -e 'p;s/Folder1/Folder2/g' -e 's/.png/_suffix.png' | xargs -n2 convert -brightness-contrast 10x60

note : the p in the first sed makes the trick.

find will list you all the files in Folder1 with a name that matches the *.png expression

sed -e 'p;/Folder1/Folder2/g' will (a) print the input line and (b) replace Folder1 by Folder2

-e 's/.png$/_suffix.png' replaces the .png suffix with the _suffix.png suffix

xargs -n2 tells the shell that xargs should take two arguments max (the first one being printed by sed 'p' and the second one going through all the -e)

convert ... is your command, taking two inputs.

fzd
  • 765
  • 1
  • 6
  • 19
  • thank you so much for this one liner and detailed explanation of how it works! quick followup: what is the purpose of the double quote at the very end of the line: `10x60"` ? – dd23 Feb 24 '18 at 07:56
  • It's a typo :) It shouldn't be there :) – fzd Mar 01 '18 at 14:36
0

It's enough to change your first script, not to just echo the commands, but to execute them.

#!/bin/bash

mkdir "Folder2"
for f in Folder1/*.png
do
    convert -brightness-contrast 10x60 "Folder1/$f" "Folder2/${f%.*}_suffix.png"
done
dd23
  • 17
  • 6
Arusekk
  • 827
  • 4
  • 22
  • oh geez... the "some weirdness" is because of the extra quotes in this bit: `"Folder2/"${f%.*}"_suffix.png"` I originally was trying to make some thing like this work, but it wasn't displaying properly. For newbies to bash scripts, this is probably the best option. – dd23 Feb 24 '18 at 07:55
0

Crystal ball is telling me that there are spaces in filenames causing "some weirdness with the variable handling". In that case you need a workaround for the spaces. For example, you may try the following script:

#!/bin/bash

hasspaces="^(.+[^\'\"])([ ])(.+)$"

function escapespaces { 
  declare -n name=$1
  while [[ $name =~ $hasspaces ]] ; do
    name=${BASH_REMATCH[1]}'\'${BASH_REMATCH[2]}${BASH_REMATCH[3]}
  echo 'Escaped string: '\'$name\'
  done
}

mkdir Folder2
while read -r entry; do
  echo "File '$entry'"
  escapespaces entry
  echo "File '$entry'"
  tmp=${entry#Folder1}
  eval "convert -brightness-contrast 10x60" "$entry" "Folder2/"${tmp%.*}"_suffix.png"
done <<<"$(eval "ls -1 Folder1/*.png")" 

If this does not work, by all means let me know so I can request a refund for my crystal ball! Also, if you can give more details on the "weirdness in variable handling", we could try to help with those other weirdnesses :-)

  • not the problem on this one, but almost definitely something I'd encounter in the future - thanks for preemptively answering a future question – dd23 Feb 24 '18 at 08:08
  • Ok, the crystal ball is going back. Anyway, if I were you, I would do my best to resolve the weird variable handling. – Strahinja Lukić Feb 24 '18 at 08:20