Skip to content

Instantly share code, notes, and snippets.

@spacedman
Created April 21, 2014 19:00
Show Gist options
  • Save spacedman/11152792 to your computer and use it in GitHub Desktop.
Save spacedman/11152792 to your computer and use it in GitHub Desktop.
Tweak the DPI on an image file so that it specifies it to be a certain number of inches.
#!/bin/sh
#
# take an image and a desired width in inches, set the DPI
#
input=$1
width=$2
echo $input
echo $width
# get the current DPI, divide by width to get new DPI
density=`identify -format "%w" $input | awk "{print \\$0/$width}"`
echo $density
convert -units PixelsPerInch $input -density $density $input
@spacedman
Copy link
Author

Sample usage. Create standard rose png file from imagemagick:

$ convert rose: rose.png
$ identify -verbose rose.png | grep Print
Print size: 0.972222x0.638889
$ ../setdpi.sh rose.png 2
rose.png
2
35
$ identify -verbose rose.png | grep Print
Print size: 5.07983x3.33817
$ identify -verbose rose.png | grep Units
Units: PixelsPerCentimeter

5.07cm is 2 inches.

Now with jpg:

$ convert rose: rose.jpg
$ identify -verbose rose.jpg | grep Print
Print size: 0.972222x0.638889
$ ../setdpi.sh rose.jpg 2
rose.jpg
2
35
$ identify -verbose rose.jpg | grep Print
Print size: 2x1.31429
$ identify -verbose rose.jpg | grep Units
Units: PixelsPerInch

Jpegs units are pixels per inch so print size is 2x1.31424 inches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment