Skip to content

Instantly share code, notes, and snippets.

@CharlesNepote
Last active October 31, 2018 18:07
Show Gist options
  • Save CharlesNepote/231e26c9b351d7bfa36e312ae8cb1df9 to your computer and use it in GitHub Desktop.
Save CharlesNepote/231e26c9b351d7bfa36e312ae8cb1df9 to your computer and use it in GitHub Desktop.
read image metadata
#!/bin/bash
# Needs a few tools to be installed: perl, exiftool and jq.
# Usage: see usage() function next line.
usage()
{
echo "Usage: $0 [ -u http://ex.org/base/dir/ ] [ -o /dest/path ] [ -l ] -p </complete/path/to/pics>"
echo " -p : the absolute path to images. Mandatory."
echo " -u : base URL, where manifest.json is located,"
echo " default: \"http://localhost:8100\""
echo " -o : output directory,"
echo " default: \"./new\""
exit 2
}
if [ "$1" == "" ] ; then
usage
fi
while getopts "u:o:lp:" option; do
case "${option}" in
p)
PICSPATH=${OPTARG} ; export PICSPATH;
if [ ! -d $PICSPATH ] # Directory?
then
echo "-p value need to be an existing directory"
usage
fi;;
u)
BASEURL=${OPTARG};;
o)
OUT=${OPTARG};;
l)
;;
:) # If expected argument omitted...
echo "Error: -${OPTARG} requires an argument."
usage;;
\?)
echo "Invalid option: -$OPTARG" >&2;;
esac
done
if [ "$BASEURL" == "" ] ; then
BASEURL="http://localhost:8100"
fi
echo "base url: $BASEURL"
CURRENT_PATH=$(pwd) ; export CURRENT_PATH ; echo "Current path: $CURRENT_PATH"
if [ "$OUT" == "" ] ; then
OUT="$CURRENT_PATH/new"
fi
echo "Output directory: $OUT"
# Relative path
echo "Pics path: $PICSPATH"
REL_PATH=$(echo $PICSPATH|perl -ne 'print $1 if m/$ENV{CURRENT_PATH}(.*)/')
echo "Base dir: $REL_PATH"
#exit 2
# exiftool quick doc:
# -qq : -q : quiet and -qq very quiet
# -m : ignore errors
#
# jq playground: https://jqterm.com/
find $PICSPATH -iname "*.JPG" | while read f
do
# ... loop body
FILENAME=$(echo $f|perl -ne 'print $2 if m/(.*)\/(.*)$/') ; export FILENAME
REL_PIC_PATH=$(echo $f|perl -ne 'print $1 if m/$ENV{PICSPATH}(.*)/')
echo "File: $f" # /home/user/path/to/pics/IMG_123.JPG
echo "Filename: $FILENAME" # IMG_123.JPG
#echo "Output dir: $OUT" # /home/user/new
echo "Creating dir: $OUT$REL_PIC_PATH/full/full/0/" # /home/user/new/IMG_123.JPG/full/full/0/
mkdir -p $OUT$REL_PIC_PATH/full/full/0/
echo "Creating file: $OUT$REL_PIC_PATH/full/full/0/default.jpg" # /home/user/new/IMG_123.JPG/full/full/0/IMG_123.JPG
cp $f $OUT$REL_PIC_PATH/full/full/0/default.jpg
echo "Extract tags in info.json in $OUT$REL_PIC_PATH/ directory"
exiftool -qq -m \
-ImageHeight -ImageWidth -Creator -CopyrightNotice -license \
-j "$OUT$REL_PIC_PATH/full/full/0/default.jpg" | tr -d "[" | tr -d "]" \
| jq --slurp 'add' template.info.json - > $OUT$REL_PIC_PATH/info.pre.json
perl -pi -e \
's/"License"/"license"/;s/"ImageHeight"/"height"/;s/"ImageWidth"/"width"/;s///' \
$OUT$REL_PIC_PATH/info.pre.json
# {
# "@context": "http://iiif.io/api/image/2/context.json",
# "@id": "...",
# "protocol": "http://iiif.io/api/image",
# "SourceFile": "/media/DONNEES/dev.opensource/iiif.static/new/ADIs/20171003.1057.Dolive.3E2086.1652/IMG_5698.JPG/full/full/0/IMG_5698.JPG",
# "height": 3456,
# "width": 5184,
# "Creator": "Charles Nepote",
# "CopyrightNotice": "Licence Creative Commons CC-BY 4.0 : https://creativecommons.org/licenses/by/4.0/deed.fr",
# "license": "http://creativecommons.org/licenses/by/4.0/"
# }
jq '."@id" = "'$BASEURL$REL_PIC_PATH/full/full/0/default.jpg'"' $OUT$REL_PIC_PATH/info.pre.json \
| jq '. + { "profile" : [ "http://iiif.io/api/image/2/level0.json" ]}' - \
| jq '. + { "sizes" : [ {"width" : .width, "height": .height } ] }' - \
> $OUT$REL_PIC_PATH/info.json
# Target
# {
# "@context" : "http://iiif.io/api/image/2/context.json",
# "@id" : "http://www.example.org/IMG_001/",
# "protocol" : "http://iiif.io/api/image",
# "width" : 6000,
# "height" : 4000,
# "profile" : [ "http://iiif.io/api/image/2/level2.json" ],
# "attribution" : "Provided by Example Organization",
# "logo" : "http://example.org/images/logo.png",
# "license" : "http://rightsstatements.org/vocab/InC-EDU/1.0/"
# }
echo " "
# Add key-values for manifest.json
# https://ronallo.com/iiif-workshop/presentation/image-service.html
#
# exiftool -qq -m \
# -Title -ImageHeight -ImageWidth -Creator -CopyrightNotice -license -Description \
# -j "$OUT/$f/$FILENAME" | tr -d "[" | tr -d "]" \
# | jq --slurp 'add' template.manifest.json - > $OUT/$f/manifest.pre.json
done
# python -m SimpleHTTPServer 8000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment