-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_icon.py
97 lines (86 loc) · 3.22 KB
/
weather_icon.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# import the necessary packages
import numpy as np
import urllib
import cv2
import requests
import json
import urllib.request
import pygame
# METHOD #1: OpenCV, NumPy, and urllib to get the weather details
api_key = "a68a0ee8420eb5ffcb2a5ce643107da7"
lat = "10.075239"
lon = "108.224136"
location = "Turan"
url = "https://api.openweathermap.org/data/2.5/find?q={}&units=metric&appid={}".format(location, api_key)
response = requests.get(url)
data = json.loads(response.text)
#Print the weather dtails
print("Temperature is ", data["list"][0]["main"]["temp"])
print("Pressure is ", data["list"][0]["main"]["pressure"])
print("Humidity is ", data["list"][0]["main"]["humidity"])
print("Icon number is", data["list"][0]["weather"][0]["icon"])
# Get the weather icon
url = "https://openweathermap.org/img/wn/{}.png".format(data["list"][0]["weather"][0]["icon"])
#url = "https://openweathermap.org/img/wn/01d.png"
#url_response = urllib.request.urlopen(url)
#image = cv2.imdecode(np.array(bytearray(url_response.read()), dtype=np.uint8), -1)
resp = requests.get(url, stream=True).raw
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, -1)
trans_mask = image[:,:,3] == 0
#replace areas of transparency with white and not transparent
image[trans_mask] = [103, 65, 100, 255]
#new image without alpha channel...
new_img = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
#Change the icon to required image dimensions
#height_image =28
#width_image = 36
#marginx = 8
#marginy = 6
#crop_img = new_img[marginy:marginy+height_image, marginx:marginx+width_image]
#print("Size is", crop_img.shape)
# Set the pygame background colour and size
background_colour = (0,0,0)
(width, height) = (1000, 1000)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Weather')
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
# Show original icon image
cv2.imshow('Icon',image)
# Show the croppedweather icon
#cv2.imshow('Cropped icon',crop_img)
# Print out all the panels
# First set an are to be white
for x in range (1,100,1):
for y in range(20,70,1):
pygame.draw.circle(screen, (255,255,255), (x*10,y*10), 5)
# Now print all the individual panes
# Humidity Red
for x in range (14,24,1):
for y in range(20,70,1):
pygame.draw.circle(screen, (0,255, 0), (x*10,y*10), 5)
# Temperature Blue
for x in range (2,12,1):
for y in range(20,70,1):
pygame.draw.circle(screen, (0,0,255), (x*10,y*10), 5)
# Weather icon
for x1 in range (0,50,1):
for y1 in range(2,50,1):
#Get the color values from the icon image
(b,g,r) = new_img[y1,x1]
pygame.draw.circle(screen, (r,g,b), ((x1*10)+260,(y1*10)+190), 5)
# Pollution Green
for x in range (77,87,1):
for y in range(20,70,1):
pygame.draw.circle(screen, (100, 100, 100), (x*10,y*10), 5)
# Air Presssure Blue
for x in range (89,99,1):
for y in range(20,70,1):
pygame.draw.circle(screen, (14,140, 80), (x*10,y*10), 5)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False