On my MacBook Pro with retina display, when I take a screenshot, the image is saved with a DPI of 144. But, using ImageOptim to compress the image with Strip JPEG/PNG Metadata enabled will remove (or reset) the DPI setting to 72 DPI - resulting in a double sized image when displayed in Preview and Quick Look. Here is how I correct this.
The Problem
Here is what I have noticed with macOS (even up to Catalina 10.15):
- Quick Look does not bother with the DPI and instead only looks at the file name - if the filename ends with "@2x" it assumes the image is a retina image!
- However, Preview ignores the filename, and instead looks at the image metadata to determine the DPI.
Filename | Correct 144 DPI | Incorrect 72 DPI |
---|---|---|
Ends with @2x |
Quick Look: ✔︎ Preview: ✔︎ |
Quick Look: ✔︎ Preview: ✘ |
Does not | Quick Look: ✘ Preview: ✔︎ |
Quick Look: ✘ Preview: ✘ |
Here’s how Preview, under Tools > Adjust Size... shows the DPI or “resolution” for a screen shot at 144 pixels per inch:
In Preview, make sure View > Actual Size is checked.
TL;DR - a retina screenshot should have the DPI set, and a filename that ends with @2x
!
The Fix
To fix this for a PNG image, the in-built macOS SIPS tool can be used to set the DPI in-place:
sips -s dpiWidth 144 -s dpiHeight 144 img.png
However, using SIPS this way does not fix JPEGs, or rather, I’m probably missing something. Therefore, I use the renowned ExifTool by Phil Harvey:
exiftool -Xresolution=144 -Yresolution=144 -overwrite_original img.jpg
Warning: -overwrite_original
means no backup file!
Scripting it all together:
#!/bin/bash
/Applications/ImageOptim.app/Contents/MacOS/ImageOptim "$1" &>/dev/null
[[ "$1" == *.png ]] && sips -s dpiWidth 144 -s dpiHeight 144 "$1"
[[ "$1" == *.jpg ]] && ./exiftool -Xresolution=144 -Yresolution=144 -overwrite_original "$1"