- 1 year ago
Set Windows Wallpaper fit/span/position using Python Script
To set the Windows wallpaper fit, span, or position using a Python script, you can make use of the ctypes
library to interact with the Windows API. Here's an example script that demonstrates how to achieve this:
pythonimport ctypes
# Constants for wallpaper style
SPI_SETDESKWALLPAPER = 0x0014
SPIF_UPDATEINIFILE = 0x01
SPIF_SENDCHANGE = 0x02
# Constants for wallpaper position
WPSTYLE_CENTER = 0
WPSTYLE_TILE = 1
WPSTYLE_STRETCH = 2
WPSTYLE_FILL = 3
WPSTYLE_FIT = 4
WPSTYLE_SPAN = 5
def set_wallpaper(image_path, style):
# Set the wallpaper style
if style in [WPSTYLE_CENTER, WPSTYLE_TILE, WPSTYLE_STRETCH, WPSTYLE_FILL, WPSTYLE_FIT, WPSTYLE_SPAN]:
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, image_path, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE)
ctypes.windll.user32.SystemParametersInfoW(0x0014, 0, image_path, 3)
print(f"Wallpaper set with style: {style}")
else:
print("Invalid wallpaper style")
# Example usage
image_path = r"C:\path\to\your\image.jpg"
style = WPSTYLE_FILL # Choose the desired style: WPSTYLE_FILL, WPSTYLE_FIT, WPSTYLE_SPAN, etc.
set_wallpaper(image_path, style)
In this script, the set_wallpaper
function takes the path of the image file and the desired wallpaper style as arguments. The ctypes.windll.user32.SystemParametersInfoW
function is used to set the wallpaper with the specified style. The SPI_SETDESKWALLPAPER
parameter indicates the action of setting the wallpaper, and the SPIF_UPDATEINIFILE
and SPIF_SENDCHANGE
flags specify that the change should be saved to the user's profile and broadcast to other applications, respectively.
You can modify the style
variable to one of the available wallpaper styles such as WPSTYLE_FILL
, WPSTYLE_FIT
, WPSTYLE_SPAN
, etc., according to your requirements.
Make sure to provide the correct path to your image file in the image_path
variable. Note that this script uses the SystemParametersInfoW
function from the Windows API, which expects the Unicode version of the function (SystemParametersInfoW
) to work with Unicode paths.