[Script collection] Collect Bing background images

Source: Internet
Author: User
In this example, the Image module of Python Image Library (PIL) is used to process images and set the saved images to the desktop. Before that, you must use urllib to obtain the website's response. PIL is the most famous image processing suite in Python. It consists of many different modules and provides many processing functions, allowing us to process images in a simple Python program. Using libraries like PIL can help us focus on Image Processing and avoid getting lost in underlying algorithms. Since image processing involves a large number of mathematical operations, many modules in PIL are written in C language to improve processing efficiency. For more information, see here. When we use Microsoft's Bing, we will find that the background images are different every day, but each one is very beautiful. Since I recently learned python, I came up with the idea of getting the daily Bing image. The code was taken from the Internet and I just analyzed it. If you want to see the previous ones, refer to Bing Image Archive. If you want to download them, use Bing Downloader.

Work steps:

  1. Get Bing response, analyze the source code of Bing homepage, there are var g_img = {url: '\/fd \/hpk2 \/ShuBrocade_ZH-CN760216482.jpg', id: 'bgdiv ', d: 200, cN: '_ ss', crN: 'bim', hash: '000000'}; SC _bgL (), where you need to obtain the background image. Because the entire source code only has this one, you can use index ('G _ img = {url: ') to filter the address of the background image: content = urllib. urlopen ('HTTP: // cn.bing.com /'). read ()
    TempStr = content [content. index ('G _ img = {url: ') + 12: len (content)]
    TempStr = tempStr [0: tempStr. index (', id:')-1]
    TempStr = tempStr. replace ('\\','')
    BgImageUrl = 'HTTP: // cn.bing.com '+ tempStr
  2. When downloading an image, useUrlretrieve(Url, filename = None, reporthook = None, data = None)
  3. Use win32gui. SystemParametersInfo (Action, Param, WinIni)

Code
#! /Usr/bin/env python
# Coding = UTF-8
Import OS
Import sys
Import random
Import urllib
Import win32gui
Import win32con
Import Image

Class Bing:
Def _ init _ (self ):
Self. content = urllib. urlopen ("http://cn.bing.com/"). read ()
Self. bgImageUrl =''
Self. localFileName =''
Self. localbmp filename =''

Def parseImageURL (self ):
TempStr = self. content [self. content. index ('G _ img = {url: ') + 12: len (self. content)]
TempStr = tempStr [0: tempStr. index (', id:')-1]
TempStr = tempStr. replace ('\\','')
Self. bgImageUrl = 'HTTP: // cn.bing.com '+ tempStr

# Only use for generate file name in local
Def createLocalFileName (self ):
RandomStr = ''. join (random. sample (['A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'I ', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q', 'R','s ', 'T', 'U', 'V', 'w', 'x', 'y', 'z'], 6 )). replace ("","")
Self. localFileName = "D:/Bing/" + randomStr + ". jpg"
Self. localbmp filename = "D:/Bing/" + randomStr + ". bmp"

Def downloadImage (self ):
If self. bgImageUrl = "":
Self. parseImageURL ()
If self. localFileName = "":
Self. createLocalFileName ()

Data = urllib. urlretrieve (self. bgImageUrl, self. localFileName)

Def updateBGImage (self ):
Img = Image. open (self. localFileName)
Img. save (self. localbmp filename)
OS. remove (self. localFileName)
Win32gui. SystemParametersInfo (win32con. spi_set1_wallpaper, self. localbmp filename, 0)

If _ name _ = '_ main _' = "_ main __":
Bing = Bing ()
Bing. downloadImage ()
Bing. updateBGImage ()

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.