Skip to content

Instantly share code, notes, and snippets.

@JinBridger
Last active May 22, 2024 15:10
Show Gist options
  • Save JinBridger/82936502c80130fbe1f59fa33e192d08 to your computer and use it in GitHub Desktop.
Save JinBridger/82936502c80130fbe1f59fa33e192d08 to your computer and use it in GitHub Desktop.
# Use latest himawari 8 photo as wallpaper
# for Windows user
from datetime import datetime, timezone, timedelta
import requests
import ctypes
from PIL import Image, ImageDraw, ImageFont
import cv2
import numpy as np
minutes_before = 30
resolution = 1500
max_retry = 10
slices = 4
base_path = r"PATH/TO/PROJECT/DIR"
font_path = r"PATH/TO/FONT.TTF"
earth_img_path = base_path + r"\latest-earth.jpg"
wallpaper_img_path = base_path + r"\wallpaper.jpg"
utc_now = datetime.now(timezone.utc)
formatted_time = utc_now - timedelta(
minutes=minutes_before + utc_now.minute % 10,
seconds=utc_now.second,
)
def download_earth_img():
for i in range(slices):
for j in range(slices):
url = "https://himawari8.nict.go.jp/img/D531106/{}d/550/{:04d}/{:02d}/{:02d}/{}_{}_{}.png".format(
slices,
formatted_time.year,
formatted_time.month,
formatted_time.day,
formatted_time.strftime("%H%M%S"),
i,
j,
)
retry_cnt = 1
while True:
try:
response = requests.get(url)
response.raise_for_status()
break
except Exception as e:
if retry_cnt >= max_retry:
print(f"max retry exceeded: {e}")
break
print(f"retrying... {retry_cnt}")
retry_cnt += 1
temp_img_path = base_path + "/temp_{}_{}.png".format(i, j)
with open(temp_img_path, "wb") as f:
f.write(response.content)
imgs = []
for j in range(slices):
for i in range(slices):
temp_img_path = base_path + "/temp_{}_{}.png".format(i, j)
img = cv2.imread(temp_img_path)
imgs.append(img)
rows = []
for i in range(0, slices * slices, slices):
row = np.hstack(imgs[i:i + slices])
rows.append(row)
earth_img = np.vstack(rows)
earth_img = cv2.resize(earth_img, (resolution, resolution))
cv2.imwrite(earth_img_path, earth_img)
def color_correct(img_path, new_img_path):
img = cv2.imread(img_path)
def levels(img, thres):
inBlack = np.array([0, 0, 0], dtype=np.float32)
inWhite = np.array([255, 255, 255], dtype=np.float32)
inGamma = np.array([thres, thres, thres], dtype=np.float32)
outBlack = np.array([0, 0, 0], dtype=np.float32)
outWhite = np.array([255, 255, 255], dtype=np.float32)
img = np.clip( (img - inBlack) / (inWhite - inBlack), 0, 255 )
img = ( img ** (1/inGamma) ) * (outWhite - outBlack) + outBlack
img = np.clip( img, 0, 255).astype(np.uint8)
return img
# increase levels to 1.30
img = levels(img, 1.30)
# increase saturation by 15%
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(img)
s = cv2.add(s, 0.15)
img = cv2.merge((h, s, v))
img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
# split color into R, G, B
b, g, r = cv2.split(img)
new_r = 0.2 * g + 0.8 * r
new_g = 0.33 * r + 0.66 * g
new_b = 0.2 * r + 0.8 * b
new_img = cv2.merge((new_b, new_g, new_r))
# new_img = cv2.convertScaleAbs(new_img, alpha=1.0, beta=0)
new_img = levels(new_img, 1.40)
cv2.imwrite(new_img_path, new_img)
def generate_wallpaper():
wallpaper = Image.new("RGB", (3840, 2160), "black")
earth_img = Image.open(earth_img_path)
start_x = (wallpaper.width - earth_img.width) // 2
start_y = (wallpaper.height - earth_img.height) // 2
wallpaper.paste(earth_img, (start_x, start_y))
draw = ImageDraw.Draw(wallpaper)
text = f"Last Update: {formatted_time.astimezone().strftime("%Y-%m-%d %H:%M:%S")}"
text_width = draw.textlength(
text, font=ImageFont.truetype(font_path, 25)
)
draw.text(
((wallpaper.width - text_width) / 2, wallpaper.height - 200),
text,
fill=(100, 100, 100, 100),
font=ImageFont.truetype(font_path, 25),
)
wallpaper.save(wallpaper_img_path)
if __name__ == '__main__':
download_earth_img()
color_correct(earth_img_path, earth_img_path)
generate_wallpaper()
ctypes.windll.user32.SystemParametersInfoW(20, 0, wallpaper_img_path, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment