Make a "for i in $(...) in bash" command on linux - bash

I'm trying to execute a command line on each .jpg file in folder:
for i in ls *.jpg; do convert $i -resize 400x511 -gravity center -background white -extent 400x511 $i; done
But only the first .jpg is "done", what is wrong ?

First of all you don't need ls here and 2nd you need to quote it.
for i in *.jpg; do
convert "$i" -resize 400x511 -gravity center -background white -extent 400x511 "$i"
done

Related

How do I rename images when using mogrify with Image Magick?

I am trying to automate the converting images to specified tif formats. I have it converting just fine, but am also needing to add "_GS" at the end of the file name and before the extension to each file converted. Below is what I have but have had no luck finding a solution to add "_GS" to the file name. Thanks in advance for any help.
for f in "$#"
do
echo "$f"
/usr/local/bin/mogrify -density 300 -resize 1000x1000 -type grayscale -define tiff:endian=msb -compress LZW -format tif "$f" [0]
done
Item 1: With Image Magick, to create a new file instead of overwriting an existing one, use convert, not mogrify (magick convert with newer versions of IM).
Item 2: You can use shell parameter expansion to remove the extension, and then build the new filename from that and the new suffix:
for f in "$#"
do
echo "$f"
/usr/local/bin/convert "$f" -density 300 -resize 1000x1000 -type grayscale \
-define tiff:endian=msb -compress LZW -format tif "${f%.*}_GS.tiff"
done
${variable%pattern} returns the expansion of variable with the shortest match of pattern removed from the end.

How to write a loop in shell for graphicsmagick?

