How to Control Screen Brightness Through Python - python

Is there a way to control screen brightness through Python? For example, I'm using Tkinter right now, and I'm wondering if there is a way I can program a button in tkinter to increase the brightness of the screen (and make another button that decreases the brightness of the screen)?
EDIT: I'm using a Windows computer, it's on Windows 10, but I also need it to work on Windows 7 too.

I used the WMI library and it really worked fine. Here is the code, but this is for Windows. I think it is OS specific so if it doesn't work for you, you should look for a better solution.
import wmi
brightness = 40 # percentage [0-100] For changing thee screen
c = wmi.WMI(namespace='wmi')
methods = c.WmiMonitorBrightnessMethods()[0]
methods.WmiSetBrightness(brightness, 0)

In tkinter you will not be able to adjust screen brightness. You might be able to make a macro and have your python program access it. Also with desktop monitors brightness is set by the monitor so depending on your model you will probably not be able to make any change at all. Good luck :)
Here is a link on using macros. Check it out and maybe find a way to control screen.

Related

How do I create something as shown below (volume change indicator) in linux?

I would like to show an image with transparent background to indicate something when a key combination is pressed.
Let's say I pressed ctrl+f3, I trigger a python script. Is there anyway I can make that happen?
What python library can I use to show an image without window border and background?
I have figured out how to trigger the file on key press. How to I deal with the (imshow) thing?
Thank you.
show an image without window border and background
This sound like task for some GUI library. There are many available but you would need test them in order to find which one can do it. First feature is generally known as frameless or borderless window. tkinter which ships with python has ability to work this way, see for example tutorialspoint.com tutorial, though I do not know how it will work with alpha channel of your image.

How to get the screen brightness in python on a windows laptop?

I'm creating a program to set my keyboard and mouse brightness according to my screen brightness.
The issue is that I don't know how to get my screen brightness (I know that i can set my screen brightness with this line:
wmi.WMI(namespace='wmi').WmiMonitorBrightnessMethods()[0].WmiSetBrightness(desired_brightness, 0))
But I want to know how to get the brightness instead of changing it.
Maybe I can use one of the windows dlls with ctypes or maybe I can do this with wmi but i don't really understand how it works.
If you have any idea on how to do that, please tell me.
Try WmiMonitorBrightness()[0].Like:
import wmi
print(wmi.WMI(namespace='wmi').WmiMonitorBrightness()[0])
This has the same result with my device:

Can you get Pygame to work properly on retina screens?

I have the same problem as here: Screen displays only in top left corner of window
The pygame container is messed up by the high resolution retina screen. I have searched around and haven't found any one with an answer other than "change your resolution".
Is it possible to fix this in the game, so that the player doesn't have to fix it themselves? Either fixing the bug in pygame or resizing the resolution when the game starts? Maybe it could work only in fullscreen somehow?
TO CLARIFY: I don't want to have to change the screen resolution manually, but within the pygame-code.
I don't know if you've figured this out at all yet as it's been a while. But you can enable low resolution mode for the pygame window in the Macs application settings. Then you don't have to change your resolution every time. I still haven't figured out how to fix this in code though.
https://support.apple.com/en-us/HT202471
Edit: I'm assuming it's a Mac you're using.
I am afraid there isn't any way. pygame hasn't been updated for a while and I think development has stopped. I ran into the same problem and ended up dealing with using a program called setresx when necessary. The problem can also be fixed by hooking up your computer to an external monitor. I know you can get some portable ones that you can put on the side of your screen.
As to changing the code, as far as I'm concerned it isn't pygame that there is anything wrong with, but technically the screen. That said, you might be able to change the library itself to handle the issue.

Using PIL (Python Image Library) to detect image on screen

I am trying to understand how I can use PIL in Python 2.7 to search the whole screen for a certain image and click on it. I've been searching around and haven't been able to find a solution. I want to create a small GUI with one button in the middle of it that when clicked will search the entire screen for a predefined image. Once the image is found the program will then click in the centre of it and end. In short the program will detect if an image is present on the users screen and click it.
I did find an interesting bit on Sikuli, but that doesn't help me because it's unable to export to an .exe.
The image that the program will look for will most likely be in the same place each time it searches, but I didn't want to hard-code the location as it has the potential to move and I don't want that being an issue later on.
What I need is the code method I would use to search for the image on screen and send back the cords to a variable.
Image explanation/example:
Reference image of rifle:
PIL is the wrong tool for this job. Instead you should look into openCV (open source computer vision), which has fantastic python bindings. Here is a link to an example (in C but should be easy to redo with the python bindings) that does what you are looking for, but even allows the image to be rotated, scaled, etc.
http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html
http://docs.opencv.org/doc/tutorials/features2d/detection_of_planar_objects/detection_of_planar_objects.html
Edit:
I assume you are using windows, as your example image looks like window. In this case you can use:
from PIL import ImageGrab
pil_img = ImageGrab.grab()
opencv_img = numpy.array(pil_img)
then use opencv to process the image to find sub image you are looking for.
If you want to do this cross platform, then you will need to use wxWidgets to do the screengrab: https://stackoverflow.com/a/10089645/455532
Even I wanted to do the same but using different module - pyautogui. I finally found the solution for my problem and I am sure this solution will also help you.
You have to just go to this webpage and read the locate function topic completely
and you'll be able to solve your problem.
I recommend you give a look on PyAutoGUI, a well documented library to control mouse and keyboard, also can locate imagens on screen, find the position, move the mouse to any location and clicks on location, also can simulate drag and drop, type on input fields, give double clicks and much more.

Take all input in Python (like UAC)

Is there any way I can create a UAC-like environment in Python? I want to basically lock the workstation without actually using the Windows lock screen. The user should not be able to do anything except, say, type a password to unlock the workstation.
You cannot do this without cooperation with operating system. Whatever you do, Ctrl-Alt-Del will allow the user to circumvent your lock.
The API call you're looking for Win32-wise is a combination of CreateDesktop and SetThreadDesktop.
In terms of the internals of Vista+ desktops, MSDN covers this, as does this blog post. This'll give you the requisite background to know what you're doing.
In terms of making it look like the UAC dialog - well, consent.exe actually takes a screenshot of the desktop and copies it to the background of the new desktop; otherwise, the desktop will be empty.
As the other answerer has pointed out - Ctrl+Alt+Delete will still work. There's no way around that - at least, not without replacing the keyboard driver, anyway.
As to how to do this in Python - it looks like pywin32 implements SetThreadDesktop etc. I'm not sure how compatible it is with Win32; if you find it doesn't work as you need, then you might need a python extension to do it. They're not nearly as hard to write as they sound.
You might be able to get the effect you desire using a GUI toolkit that draws a window that covers the entire screen, then do a global grab of the keyboard events. I'm not sure if it will catch something like ctrl-alt-del on windows, however.
For example, with Tkinter you can create a main window, then call the overrideredirect method to turn off all window decorations (the standard window titlebar and window borders, assuming your window manager has such things). You can query the size of the monitor, then set this window to that size. I'm not sure if this will let you overlay the OSX menubar, though. Finally, you can do a grab which will force all input to a specific window.
How effective this is depends on just how "locked out" you want the user to be. On a *nix/X11 system you can pretty much completely lock them out (so make sure you can remotely log in while testing, or you may have to forcibly reboot if your code has a bug). On windows or OSX the effectiveness might be a little less.
I would try with pygame, because it can lock mouse to itself and thus keep all input to itself, but i wouldn't call this secure without much testing, ctr-alt-del probably escape it, can't try on windows right now.
(not very different of Bryan Oakley's answer, except with pygame)

Categories