Resize display resolution using a python function. It should be cross platform, ie support for windows, linux and mac (it is okay to have multiple cases depending on the operating system)
I have code which I think works on linux (Ubuntu) I am looking for a solution for windows and mac (should support both 32 and 64 bit machines)
def SetResolution(width, height):
os.popen("xrandr -s "+str(width)+'x'+str(height))
I would also appreciate it if somebody could tell me how I could get the possible display resolutions for windows and mac
my function on linux is this:
def GetResolutions():
screen = os.popen("xrandr").readlines()
possibleResolutions = []
for a in screen:
data = a.split()
if len(data)<4:
width, height = data[0].split('x')
fps = re.sub("[^0-9.]", "", data[1])
possibleResolutions.append({'width':int(width),'height':int(height),'fps':float(fps)})
if '*' in data[1]:
currentResolution = {'width':int(width),'height':int(height),'fps':float(fps)}
return possibleResolutions, currentResolution
To get and set the resolution under Windows (both 32 and 64 bits) you can use the ctypes and the user32 dll (ctypes.windll.user32). Don't care about the '32' in the name of the dll - it's a 64 bits dll on 64 bits Windows. This library you can use also to discover the allowed resolutions.
Alternatively you can use some command line tool like nircmd.exe :
The last number is the colour depth.
Hope this helps.
Below is a solution that works on Windows (depends on pywin32). There are placeholders where you can put in your existing linux code, I'm not sure what to do about OS X though.
Many of the answers are already scattered around StackOverflow and can be summarized as follows.
To get the resolution on Windows in a purely pythonic fashion (reference: https://stackoverflow.com/a/3129524/2942522):
The MacOS solution also uses Python, but uses a package outside the standard library (reference: https://stackoverflow.com/a/3129567/2942522):
Apparently the list comprehension will iterate over the screens in a multiple monitor setup.
I think Alex Martelli's response to a related issue (https://stackoverflow.com/a/2662892/2942522) is also notable. He uses:
to get a list of largest to smallest resolutions available (although
pygame
would become a dependency if you went this route). Conversely, I suspect it would work just fine in a cross-platform setting. Furthermore, he mentionspygame.display.set_mode
for setting the resolution (docs: http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode). Here's a snippet of the docs forset_mode
:"The resolution argument is a pair of numbers representing the width and height. The flags argument is a collection of additional options. The depth argument represents the number of bits to use for color."
Maybe that will get you started. At the very least you could perhaps check the source code for
set_mode
to see if there's some possible inspiration there if you cannot use it directly.Other potentially useful ideas:
sys.platform
(docs: http://docs.python.org/2/library/sys.html#sys.platform). This returns'darwin'
on MacOS.platform
module. If I runplatform.architecture()
on my machine it returns a tuple:('64bit', '')
(docs: http://docs.python.org/2/library/platform.html#platform.architecture)