Page 1 of 1
Conditional Removal
Posted: 2014-02-20T06:30:12-07:00
by salahuddinonline
Hi all,
hope all of you are doing fine, I have a question so I hope this forum can me out of this,
I am using ImageMagick on "ubuntu" and I want to do some conditional removing of images
I have a directory (Named Original ) in which I have more then 100 images, of different sizes such as 700x700, 380x380, 225x225 etc ...
I have another directory Named Processed in which I have same name and same number of images as I have in Directory Original but all images have same size, as 500x500
I want to remove images from Directory Processed if image in the Directory Original is less then 400x400 then remove same named image from Directory Processed.
there is not any other condition applied ..
so is there any one who can help out on this...
Re: Conditional Removal
Posted: 2014-02-20T07:43:32-07:00
by mrmattnc
What you have is the makings of a good bash script. Imagemagick will provide some of the nuts and bolts, but really it sounds like your problem is just getting it to run.
Here is an untested framework that should provide you with the commands you need. These should be put in a file with the .sh extension and executed from the command line. Hopefully there aren't too many bugs, but I haven't had my morning coffee yet so anything is possible.
Code: Select all
#/bin/bash
#
# The script looks for images in original_dir that are less than 400x400, and if found removes them from original_dir and processed_dir
#directories we're working on [full path]
processed_dir=/home/user/Desktop/processed
original_dir=/home/user/Desktop/original
loglocation=/home/user/Desktop/log
#parameters
minheight=400
minwidth=400
#collect a list of the files to check
# if your files are not .jpg, please change the extension
# if there are nothing but photos in there remove name directive entirely
cd $original_dir
find -name "*.jpg" | sed "s/^\.\///" > ~/originalfiles
echo
while read line; do
#make sure we can actually find the file (spaces and windows only characters can break this)
if [ -f "$original_dir/$line" ]
then
#get their dimensions
height=$(identify -format "%h" "$original_dir/$line")
width=$(identify -format "%w" "$original_dir/$line")
if [ $height -lt $minheight -o $width -lt $minwidth ]
then
#if it exists in processed_dir, remove it
if [ -f "$processed_dir/$line" ]
then
rm "$processed_dir/$line"
rm "$original_dir/$line"
echo "$line removed, dimensions $width $height" >> "$loglocation"
else
echo "$line removal failed (no file found in processed_dir), dimensions $width $height" >> "$loglocation"
fi
#echo progress indicator
echo -n "+"
else
#echo progress indicator
echo -n "-"
fi
else
echo
echo "ERROR, COULD NOT FIND FILE $line in original_dir"
fi
done < ~/originalfiles
rm ~/originalfiles
Re: Conditional Removal
Posted: 2014-02-20T22:32:34-07:00
by salahuddinonline
Thanks for your response Matt
your script is working fine but its removing images from both directories
actually what I am looking for is
directory original has images 1.jpg (200x200), 2.jpg (1000x1000), 3.jpg(350x350) 4.jpg(400x400))
same images are in directory processed but with same size as 1.jpg (300x300), 2.jpg (300x300), 3.jpg(300x300) 4.jpg(300x300)
so if any image in directory original is less then 400x400 its should be removed from directory processed but without removing images from directory original
I dont wanna create a log for this as well
Re: Conditional Removal
Posted: 2014-02-20T22:46:09-07:00
by mrmattnc
The "rm" commands are 'remove'.
So what you want to do is comment out (by adding a # to the front of the line) or deleting the line so the command doesn't exist anymore. There are multiple lines where these occur.
Code: Select all
rm "$processed_dir/$line"
rm "$original_dir/$line"
becomes
Code: Select all
rm "$processed_dir/$line"
#rm "$original_dir/$line"
or just delete the whole line. You should be able to see it's color change if you're editing this in Notepad++ (windows), gedit (linux/ubuntu), or even VIM if you're a masochist.
To handle not needing a log you've got basically the same options as well. You can either comment out the redirect to file ( the >> "$loglocation" ), delete the redirect to file, delete the whole line, OR you can change the log location parameter to /dev/null
/dev/null is basically a linux blackhole. Whatever you send there is never recorded to file or the command line. I use this exact trick when I'm debugging more complicated scripts. That way you can turn logging back on if you need it later. Whatever your preference is...
Glad I could help!
Re: Conditional Removal
Posted: 2014-02-20T23:50:02-07:00
by salahuddinonline
I want to use this script on multiple and different directories, so I need to change directories as well
as original and processed
then
leftlarge and LeftCroped
then
RightMax and Right_Trimmed
dont want to create log
this is not working as I want it to work on different directories
please Simplify this script so I could use it
Re: Conditional Removal
Posted: 2014-02-25T12:13:46-07:00
by mrmattnc
I'll put this here, in addition to the PM sent earlier.
You need to supply the full path in the parameters at the top of the script. I intentionally wrote it this way so it would be portable for you and any other users needing a similar tool. It can also be easily expanded for other functions - it's a framework of basic file checks and processing instead of a barebones script only applicable to your use case.
Put the path to the log file as /dev/null to not save it.
If you need additional help I'm lurking around the Consultation board, or you can consult the bash basics at
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html for building upon what has been posted.