Google images allows you to search the web for images instead of web pages. You can specify keywords but also image size, file extension, dominant color, …
Sometimes you just need a set of image to test your latest program. Instead of opening every web page and right-click/Save as image you might want to use the following script.
- Copy/paste the script at the end of the post in a file.
- Make it executable:
$ chmod +x googleimage.sh - Give it a try by searching red bananas image with a .jpg extension:
$ cd /tmp
$ /path-of-the-script/google-image.sh -s bananas -c red -e jpg
- If you want more images use the “page” option:
$ /path-of-the-script/google-image.sh -s bananas -c red -e jpg -p 0
$ /path-of-the-script/google-image.sh -s bananas -c red -e jpg -p 1
$ /path-of-the-script/google-image.sh -s bananas -c red -e jpg -p 2
$ /path-of-the-script/google-image.sh -s bananas -c red -e jpg -p 3
$ /path-of-the-script/google-image.sh -s bananas -c red -e jpg -p 4
The script use wget the retrieve the content of the page then it parses it to find images URL. The only tricky part is to set the user-agent (-U option of wget) to an actual web browser (here firefox), otherwise you won’t have any results. There is however some limitations: some images appears twice or more, you cannot use multiples keywords, …
#!/bin/bash
# TODO: command line takes arguments
# search, color (all), number of answers
#
# Dislay help message
function dispHelp()
{
echo "Google Image Retriever version $VERSION"
echo "Usage: gir [options]"
echo " -s : specify keywords"
echo " -e : file extension filter (gif, jpg, png, ...)"
echo " -c : dominant color: red,orange,yellow,green,teal,blue,purple,pink,white,grey,black,brown"
echo " -p : page result (20 images per pages)"
echo " -h -help : print usage information"
}
###############################################################################
### Begining/Initialisation ###
VERSION="0.1" # Program version
SEARCH=""e
EXTENSION=""
COLOR=""
OFFSET=""
### Input option parsing ###
while getopts ":s:e:c:p:h" opt; do
case $opt in
s)
SEARCH=${OPTARG}
;;
e)
EXTENSION=${OPTARG}
;;
c)
COLOR=${OPTARG}
;;
p)
OFFSET=`echo ${OPTARG}*20 | bc`
;;
h|help)
if [ $# = 3 ]
then
echo "Too much arguments with -h option: use -h for help"
exit -1
fi
dispHelp
exit 0
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
;;
esac
done
# download google page result
cmd="wget -U firefox -O - http://images.google.com/images?q=${SEARCH}\&imgcolor=${COLOR}\&start=${OFFSET}\&as_filetype=${EXTENSION}"
cmd="${cmd} | tr \"?&\" \"\\n\""
cmd="${cmd} | grep imgurl"
cmd="${cmd} | tr \"=\" \"\\n\""
cmd="${cmd} | grep http"
cmd="${cmd} | wget -U firefox -i -" # user agent: Firefox, read from pipeline
eval "${cmd}"
# remove identical files
rm *.?
rm *.??
rm *.html.?
rm *.html.??
echo "SEARCH=${SEARCH}"
echo "COLOR=${COLOR}"
echo "OFFSET=${OFFSET}"
echo "EXTENSION=${EXTENSION}"
awesome script.. I use multiple keywords with a ‘+’ sign in between the two words.. for example – Julius+Caesar… works fine for me..
plz refer me to any other useful scripts u might have..
very good script! thanks
but beware to the rm befire ending, you may erase your own script.