Skip to content

Instantly share code, notes, and snippets.

@ljmccarthy
Last active December 20, 2015 13:39
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 ljmccarthy/6140389 to your computer and use it in GitHub Desktop.
Save ljmccarthy/6140389 to your computer and use it in GitHub Desktop.
Calculate display PPI (pixels per inch) given resolution, aspect ratio and diagonal size.
# Calculate display PPI (pixels per inch) given resolution, aspect ratio and diagonal size.
# Luke McCarthy 2013-08-02
import math
def display_ppi(resolution, aspect_ratio, size_inches):
res_h, res_v = resolution
aspect_h, aspect_v = aspect_ratio
theta = math.atan(float(aspect_v) / aspect_h)
width = math.cos(theta) * size_inches
height = math.sin(theta) * size_inches
return (res_h / width, res_v / height)
if __name__ == '__main__':
resolution = (1920, 1080)
aspect_ratio = (16, 9)
monitor_sizes = [5, 7, 10, 18.5, 21.5, 24, 27, 32, 40, 50]
for size in monitor_sizes:
print '%.1f" @ %dx%d -> %.f PPI' % (
size, resolution[0], resolution[1], display_ppi(resolution, aspect_ratio, size)[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment