-
-
Save deadalusai/114bec39e8d1ed8aeaf0 to your computer and use it in GitHub Desktop.
Windows Powershell Script to download the latest image from the Himawari-8 satelite, combine the tiles into a single image, convert to jpg and then set as the desktop background.
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
# | |
# Himawari-8 Downloader | |
# | |
# | |
# | |
# This script will scrape the latest image from the Himawari-8 satellite, recombining the tiled image, | |
# converting it to a JPG which is saved in My Pictures\Himawari\ and then set as the desktop background. | |
# | |
# http://himawari8.nict.go.jp/himawari8-image.htm | |
# | |
# | |
param( | |
[Switch] $Force, | |
[Switch] $StoreLatestOnly | |
) | |
Write-Output "Checking for latest" | |
$latest_result = $null | |
try { | |
$cachebuster = [DateTime]::UtcNow.ToFileTime() | |
$response = Invoke-WebRequest "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/latest.json?uid=$cachebuster" | |
$latest_result = $response.Content | ConvertFrom-Json | |
} | |
catch { | |
Write-Error "Failed to retrieve latest ($x, $y): $($_.Exception.Message)" | |
return | |
} | |
if (!$latest_result -or !$latest_result.date) { | |
Write-Error 'Unable to retrieve latest date' | |
return | |
} | |
$latest_date = (Get-Date $latest_result.Date) | |
$width = 550 | |
$level = 4 #Level can be 4, 8, 16, 20 | |
$time = $latest_date.ToString("HHmmss") | |
$year = $latest_date.ToString("yyyy") | |
$month = $latest_date.ToString("MM") | |
$day = $latest_date.ToString("dd") | |
#Create the folder My Pictures\Himawari\ if it doesnt exist | |
$outpath = (Join-Path ([Environment]::GetFolderPath("MyPictures")) "Himawari") | |
if(!(Test-Path $outpath)) | |
{ | |
[void](New-Item -ItemType directory -Path $outpath) | |
} | |
# The filename that will be saved | |
if ($StoreLatestOnly.IsPresent) { | |
# Use this to have the script just store the latest file only | |
$outfile = Join-Path $outpath "latest.jpg" | |
} | |
else { | |
# Use this to have the files accumulate in the directory: | |
$outfile = Join-Path $outpath "$($year)$($month)$($day)_$($time).jpg" | |
} | |
# Have we already downloaded this one? | |
# Override this check when $StoreLatestOnly or $Force was provided | |
if (!$StoreLatestOnly.IsPresent -and (!$Force.IsPresent -and (Test-Path $outfile))) { | |
Write-Warning "$outfile already exists! Use -Force to redownload." | |
return | |
} | |
[void][Reflection.Assembly]::LoadWithPartialName("System.Drawing") | |
$image = New-Object System.Drawing.Bitmap(($width * $level), ($width * $level)) | |
try { | |
# Settings for saving images as JPEG | |
$qualityEncoder = [System.Drawing.Imaging.Encoder]::Quality | |
$encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1) | |
$encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($qualityEncoder, 90) | |
$jpegCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | where MimeType -eq 'image/jpeg' | |
# Drawing context | |
$graphics = [System.Drawing.Graphics]::FromImage($image) | |
$graphics.Clear([System.Drawing.Color]::Black) | |
for ($y = 0; $y -lt $level; $y++) | |
{ | |
for ($x = 0; $x -lt $level; $x++) | |
{ | |
$block_url = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/$($level)d/$width/$year/$month/$day/$($time)_$($x)_$($y).png" | |
Write-Output "Downloading chunk: $block_url" | |
try | |
{ | |
$response = Invoke-WebRequest $block_url | |
if ($response.Headers.'Content-Type' -ne 'image/png') { | |
Write-Error 'Received non-PNG response to chunk query' | |
return | |
} | |
$imgblock = [System.Drawing.Image]::FromStream((new-object System.IO.MemoryStream((, [byte[]] $response.Content)))) | |
#$imgblock.Save((Join-Path $outpath "image_$($x)_$($y).png"), $jpegCodecInfo, $encoderParams) | |
try { | |
$graphics.DrawImage($imgblock, ($x * $width), ($y * $width) , $width, $width) | |
} | |
finally { | |
$imgblock.Dispose() | |
} | |
} | |
catch { | |
Write-Error "Failed to download chunk ($x, $y): $($_.Exception.Message)" | |
return | |
} | |
} | |
} | |
Write-Output "Writing to $outfile..." | |
$image.save($outfile, $jpegCodecInfo, $encoderParams) | |
} | |
finally { | |
$image.Dispose() | |
} | |
<# | |
Different settings for the wallpaper: | |
Tile : | |
key.SetValue(@"WallpaperStyle", "0") ; | |
key.SetValue(@"TileWallpaper", "1") ; | |
break; | |
Center : | |
key.SetValue(@"WallpaperStyle", "0") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
Stretch : | |
key.SetValue(@"WallpaperStyle", "2") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
Fill : | |
key.SetValue(@"WallpaperStyle", "10") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
Fit : | |
key.SetValue(@"WallpaperStyle", "6") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
#> | |
Write-Output "Setting Wallpaper..." | |
Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name Wallpaper -value $outfile | |
Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name WallpaperStyle -value 6 | |
Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name TileWallpaper -value 0 | |
Set-ItemProperty 'HKCU:\Control Panel\Colors' -name Background -Value "0 0 0" | |
#rundll32.exe user32.dll, UpdatePerUserSystemParameters | |
$source = @" | |
using System.Runtime.InteropServices; | |
public class Wallpaper | |
{ | |
public const int SetDesktopWallpaper = 20; | |
public const int UpdateIniFile = 0x01; | |
public const int SendWinIniChange = 0x02; | |
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] | |
private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni); | |
public static void SetWallpaper(string path) | |
{ | |
SystemParametersInfo(SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange); | |
} | |
} | |
"@ | |
Add-Type -TypeDefinition $source | |
[Wallpaper]::SetWallpaper($outfile) | |
Write-Output "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment