Generate JSON Index Of Images With awk and identify
I maintain a manifest file for a Trigger.io mobile app which includes a set of images indexed by size. I’ve taken to generating the JSON with awk
and Imagemagick’s identify
command:
identify
returns a wealth of information about an image file:
$ identify Default~iphone.png
Default~iphone.png[6] PNG 320x480 320x480+0+0 8-bit sRGB 11.5KB 0.000u 0:00.000
I want to transform that into some JSON. awk
is designed for exactly this kind
of text transformation problem.
$ identify *.png | awk '{gsub("\.png.*$", ".png", $1); printf "\"%s\": \"%s\",\n", $3, $1}' | sort
"1024x748": "Default-Portrait@2x~ipad.png",
"1024x768": "Default-Landscape~ipad.png",
"2048x1496": "Default-Landscape@2x~ipad.png",
"320x480": "Default~iphone.png",
"640x1136": "Default-568h@2x~iphone.png",
"640x960": "Default@2x~iphone.png",
"768x1004": "Default-Portrait~ipad.png",
Copy and paste into manifest, remove trailing comma, make cup of tea.
EDIT: Achieve the same effect with the -format
parameter for identify
(-format
docs here):
identify -format '"%wx%h": "%f",\n' *.png