How can I search for photos by height or width?
you can use simple shell script for this.
create any file search.sh
in your photos folder using
gedit search.sh
and paste bellow lines:
#!/bin/bash
mkdir /home/$USER/test
for i in *.jpg; do
read -r w h <<<$(identify -format "%w %h" "$i")
if [ `expr $h / $w` == `expr 4 / 3` ]; then
cp "$i" /home/$USER/test/
fi
done
save this file using Ctrl+s and make it executable using
sudo chmod +x search.sh
then execute it using command
./search.sh
it will copy all photos that have 4:3
to test
directory in your home directory.
if you want copy to flash drive or somewhere else then make change in script and if identify
command is not available in your system then you can install is using command:
sudo apt-get install imagemagick
Extracting images from a recursive directory
The script below will copy images of a 4:3 ratio, from a source directory recursively into a target directory. The script reads all common image formats, non- image files are ignored (raising in IOError
)
#!/usr/bin/env python3
import os
import shutil
from PIL import Image
import sys
dr = sys.argv[1]; target = sys.argv[2]
for root, dirs, files in os.walk(dr):
for f in files:
try:
file = root+"/"+f
img = Image.open(file); size = img.size; ratio = size[0]/size[1]
if ratio == 4/3:
shutil.copyfile(file, target+"/"+f)
except (IOError, ValueError):
pass
How to use
- Copy the script into an empty file, save it as
search_ratio.py
-
Run it by the command:
python3 /path/to/search_ratio.py /source/directory /target/directory
If one or more directories include spaces, use quotes.
An example command I used for testing:
python3 '/home/jacob/Bureaublad/pscript_4.py' '/home/jacob/Bureaublad' '/home/jacob/Afbeeldingen/test'
In case you are extracting images from a "flat" (single) directory
Then the script can be a little simpler:
#!/usr/bin/env python3
import os
import shutil
from PIL import Image
import sys
dr = sys.argv[1]; target = sys.argv[2]
for f in os.listdir(dr):
try:
file = dr+"/"+f
img = Image.open(file); size = img.size; ratio = size[0]/size[1]
if ratio == 4/3:
shutil.copyfile(file, target+"/"+f)
except (IOError, ValueError):
pass
Use it exactly like the first one.
Note that with the first script, you will get name clashes in case of duplicated names in different source folders.