I managed (with the help of SO) to make perfect png-snippets from a pdf file with graphicsmagick. My pdf contains text and formula each "snippet" on a single page. My command trims the content of a page to the very content and finally scales this up to 2000 pixel width.
Untill now, I need to repeat that command for each single page in every pdf. I am wondering how to automate this. I think I could try a loop for the repetition of the command for every page i untill the last page.
Assume file1.pdf is in my current working directory.
gm convert -density 300x300 file1.pdf[0] -trim -resize 2000x file1_page1.png
gm convert -density 300x300 file1.pdf[1] -trim -resize 2000x file1_page2.png
gm convert -density 300x300 file1.pdf[2] -trim -resize 2000x file1_page3.png
...
How can I set a counter and run a loop for every page in my document?
You are in luck. GraphicsMagick knows how to do that for you:
gm convert -density 300x300 input.pdf -trim -resize 2000x +adjoin output-%d.png
If you are ok using ImageMagick instead, you can set the starting output file number to 1 instead of 0 and don't need the -adjoin:
convert -density 300x300 input.pdf -scene 1 -trim -resize 2000x output-%d.png
Or, if you want them all done in parallel, use GNU Parallel:
parallel gm convert -density 300x300 {} -trim -resize 2000x output-{#}.png ::: $(identify input.pdf | awk '{print $1}')
for file in *.pdf
do
pages=$(identify "$file" | wc -l)
for (( i=0; i<$pages; i++ ))
do
name=$(sed "s/\.pdf$/$i.png/g" <<< "$file");
gm convert -density 300x300 "$file[$i]" -trim -resize 2000x "$name"
done
done
Try this one.
It will convert every page in every *.pdf file to .png.

How to crop image from center without resizing with imagemagick

for parentDir in *
do
cd "$parentDir"
for subDir in *
do
cd "$subDir"
for file in *.*
do
convert "$file" -crop 120x95 summary_"$file"
convert "$file" -crop 160x225 detail_"$file"
done
mkdir detail
mkdir summary
mv summary_* summary/
mv detail_* detail/
cd ..
done
cd ..
done
Here is my script, I need a way to crop the image without resizing, get rid of the extra surrounding.
For example: 1200* 1500 image ----> 120px * 90px from the center
If you are just trying to crop each image to one center part then use
convert input.suffix -gravity center -crop WxH+0+0 +repage output.suffix
Otherwise, you will get many WxH crops for each image.
Thanks to #fmw42 I've made this script to use with my file manager Dolphin, which can be adapted for others as well:
#!/usr/bin/env bash
# DEPENDS: imagemagick (inc. convert)
OLDIFS=$IFS
IFS="
"
# Get dimensions
WH="$(kdialog --title "Image Dimensions" --inputbox "Enter image width and height - e.g. 300x400:")"
# If no name was provided
if [ -z $WH ]
then
exit 1
fi
for filename in "${#}"
do
name=${filename%.*}
ext=${filename##*.}
convert "$filename" -gravity center -crop $WH+0+0 +repage "${name}"_cropped."${ext}"
done
IFS=$OLDIFS
Enother imagemagick based solution.
Here is a scrip version with mogrify to bulk images manipulation instead of convert which works on individual images:
for parentDir in *
do
cd "$parentDir"
for subDir in *
do
cd "$subDir"
mkdir detail
cp * detail/
mogrify -gravity center -crop 160x225+0+0 +repage detail/*
mkdir summary
cp * summary/
mogrify -gravity center -crop 120x95+0+0 +repage summary/*
done
cd ..
done

copy one picture and paste it on top of the other on terminal

In here I found a piece of code that finds and resizes all images within a folder with applescript
Now I need to apply a stamp to those images one by one. I already have a transparent jpg prepared with the same size and ready to be pasted on all of them.
I just lack the knowledge to do it on terminal so I could mix it with applescript.
Does anyone know how to do this without making a workflow? Because it takes a lot of time and keeps the laptop busy
Updated Again :-)
You can get the height of an image like this:
identify -format "%h" image.jpg
500
and the width like this:
identify -format "%w" image.jpg
800
If you want to find the lesser of the height and the width, and then take 10% of that for your logo size and make it integer, you would do:
identify -format "%[fx:int(0.1*min(h,w))]" image.jpg
50
If you want to put that in a variable, you would do
logosize=$(identify -format "%[fx:int(0.1*min(h,w))]" image.jpg)
echo $logosize
50
You could then use that in the original command below like this:
convert image.jpg -gravity southeast \
\( copyright.png -resize "${logosize}x${logosize} \) \
-composite result.jpg
I suspect you are going to want to run that with the find command, aren't you? :-)
find . -iname \*.jpg -exec bash -c 'echo Processing {};logosize=$(identify -format "%[fx:int(0.1*min(h,w))]" "{}"); echo $logosize;convert "{}" -gravity southeast \( copyright.png -resize "$logosizex$logosize" \) -composite "{}"' \;
Updated Answer
If you want to resize your original image before compositing the logo unchanged on top, use a command like this:
convert image.jpg -resize 2000x1000 -gravity southeast copyright.png -composite result.jpg
If you want to resize the logo before compositing it on top of the unchanged original image, use a command like this:
convert image.jpg -gravity southeast \( copyright.png -resize 700x200! \) -composite result.jpg
Original Answer
I would use ImageMagick. It is extremely powerful and simple to install if you use homebrew. Ask if you don't know how...
Let's assume you have an image called image.jpg and a transparent logo/copyright called copyright.png.
image.jpg
copyright.png
Then the command to do a single image would be:
convert image.jpg copyright.png -composite result.jpg
If your logo/copyright and image are different sizes, and you want to force the logo to, say, the bottom right corner, just set the -gravity accordingly like this:
convert image.jpg -gravity southeast copyright.png -composite result.jpg
If you wanted to do all the JPEGs in the folder, you would need a loop:
#!/bin/bash
shopt -s nullglob
shopt -s nocaseglob
for f in *.jpg; do
convert "$f" copyright.png -composite "$f"
done
By the way, you cannot have a transparent JPEG - that format doesn't support transparency.
If you are really intent on using Applescript - I don't know why anyone would be - you can call the above using do shell script - make the file executable first. Ask if unsure how.
If you want to recurse down into all directories (folders) starting at the one you are currently located in and add the logo/copyright to all images, I would make a copy of them somewhere else first and do a trial run. The command would be:
find . -iname "*.jpg" -exec convert "{}" copyright.png -composite "{}" \;
That says... "find, starting at the current directory (.), disregarding upper/lower case (-iname), all files ending in "JPG", and apply the logo/copyright to them, resaving on top of the original file".
I had same issue years ago, and the only way I found is an Applescript using Photoshop (duplicate layer containing the logo on the photo and flatten file).
the script loops to all file of a folder.
... but you need Photoshop for this script !
I can share that script if it helps

resize images by using shell script

I want to resize multiple .jpg and .png images by using bash shell script.
The following script works fine, but I don't want to write same things twice.
for image in *.jpg; do
mogrify -resize x1000 "${image}"
done
for image in *.png; do
mogrify -resize x1000 "${image}"
done
How can I filter jpg and png images at once?
shopt -s nullglob
for image in *.jpg *.png; do
mogrify -resize x1000 "${image}"
done

Resources