-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc.py
74 lines (59 loc) · 2.56 KB
/
wc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /usr/bin/python3 -u
# -*- coding: utf-8 -*-
import requests
import datetime
import json
import sys
import base64
from requests.exceptions import HTTPError
from PIL import Image, ImageFont, ImageDraw, ImageOps
from picamera import PiCamera
from time import sleep
###################
## Capture photo ##
###################
camera = PiCamera()
camera.resolution = (1080, 720)
sleep(5)
camera.capture('/tmp/picture.jpg')
############################################################
## Get weather information from the OpenWatherMap.org API ##
############################################################
response = requests.get('http://api.openweathermap.org/data/2.5/weather?q=Belo Horizonte,br&APPID=YOUR-OWM-APP-ID')
response.raise_for_status()
jsonResponse = json.loads(response.text)
################################################
## Draws the weather information on the image ##
################################################
now = datetime.datetime.now()
city_name = "{}".format(jsonResponse['name'])
date = "Date: " + now.strftime("%B,%d - %Y | %Hh%M")
summary = "Summary: {}".format(jsonResponse['weather'][0]['main'])
temperature = "Temperature: {:.1f}".format(jsonResponse['main']['temp']-273.15)+chr(176)+"C"
pressure = "Pressure: {}".format(jsonResponse['main']['pressure'])+" mb"
humidity = "Humidity: {}".format(jsonResponse['main']['humidity'])+"%"
font_title = ImageFont.truetype("Impact", 55)
font = ImageFont.truetype("FreeSans", 24)
img = Image.open('/tmp/picture.jpg')
draw = ImageDraw.Draw(img)
draw.text((50, 40), city_name, (255,255,255), font=font_title, stroke_width=2, stroke_fill="#000")
draw.text((50, 130), date, (255,255,255), font=font, stroke_width=2, stroke_fill="#000")
draw.text((50, 170), summary, (255,255,255), font=font, stroke_width=2, stroke_fill="#000")
draw.text((50, 210), temperature, (255,255,255), font=font, stroke_width=2, stroke_fill="#000")
draw.text((50, 250), pressure, (255,255,255), font=font, stroke_width=2, stroke_fill="#000")
draw.text((50, 290), humidity, (255,255,255), font=font, stroke_width=2, stroke_fill="#000")
img.save('/tmp/picture.jpg')
#########################
## Send Image to ImGur ##
#########################
with open("/tmp/picture.jpg", "rb") as img_file:
photo = base64.b64encode(img_file.read())
url = "https://api.imgur.com/3/image"
payload = {'image': photo,
'name': 'YOUR-IMAGE-NAME',
'title': 'YOUR-ALBUM-NAME',
'album': 'YOUR-ALBUM-ID'
}
files = [ ]
headers = {'Authorization': 'Bearer YOUR-IMGUR-API-KEY'}
response = requests.request("POST", url, headers=headers, data = payload)