0

I use the following script to convert all jpg and png images:

# absolute path to image folder
FOLDER="/home/*/public_html/"

# max width
WIDTH=1280

# max height
HEIGHT=720

#resize png or jpg to either height or width, keeps proportions using imagemagick
find ${FOLDER}  -type f \( -iname \*.jpg -o -iname \*.png \)    -exec convert \{} -verbose -resize $WIDTHx$HEIGHT\> \{} \;

but I was shocked today when I run

ls -l 

and found all photos modified, data changed, big or not

 Oct 28 11:18 /home/photos/20210321/T161631305496ece25372fc18a9239da7911ac7c0dd056 (2).jpg

So I'm thinking about using an if condition to check the path of the image first, then if WIDTH is greater than 1280px run convert. Else don't do anything.


update 2

I built this script

#!/bin/bash
for  i in /root/d/*.jpg; do
  read -r w h <<< $(identify -format "%w %h" "$i")
  if [ $w -gt  1280 ]; then
    FOLDER="$i"
    WIDTH=1280
    HEIGHT=720
    find ${FOLDER}  -type f \( -iname \*.jpg -o -iname \*.png \)    -exec convert \{} -verbose -resize $WIDTHx$HEIGHT\> \{} \;
  fi
done

So I see find better for .

for didn't search all folder and subfolders.


update 3

WIDTH=1280
HEIGHT=720
find /home/sen/tes/  -type f \( -iname \*.jpg -o -iname \*.png \)   | while read img; do \
  anytopnm "$img" | pamfile | \
   perl -ane 'exit 1 if $F[3]>1280' || convert  "$img"    -verbose -resize "${WIDTH}x${HEIGHT}>"   "$img"; \
done

it's working good , but i get

jpegtopnm: WRITING PPM FILE

when no images > 1280

6
  • Welcome to the site. Please edit your post to add an actual question.
    – AdminBee
    Commented Oct 28, 2021 at 15:00
  • It's unclear if your question is about your original script (in which case the second half is irrelevant) or about cleaning up after your mistake (in which case the first half is irrelevant).
    – Kusalananda
    Commented Oct 28, 2021 at 15:21
  • for i in /root/d/*.jpg; do - this construction will works incorrectly with files which could contains spaces. I think you need to write function or separate script and use find with exec myscript {} \;
    – Saboteur
    Commented Oct 28, 2021 at 16:03
  • @AdminBee thanks am confuse about title Commented Oct 28, 2021 at 16:26
  • @Saboteur i add update 3 , please check Commented Oct 28, 2021 at 16:27

2 Answers 2

1

The main issues with your convert command is that the argument $WIDTHx$HEIGHT\> tries to expand a variable called $WIDTHx. As this variable does not exist, the argument used with -resize will be just whatever $HEIGHT\> is (which is the same as using "${HEIGHT}x$HEIGHT>"). You fix this by using -resize "${WIDTH}x$HEIGHT>". This is an issue in both your find commands.

To shrink your too large images, you could possibly use something like

#!/bin/sh

w=1280
h=720

find /home/*/public_html -type f \( -iname '*.jpg' -o -name '*.png' \) \
    -exec convert -resize "${w}x${h}>" {} \;

Personally, I would just restore the images from the latest backup, as scaling images up and down like this is bound to reduce their quality drastically.

When testing things out, run on smaller sets of copies of your images before letting your scripts loose on your whole image collection. Also make sure that your backups are running as expected.

7
  • thanks but i need convert run only after check path of img and it's greater than 1280px Commented Oct 28, 2021 at 15:10
  • @MikelTawfik Could you possibly test my find command as it currently is, on a copy of a subset of images, to see if it does the right thing. I'm sure it would be easy to adjust it if it seems to do the wrong thing.
    – Kusalananda
    Commented Oct 28, 2021 at 15:20
  • convert: no images defined /home/sen/tes/c73f3dfe4fbb7ccf149797e1644255b1.jpg' @ error/convert.c/ConvertImageCommand/3235. convert: no images defined /home/sen/tes/wallpapersden.com_lily-aldridge-sexy-little-bride-wallpaper_3840x2160.jpg' @ error/convert.c/ConvertImageCommand/3235. Commented Oct 28, 2021 at 15:38
  • i fix code by convert {} -verbose -resize "${w}x${h}>" {} \; but. same problem , files modified date updated every time i run Commented Oct 28, 2021 at 15:41
  • @MikelTawfik You never said that updating timestamps were an issue.
    – Kusalananda
    Commented Oct 28, 2021 at 16:02
-1

am so sorry i was not clear , i found and build my final script and hope to correct title of Q with clever one

script search for files jpg or png and if found check Width if found greater than 1280 will convert

#!/bin/bash
find /home/sen/tes/  -type f \( -iname \*.jpg -o -iname \*.png \)   | while read i; do \
read -r w h <<<$(identify -format "%w %h" "$i")
if [ $w  ]; then
if [ $w -gt  1280 ]; then
FOLDER="$i"
WIDTH=1280
HEIGHT=720
find ${FOLDER}  -type f \( -iname \*.jpg -o -iname \*.png \)    -exec convert \{} -verbose -resize ${WIDTH}x${HEIGHT}\> \{} \;
fi
fi
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .