Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Himawari-8 Wallpaper Bot: Fresh images of the whole Earth from space, every 10 minutes
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()
#!/bin/bash
sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '$1'" && killall Dock
@jarmitage

This comment has been minimized.

Copy link
Owner Author

commented Dec 30, 2015

Desktop

@jarmitage

This comment has been minimized.

Copy link
Owner Author

commented Dec 30, 2015

Cron:

*/10 * * * * python /dir/to/himawari-8.py

@kinduff

This comment has been minimized.

Copy link

commented Dec 31, 2015

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:

Traceback (most recent call last):
  File "himawari-8.py", line 4, in <module>
    from PIL import Image
ImportError: No module named PIL

Fixed with:

sudo pip install image

Also, dir variable should contain full path to your folder (from the root), since the sqlite3 command will not receive relative paths.

And finally, to set the cron, try using crontab.

@jarmitage

This comment has been minimized.

Copy link
Owner Author

commented Dec 31, 2015

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 :)

@RaedsLab

This comment has been minimized.

Copy link

commented Dec 31, 2015

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
You can’t perform that action at this time.