1

How do i get aspect ratio of system display resolution in python?

When I check for display resolution in Ubuntu, I see 1024x768(4:3). How do I get aspect ratio 4:3 in Python?

2
  • What do you mean "check for display resolution"? Are you using some API to get the resolution now? Commented Jul 4, 2013 at 10:49
  • I meant display resolution we find in system display settings. The resolution I see is 1366x768(16:9). I have been able to get the resolution part...But I do not know if its possible to get the aspect ratio using Python. Commented Jul 4, 2013 at 12:12

1 Answer 1

2

For this answer I used the following resource: How to get the screen size in Tkinter?

The question you're asking is (1) How do I get the screen resolution in Python (2) How do I convert this resolution to a aspect ration. I solved this problem in the following way:

import Tkinter
import Fraction as f

root   = Tkinter.Tk()

width  = root.winfo_screenwidth()
height = root.winfo_screenheight()
frac   = f.Fraction(width,height)

print frac

Note that this gives the lowest denominator. So for a 1680x1050 screen this is 8/5 instead of a possibly expected 16/10.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply Peter. I do not see the use of Fraction here..I just want the ratio specified in the system..for ex..my system has a resolution of 1366x768 with aspect ratio 16:9...How do i get 16:9 in Python.
1366x768 does not work like you expected because it is not exactly 16:9. It only approximates 16:9.
OK...but is it possible to get that ratio in Python.
If you are only interested in a few aspect ratios and want to include approximate values you could just try to compare the fraction in decimals: 1.33 for 4:3, 1.6 for 16:10, 1.77 for 16:9. Replace frac = float(width)/float(height) to get the fraction in decimals.
Could you accept or up-vote my answer if it was useful for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.