Last active
March 5, 2018 09:21
-
-
Save jarmitage/5042bfe20aa54b3d8dc8 to your computer and use it in GitHub Desktop.
Himawari-8 Wallpaper Bot: Fresh images of the whole Earth from space, every 10 minutes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
import datetime as dt | |
import pytz | |
from PIL import Image | |
import numpy as np | |
import subprocess | |
import socket | |
import os | |
dir = '/FULL/PATH/TO/SCRIPT/DIR' | |
def download_latest_image(): | |
tz=pytz.timezone('Atlantic/Cape_Verde') | |
#hires = "http://rammb.cira.colostate.edu/ramsdis/online/images/hi_res/himawari-8/full_disk_ahi_true_color/full_disk_ahi_true_color_" | |
lores = "http://rammb.cira.colostate.edu/ramsdis/online/images/himawari-8/full_disk_ahi_true_color/full_disk_ahi_true_color_" | |
timestamp = dt.datetime.now(tz).strftime("%Y%m%d%H") | |
mins = int(dt.datetime.now(tz).strftime("%M")[:1]) | |
#todo: fix timestamp bug | |
if mins != 0: | |
mins = mins - 1 | |
timestamp = timestamp + str(mins) + '000' | |
filetype = ".jpg" | |
img_url = lores + timestamp + filetype | |
#img_url = hires + timestamp + filetype | |
img_loc = dir + '/img/' + timestamp + filetype | |
urllib.urlretrieve(img_url, img_loc, reporthook=report) | |
size = 1000, 1000 | |
im = Image.open(dir + '/img/' + timestamp + filetype).convert('RGB') | |
im = im.resize(size, Image.ANTIALIAS) | |
logo_dims = (180, 80) | |
logo_box = (0, size[0] - logo_dims[1], logo_dims[0], size[1]) | |
logo = np.array(im.crop(logo_box)) | |
logo[..., ...][...] = (0, 0, 0) | |
logo_edited = Image.fromarray(logo) | |
im.paste(logo_edited, logo_box) | |
border = (1000, 200) | |
new_size = (size[0] + border[0], size[0] + border[1]) | |
new_im = Image.new('RGB', new_size) | |
paste_pos = (border[0]/2, border[1]/2, new_size[0] - (border[0]/2), new_size[1] - (border[1]/2)) | |
new_im.paste(im, paste_pos) | |
new_im_name = timestamp + filetype | |
new_im.save(dir + '/img/' + new_im_name) | |
cmd = 'sh ' + dir + '/set_desktop.sh ' + dir + '/img/' + new_im_name | |
subprocess.call(cmd, shell=True) | |
for file in os.listdir(dir + '/img/'): | |
if file != timestamp + filetype: | |
os.remove(dir + '/img/' + file) | |
def report(count, blockSize, totalSize): | |
percent = int(count * blockSize * 100 / totalSize) | |
print("\r%d%%" % percent + ' complete') | |
def is_connected(): | |
try: | |
host = socket.gethostbyname('www.google.com') | |
s = socket.create_connection((host, 80), 2) | |
return True | |
except: | |
pass | |
return False | |
if is_connected(): | |
download_latest_image() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '$1'" && killall Dock |
Haha, I felt bad about removing the logo! But I figured I'm not reproducing the image online without it (apart from in the screenshot!)
Ah yes, should have mentioned about that dependency.
Yup, dir
should be full path. I'll update to make that more obvious.
Thanks for stopping by :)
I made this work on Linux Mint (Cinnamon) by changing just this line
cmd = 'gsettings set org.cinnamon.desktop.background picture-uri "file:///' + dir + '/img/' + new_im_name+'"'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was thinking why you need all that logo_box stuff, until I saw you're covering the logo up, haha, good one.
I was getting:
Fixed with:
Also,
dir
variable should contain full path to your folder (from the root), since thesqlite3
command will not receive relative paths.And finally, to set the cron, try using
crontab
.