resize images by using shell script - bash

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

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.

Convert entire folder to greyscale using image magick?

I am trying to convert an entire folder to grayscale, using image magick.
convert *.jpg -colorspace Gray -separate -average
is met with this error :
convert: `-average' # error/convert.c/ConvertImageCommand/3290.
What is the correct command for this?
If you have lots of files to process, use mogrify:
magick mogrify -colorspace gray *.jpg
If you have tens of thousands of images and a multi-core CPU, you can get them all done in parallel with GNU Parallel:
parallel -X magick mogrify -colorspace gray ::: *.jpg
Also, the following can be used in a script - for the context menu of file managers like Dolphin, Nautilus, Nemo, Thunar etc:
for filename in "${#}"; do
name="${filename%.*}"
ext="${filename##*.}"
cp "$filename" "$name"-grayscale."$ext"
mogrify -colorspace gray "$name"-grayscale."$ext"
rm "$name"-grayscale."$ext"~
done

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

Batch resize images and output images to new folder with ImageMagick

Current image folder path:
public_html/images/thumbs
Output image folder path:
public_html/images/new-thumbs
I have 10 video thumbs per video in current folder, named of image thumbs:
1-1.jpg
1-2.jpg
1-3.jpg
1-4.jpg
1-5.jpg (Resize)
1-6.jpg
1-7.jpg
1-8.jpg
1-9.jpg
1-10.jpg
2-1.jpg
2-2.jpg
2-3.jpg
2-4.jpg
2-5.jpg (Resize)
2-6.jpg
2-7.jpg
2-8.jpg
2-9.jpg
2-10.jpg
I want to resize all 5th images(*-5.jpg) to the new folder. I've tried below command but no luck:
mogrify
-path
public_html/images/thumbs/*-5.jpg
-resize 16×12
-quality 100
public_html/images/new-thumbs/*-5.jpg
"Mogrify" should be called from the directory with the original thumbnails, while the -path parameter is for pointing target directory.
mkdir public_html/images/new-thumbs
cd public_html/images/thumbs
magick mogrify -resize 16x12 -quality 100 -path ../new-thumbs *.jpg
http://www.imagemagick.org/Usage/basics/#mogrify
The last arguments are the list of files, so you can filter by name 1-*.jpg for example.
Suggested solutions do not work properly on the latest ImageMagick (at least, on macOS).
Command, that works overwriting source images is as follows:
magick mogrify -path ./ -resize 50% -quality 80 *.jpg
To avoid overwriting the original images, write to a new folder:
magick mogrify -path path/to/destination/folder/ -resize 50% -quality 80 *.jpg
In ImageMagick 7 versions its built into the magick ...so..
magick mogrify -resize 16x12 -quality 100 -path ../new-thumbs *.jpg
Make sure that the folder you specify in path exists. It will not be created by ImageMagick.
Find more information here https://www.imagemagick.org/script/mogrify.php
For those having Shotwell installed on Ubuntu/Debian, following may be more easy to export selected images in a folder to another folder through processing the images as needed.
Open Shotwell
Select the images you want to export
File > Export
Adjust the values to your needs
Select the folder to export

Resources