I'm trying to get my screen size using python. I keep getting the incorrect value because my code is taking into account the scale factor. For example: My screen resolution is set to: 2736 x 1824. My scale factor is 200% so when I execute my code I get 1368 x 912.
import win32api
width = GetSystemMetrics(0)
height = GetSystemMetrics(1)
print('Width:', width)
print('Height:', height)
Is there any way I can get the resolution as shown in my windows settings without the scale factor? I want to be able to read 2736 x 1824.
import ctypes
scaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0) / 100
You can get the scale factor via shcore using ctypes.
Then from this you can calculate the real resolution.
ie: scaleFactor 1.25 is 125%
Your application is not DPI aware. Windows has to lie to the application about the dimensions and magnify the GUI to fit the scale.
A quick fix:
import ctypes
ctypes.windll.user32.SetProcessDPIAware()
For an executable it is recommended to set DPI awareness in the manifest file.
Related
I am using Python 3.10 and moviepy library to process videos. I need to scale up (zoom) video without changing its resolution. There are a lot of example of using moviepy resize method, but it changes only the resolution.
Are there any options of scaling video with moviepy or maybe you can suggest some solutions with openCV?
For achieving zoomed in video, we may combine scaling and cropping.
Example:
from moviepy.editor import VideoFileClip
clip = VideoFileClip("input.mp4")
width, height = clip.size
resized_and_cropped_clip = clip.resize(2).crop(x1=width//2, y1=height//2, x2=width*3//2, y2=height*3//2)
resized_and_cropped_clip.write_videofile("output.mp4")
resize(2) - resize the video by a factor of 2 in each axis.
crop(x1=width//2, y1=height//2, x2=width*3//2, y2=height*3//2) - crop a rectangle with the original image size around the center of the resized video.
Alternately we may first crop and then resize.
It is more efficient, but may result minor degradation at the frame's margins:
from moviepy.editor import VideoFileClip
clip = VideoFileClip("input.mp4")
width, height = clip.size
resized_and_cropped_clip = clip.crop(x1=width//4, y1=height//4, x2=width*3//4, y2=height*3//4).resize(2)
resized_and_cropped_clip.write_videofile("output.mp4")
The above examples show zoom in by a factor of x2.
For zooming by other factor, we have to adjust the computation of x1, y1, x2, y2 arguments.
When using the win32api from pywin, I am getting incorrect values for the cursor position. My screen's resolution is 1920x1080, but when I use GetCursorPos() I have (0,0) in the top left and (1535,863) in the bottom right. The code I am using is as follows:
import win32api
def getCursor():
print win32api.GetCursorPos()
I am trying this using python 2.7 on windows 10, but I was also getting this error in python 2.6 on windows 8. Is there any solution or workaround to this problem?
You are subject to DPI virtualization. Your application has not declared itself aware of high DPI and you have a font scaling of 125%.
If you want to avoid DPI virtualization either add the high DPI aware option to the application manifest or call either SetProcessDPIAware or SetProcessDPIAwareness.
You can set the awareness level like this:
import ctypes
awareness = ctypes.c_int()
ctypes.windll.shcore.SetProcessDpiAwareness(2)
The awareness levels are defined as follows:
typedef enum _PROCESS_DPI_AWARENESS {
PROCESS_DPI_UNAWARE = 0,
/* DPI unaware. This app does not scale for DPI changes and is
always assumed to have a scale factor of 100% (96 DPI). It
will be automatically scaled by the system on any other DPI
setting. */
PROCESS_SYSTEM_DPI_AWARE = 1,
/* System DPI aware. This app does not scale for DPI changes.
It will query for the DPI once and use that value for the
lifetime of the app. If the DPI changes, the app will not
adjust to the new DPI value. It will be automatically scaled
up or down by the system when the DPI changes from the system
value. */
PROCESS_PER_MONITOR_DPI_AWARE = 2
/* Per monitor DPI aware. This app checks for the DPI when it is
created and adjusts the scale factor whenever the DPI changes.
These applications are not automatically scaled by the system. */
} PROCESS_DPI_AWARENESS;
Check this answer: https://stackoverflow.com/a/44422362/8537759
I am rendering Mandelbrot fractal on a pygame surface from a numpy array.
When I generate a 10k px * 10k px image and save it using pylab in a 10 * 10 inch image with a 1000dpi I get a 10k pixels image which render pretty well when windows build in photo app display it with zoom ajustment.
In pygame, the image looks pretty ugly although it is displayed with the same size :
I'm using this code :
pygame.init()
display = pygame.display.set_mode((1000, 1000))
surf = pygame.surfarray.make_surface(gimage)
surf = pygame.transform.rotate(surf, 90)
surf = pygame.transform.scale(surf, (1000, 1000))
How would one set pygame image size and ajust DPI ?
scale() is "fast scale operation" and doesn't use resampling.
There is also smoothscale() which uses different algorythm.
Maybe it will give you better result.
You can also use PIL/Pillow to resize() with different methods of resampling.
You can also try to use CV2 to resize().
Yesterday there was question how to use CV2 with PyGame
I'm trying to find the horizontal width of a monitor in inches or cm (not pixels!) to make a small "ruler" program. DPI would work too.
I'm using PyQt4.
try using the QDesktopWidget's width() and height() to get the width and height respectively.
Class reference at QDesktopWidget Class Reference, this will give you the screens size in pixels and then use QX11Info.appDpiX, this will give you the DPI in pixels per inch. Use both the above info to calculate the screen size in inches.
PS: The width() returns the union width, so in case you have multiple screens, it will return union width of all the screens.
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?
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.