Skip to content

Instantly share code, notes, and snippets.

@t184256
Created November 13, 2015 13:14
Show Gist options
  • Save t184256/a54ca203e7fbfcc69416 to your computer and use it in GitHub Desktop.
Save t184256/a54ca203e7fbfcc69416 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Given the resolution and diagonal, calculate the mm size of the screen and the ppi
import sys, math
MM_IN_INCH = 25.4
def size_and_ppi_from_resolution_and_diagonal(x, y, d): # pix, pix, mm
x, y, d = float(x), float(y), float(d)
ratio = x/y
y_mm = d / math.sqrt( 1 + ratio**2 )
x_mm = y_mm * ratio
d_pix = math.sqrt(x**2 + y**2)
d_in = d / MM_IN_INCH
ppi = d_pix / d_in
return x_mm, y_mm, ppi
if __name__ == '__main__':
if len(sys.argv) < 4:
print 'Usage:', sys.argv[0], ' x_res y_res diagonal (inches)'
exit(1)
x, y, d = float(sys.argv[1]), float(sys.argv[2]), float(sys.argv[3])*MM_IN_INCH
x_mm, y_mm, ppi = size_and_ppi_from_resolution_and_diagonal(x, y, d)
print x_mm, 'mm'
print y_mm, 'mm'
print ppi, 'PPI'
print x_mm*y_mm/100, 'cm^2'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment