Skip to content

Instantly share code, notes, and snippets.

@sepastian
Last active October 14, 2022 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sepastian/c3497462006d5e681d5c2895c9a77744 to your computer and use it in GitHub Desktop.
Save sepastian/c3497462006d5e681d5c2895c9a77744 to your computer and use it in GitHub Desktop.
Determine Image DPI
# Based on https://apple.stackexchange.com/a/389067/453491
# In order to calculate DPI, determine units and resolution first.
identify -verbose warc2corpus@2x_300dpi.png | grep -E -i "resolution|units"
# Resolution: 118.11x118.11
# Units: PixelsPerCentimeter
# png:pHYs: x_res=11811, y_res=11811, units=1
# 1 PixelsPerCentimeter corresponds to 2.54 DPI.
# Calculate DPI using the following command.
identify -format "%[fx:resolution.x*2.54] DPI" warc2corpus@2x_300dpi.png
# 299.999 DPI
# Caveats
#
# If the command below does not return units/resolution,
# this information is missing from the image. This is the case
# for images exported from whimsical.com, for example.
identify -verbose warc2corpus@2x.png | grep -E -i "resolution|units"
# Units: Undefined
# ... in this case, open and save the image to a new file using,
# for example, Krita or GIMP. Then, try again:
identify -verbose warc2corpus@2x_fix.png | grep -E -i "resolution|units"
Resolution: 28.34x28.34
Units: PixelsPerCentimeter
png:pHYs: x_res=2834, y_res=2834, units=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment