📜 ⬆️ ⬇️

Photo processing script: unix-way among us

Good evening, habrovchane!

To begin with, I work in an uneasy school as a simple laboratory assistant. All sorts of responsibilities hang on me, one of which is to replenish the school website and resource oshkole.ru with content.
Often you have to upload photos to this resource. But since it is written about-very crooked (by the way, the maintenance of the school’s pages there is mandatory and controlled by the prosecutor’s office), the photos are uploaded to the end user in full size. So you have to resize ~ 30 pieces per week.

Since I am a lover of unix-way, I quickly got the idea to write a bash script.
')
The goal was set: to easily and easily resize all images in the directory. The task is rather trivial, the script was thrown in 5 minutes, and ... It dawned on me: I wanted to write an article for Habr for so long, it was just nothing. And here I am =)

Anyone who has not yet become bored, welcome under the cat.


So let's get started.



The resizing itself will be done using the convert utility from the ImageMagick set. I’ll just say that this utility is very powerful (and ImageMagick itself is even more powerful) and you can do a lot of things with it .
> convert image_in.jpg -resize 1228800@ image_out.jpg
Here I use the option -resize @ 1228800. It means that the image will have an area of ​​1228800 pixels (corresponds to a resolution of 1280x960). It is more reasonable to use this option here because the photos can be both vertical orientation and horizontal. More information about the options for resizing images can be found here .

At this stage, the idea arose to do it not only for myself =).

So, first you need to check if you have a convert on the machine. Let's do it like this:
convert > /dev/null
if [ $? -ne 0 ]; then echo "Error: convert is needed, it's a part of ImageMagick"; fi ;

I will clarify the entry “if [$? -ne 0] ":
The bracket "[" corresponds to the test command, and you can read about it in man test (1). In $? stores the result of the previous command. For example, if the execution was successful, there will be 0, and if the command was not found - 127. The closing bracket "]" performs only an aesthetic function.

Further. There is a small problem - sometimes, the user indicates at the end of the address "/", for example: / home / user /. I didn’t find how to remove this slash by the human method, so I came up with a children's tricycle:
if [ -z $1 ]; then $DIR=`pwd`;
else
TEMP=`pwd`;
cd $DIR; TEMP2=`pwd`;
cd $TEMP;
DIR=$TEMP2;
echo $TEMP2;
fi ;

At the same time here we decide to use the current directory if the argument is not given.

Copy old files ...:
mkdir "$DIR.old";
for i in `ls $DIR`;
do
cp $DIR/$i $DIR.old;
done

For those who do not really understand "for i in ..." I advise you to look here .

And most importantly:
for i in `ls $DIR`;
do
convert $DIR/$i -resize 1228800@ $DIR/$i;
if [ $? -eq 0 ]; then
echo "$i successfully resized";
else k=$[$k+1]; #
fi ;
done

I think at this stage everything should be clear.

So, the long-awaited moment: we put everything together. The file can be downloaded here .
#!/bin/sh <br><br>
#, convert <br>
convert > /dev/null<br>
if [ $? -ne 0 ] ; then <br>
echo "Error: convert is needed, it's a part of ImageMagick" ;<br>
fi ;<br>
DIR=$1;<br>
# , "/" <br>
if [ -z $1 ]; then $DIR=`pwd`; <br>
else <br>
TEMP=`pwd`;<br>
cd $DIR; TEMP2=`pwd`; <br>
cd $TEMP;<br>
DIR=$TEMP2;<br>
echo $TEMP2;<br>
fi ; <br>
# DIR.old <br>
mkdir "$DIR.old" ;<br>
for i in `ls $DIR`; <br>
do <br>
cp $DIR/$i $DIR.old;<br>
done ;<br>
ERR=0;<br>
echo "Start resizing to 1280x960...." <br>
for i in `ls $DIR`;<br>
do <br>
convert $DIR/$i -resize 1228800@ $DIR/$i;<br>
if [ $? -eq 0 ]; then <br>
echo "$i successfully resized" ; <br>
else ERR=$[$ERR+1]; # <br>
fi ;<br>
done ;<br>
if [ $ERR -eq 0 ]; then <br>
echo "Job done!" ;<br>
else echo "Job done with some errors." ;<br>
fi ;<br>
echo "You can find your old files in $DIR.old" <br>
#end




How to run a script


Suppose a script with the name “image” is in your / data / scripts folder, and photos to be processed are in / data / foto.
Then we execute the command
> /data/scripts/image /data/foto
And, hooray!
Just do not forget to give the script execution rights:
> chmod +x /data/scripts/image

findings


Everything, as we see, is very easy and simple. There is nothing bad in the unix way. Why, there is a lot of good! I use this script (now) every day, plus a few more scripts for working with the network. And I will give you a good women's advice: automate! =)

I am writing to Habr for the first time, do not hit hard

Source: https://habr.com/ru/post/82394/


All Articles