Find All Images With Given Attributes
I’m an iOS developer and with the release of Xcode 8 came a little issue. Apple introduced wide-color support on iOS 9. This meant that if you had 16 bit images then you couldn’t target older iOS versions, otherwise you would see something like this:
ERROR ITMS-90682: Invalid Bundle - The asset catalog at ‘Payload/XXXXX/Assets.car’ can’t contain 16-bit or P3 assets if the app supports iOS 8 or earlier.
So this prompted the need for me to easily find 16-bit images. Apple did help with that but it was really Xcode-project specific and I wanted to have something more generic.
Note For iOS Devs (Others can skip) #
For those of you who are finding this page because you’ve had this error, do also check this page: https://forums.developer.apple.com/thread/60919?tstart=0 and note the following:
If your Deployment Target is set to either 8.3 or 8.4 and you have an asset catalog then you will receive this same error message, even if you do not actually have 16-bit or P3 assets. In this case you will either need to lower your Deployment Target to 8.2, or move it up to 9.x.
Finding Images #
I came across ImageMagick. You can install it using MacPorts of Homebrew:
brew install imagemagick
It took me a few minutes and I composed this command:
find ./ -iname "*.png" -type f -exec identify -format '%depth %i' '{}' \; | awk '$1=16{print $1 "-bit:", $2 "\n"}'
This essentially prints all the png
images in all subfolders that are 16 bit! let’s break it down a little:
find
is the find tool that searches for files and directories.-iname "*.png"
only gives you files that end in.png
, which you can use to do a name search, or only filter by certain file extensions.-type f
makes sure you only list files and not directories.-type d
would only show directories.-exec
is a feature offind
that executes a given command for each file and directory found. In the case above we callidentify
on each file.identify
is a tool that comes with ImageMagick and gives you information on image files.-format
allows you to print only the properties that you ask for. In our case I’m printing%depth
, which is the color depth of the image (eg. 8 bit) and%i
is the file name. Other properties are available like%w
or%h
for pixel width and height of the image.awk
is a tool that lets you run a small program in your command using the AWK programming language. In my mini AWK program above I just test a condition and print something. If the first variable$1
is equal to16
(meaning we have adepth
of16
) then it prints the string I provide$1 "-bit:", $2 "\n"
; this is the image depth, followed by the string “-bit: ”, the file name and a new line.
Other Searches #
You can use this tool to search for images by size or other properties. Here’s an example of a command that searches for images that are 1024 by 1024 pixels.
find ./ -iname "*.png" -type f -exec identify -format '%w %h %i' '{}' \; | awk '$1=1024 && $2=1024 {print $1, "x", $2, ":", $3, "\n"}'