Python: captures images through cameras and automatically uploads them to Sina Weibo

Source: Internet
Author: User

In the previous article "Python: monitoring functions implemented by cameras", I suddenly thought that I could upload the images I collected to Weibo directly, and then I could use my mobile phone to view the desired location in real time, the implementation idea is as follows:

A. program a collects and stores images every 30 s through the camera;

B. Program B uploads the collected images to Sina Weibo every 30 s;

The implementation is as follows:

1. image acquisition procedure:

#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-from videocapture import deviceimport time # Save up to five captured images, more than five, overwrite the oldest one, cyclically loop max_pic_num = 5 # capture frequency, capture sleep_time_long = 30 seconds # initialize camera cam = Device (devnum = 0, showvideowindow = 0) inum = 0 while true: # capture cam. savesnapshot (STR (inum) + '.jpg ', timestamp = 3, boldfont = 1, quality = 75) # sleep for a minute. sleep (sleep_time_long) # If there are more than five copies, the previous one will be overwritten. Otherwise, the hard disk will soon be filled with if inum = max_pic_num: inum = 0 else: inum + = 1

2. upload an image to Sina Weibo app B:

#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-from weibopy. auth import oauthhandlerfrom weibopy. API import apiimport configparserimport timemax_pic_num = 5sleep_time_long = 30def press_sina_weibo (): ''' call Sina Weibo open API to write a blog via command line. features to be improved: Author: Socrates Date: Sina Weibo: @ yourobject'' sina_weibo_config = configparser. configparser () # Read the appkey configuration file try: sina_weibo_config.readfp (open ('sina _ weibo_config.ini ') cannot configparser. error: Print 'read sina_weibo_config.ini failed. '# obtain required information consumer_key = sina_weibo_config.get ("userinfo", "consumer_key") consumer_secret = sums ("userinfo", "consumer_secret") token = sums ("userinfo ", "token") token_sercet = sina_weibo_config.get ("userinfo", "token_secret") # Call Sina Weibo openapi (Python version) auth = oauthhandler (consumer_key, consumer_secret) Auth. settoken (token, token_sercet) API = API (auth) # enter the content to be released through the command line # weibo_content = raw_input ('Please input content: ') # status = API. update_status (status = weibo_content) # print "Press Sina Weibo successful, content is: % s" % status. text inum = 0 while true: # If the name and content of the uploaded image are repeated, the open API checks, the content adopts the mechanism of retrieving the current time # The image name traverses status = API from the 0-5 loop. upload (STR (inum) + '.jpg ', time. strftime ("% A, % d % B % Y % H: % m: % S + 0000", time. gmtime () time. sleep (sleep_time_long) If inum = max_pic_num: inum = 0 else: inum + = 1 if _ name _ = '_ main _': press_sina_weibo ()

3. Test

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.