Set desktop wallpaper in Python

Source: Internet
Author: User

A piece of online viewing is fun.CodeUse python to set Windows desktop.

Original http://seasons.javaeye.com/blog/60294

This is a python snippet to set wallpaper as the photo of the day in National Geographic.

1. Install Python form http://www.python.org/download/
2. Install Python Image Library (PLI) from http://www.pythonware.com/products/pil/
3. Say the code below and run it by Python

Python code

"""
Set the wallpaper as the picture got from National Geographic picture of the day
By seasons@gmail.com
"""
Import ctypes
Import image
Import calendar
Import urllib
Import socket

Storedir = 'C:/lg_wallpaper/pod /'

Def setwallpaperfrombmp (ImagePath ):
Spi_set1_wallpaper = 20 # accordingHttp://support.microsoft.com/default.aspx? SCID = 1, 97142
# Note that this statement

Ctypes. Windll. user32.systemparametersinfoa (spi_set1_wallpaper, 0, ImagePath, 0) # spif_updateinifile | spif_sendwininichange

Def setwallpaper (ImagePath ):
"Given a path to an image, convert it to BMP and set it as Wallpaper """
BMP image = image. Open (ImagePath)
Newpath = storedir + 'mywallpaper.bmp'
BMP image. Save (newpath, "BMP ")
Setwallpaperfrombmp (newpath)

Def getpicture (fname ):
Sock = urllib. urlopen ("http://lava.nationalgeographic.com/pod/index.html ")
Htmlsource = sock. Read ()
Sock. Close ()
Pos1 = htmlsource. Find ('sm _ wallpaper ')
Pos2 = htmlsource. Find ('/pod/pictures/normal ')
Filename = htmlsource [pos1 + 13: pos2-12]
Fileurl = 'HTTP: // lava.nationalgeographic.com/pod/pictures/lg_wallpaper/'{filename
Urllib. urlretrieve (fileurl, fname)
Print 'got the picture from '+ fileurl

Def setwallpaperoftoday ():
Filename = storedir + STR (calendar. datetime. Date. Today () + '.jpg'
Print filename
Getpicture (filename)
Setwallpaper (filename)

Setwallpaperoftoday ()
Print 'wallpaper set OK! '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.