1+ import requests ,ctypes ,os ,numpy as np
2+ from PIL import Image ,ImageFont ,ImageDraw
3+ import yfinance as yf
4+ from datetime import datetime
5+
6+ #enable or disable any functionality
7+ enable_time = True
8+ enable_tickers = True
9+ enable_quote = True
10+
11+ now = datetime .now ()
12+ current_time = now .strftime ("%H:%M:%S" )
13+
14+ tickers = [
15+ ("^GSPC" ,"s&p500" ),
16+ ("aapl" ,"aapl" ),
17+ ("msft" ,"msft" ),
18+ ("goog" ,"googl" ),
19+ ("amzn" ,"amzn" ),
20+ ("tsla" ,"tsla" ),
21+ ("BTC-USD" ,"btc" ),
22+ ("ETH-USD" ,"eth" )
23+ ]
24+
25+ def add_margin (img ,top ,left ,bottom ,right ,color ):
26+ width , height = img .size
27+ new_width = width + right + left
28+ new_height = height + top + bottom
29+ result = Image .new (img .mode , (new_width , new_height ),color )
30+ result .paste (img , (left , top ))
31+ return result
32+ def conv_to_size (path ,path_to_save = None ):
33+ z = Image .open (path )
34+ if z .size [0 ]/ 1920 > z .size [1 ]/ 1080 :
35+ vres = z .size [0 ]* 1080 / 1920
36+ vres = vres - z .size [1 ]
37+ vres = int (vres / 2 )
38+ res = add_margin (z ,vres ,0 ,vres ,0 ,(0 ,0 ,0 ))
39+ else :
40+ hres = z .size [1 ]* 1920 / 1080
41+ hres = hres - z .size [0 ]
42+ hres = int (hres / 2 )
43+ res = add_margin (z ,0 ,hres ,0 ,hres ,(0 ,0 ,0 ))
44+ if path_to_save is not None :
45+ res .save (path_to_save )
46+ else :
47+ res .save (path )
48+ def ret_change (ticker ):
49+ x = yf .Ticker (ticker )
50+ z = x .history (period = "3d" )
51+ return (((z .iloc [- 1 ]["Close" ]- z .iloc [- 2 ]["Close" ])/ z .iloc [- 2 ]["Close" ])* 100 ).round (2 ),z .iloc [- 1 ]["Close" ].round (2 )
52+
53+ z = os .listdir ("path/to/folder/containing/images" )
54+
55+ #opening random image
56+ path = "path/to/folder/containing/images" + z [np .random .randint (len (z ))]
57+ z = Image .open (path )
58+
59+ #check if in correct proportion, pad if it isnt
60+ if z .size [0 ]/ z .size [1 ]> 1.78 or z .size [0 ]/ z .size [1 ]< 1.76 :
61+ conv_to_size (path )
62+ z = Image .open (path )
63+
64+ #resizing to perfect resolution
65+ z = z .resize ((1920 ,1080 ))
66+
67+
68+ #start drawing
69+ I1 = ImageDraw .Draw (z )
70+ if enable_quote :
71+ try :#write a quote on the image if it is fetched succesfully
72+
73+ myFont = ImageFont .truetype ("path/to/.ttf/font/file" ,20 )
74+ l = "http://api.quotable.io/random"
75+ r = requests .get (l )
76+ r = r .json ()
77+ text = r ["content" ]+ " - " + r ["author" ]
78+
79+ I1 .text ((10 ,1050 ), text , font = myFont , fill = (255 , 255 , 255 ))
80+ except :
81+ pass
82+ if enable_time :
83+ try :
84+ I1 .text ((1830 ,1050 ), current_time , font = myFont , fill = (255 , 255 , 255 ))
85+ except :
86+ pass
87+ if enable_tickers :
88+ try :
89+ ticpos = 10
90+ for i ,j in tickers :
91+ chn ,pc = ret_change (i )
92+ I1 .text ((1670 ,ticpos ),j , font = myFont , fill = (255 ,255 ,255 ))
93+ if chn >= 0 :
94+ I1 .text ((1750 ,ticpos ),str (chn )+ "%" , font = myFont , fill = (0 ,255 ,0 ))
95+ I1 .text ((1830 ,ticpos ),str (pc ), font = myFont , fill = (0 ,255 ,0 ))
96+ else :
97+ I1 .text ((1750 ,ticpos ),str (abs (chn ))+ "%" , font = myFont , fill = (255 ,0 ,0 ))
98+ I1 .text ((1830 ,ticpos ),str (pc ), font = myFont , fill = (255 ,0 ,0 ))
99+
100+ ticpos += 25
101+ except :
102+ pass
103+
104+ try :#save as jpg
105+ save_path = "path/to/save/temp/wallpaper/in/.jpg/format"
106+ z .save (save_path ,quality = "high" )
107+ ctypes .windll .user32 .SystemParametersInfoW (20 , 0 , save_path , 0 )
108+ except :#or save as png
109+ save_path = "path/to/save/temp/wallpaper/in/.png/format"
110+ z .save ("D:/Pictures/temp.png" ,quality = "high" )
111+ ctypes .windll .user32 .SystemParametersInfoW (20 , 0 , save_path , 0 )
0 commit comments