Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jbolster
Forked from MichaelPote/himawari.ps1
Created February 4, 2016 10:22
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jbolster/6e245f82df81cc98d87d to your computer and use it in GitHub Desktop.
Save jbolster/6e245f82df81cc98d87d 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.
#
# 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
#
#
$latestInfoUri = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/latest.json?" + (New-Guid).ToString();
$latestInfo = Invoke-RestMethod -Uri $latestInfoUri
$now = Get-Date $latestInfo.date;
$width = 550
$level = "4d" #Level can be 4d, 8d, 16d, 20d
$numblocks = 4 #this apparently corresponds directly with the level, keep this exactly the same as level without the 'd'
$timeString = $now.ToString("yyyy/MM/dd/HHmmss")
#Create the folder My Pictures\Himawari\ if it doesnt exist
$outpath = [Environment]::GetFolderPath("MyPictures") + "\Himawari\"
if(!(Test-Path -Path $outpath ))
{
[void](New-Item -ItemType directory -Path $outpath)
}
#The filename that will be saved:
#Uncomment this to have the files accumulate in the directory:
#$outfile = "$year$month$day"+"_" + $time + ".jpg"
#Use this to have the script just store the latest file only:
$outfile = "latest.jpg"
$url = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/$level/$width/$timeString"
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$image = New-Object System.Drawing.Bitmap(($width * $numblocks), ($width * $numblocks))
$graphics = [System.Drawing.Graphics]::FromImage($image)
$graphics.Clear([System.Drawing.Color]::Black)
for ($y = 0; $y -lt $numblocks; $y++)
{
for ($x = 0; $x -lt $numblocks; $x++)
{
$thisurl = $url + "_" + [String]$x + "_" + [String]$y + ".png"
Write-Output "Downloading: $thisurl"
try
{
$request = [System.Net.WebRequest]::create($thisurl)
$response = $request.getResponse()
$HTTP_Status = [int]$response.StatusCode
If ($HTTP_Status -eq 200)
{
$imgblock = [System.Drawing.Image]::fromStream($response.getResponseStream())
$graphics.DrawImage($imgblock,($x*$width),($y*$width) , $width, $width)
$imgblock.dispose()
$response.Close()
}
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Output "Failed! $ErrorMessage with $FailedItem"
}
}
}
$qualityEncoder = [System.Drawing.Imaging.Encoder]::Quality
$encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)
# Set JPEG quality level here: 0 - 100 (inclusive bounds)
$encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($qualityEncoder, 90)
$jpegCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | where {$_.MimeType -eq 'image/jpeg'}
$image.save(($outpath + $outfile), $jpegCodecInfo, $encoderParams)
$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 ($outpath + $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
$setwallpapersource = @"
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 $setwallpapersource
[wallpaper]::SetWallpaper(($outpath + $outfile))
Write-Output "Done"
@FriesischScott
Copy link

Depending on your system locale you might have to set the locale to get the correct date representation:

$locale = New-Object System.Globalization.CultureInfo("en-US");
$timeString = $now.ToString("yyyy/MM/dd/HHmmss", $locale)

In my case the German locale superceeded the format string and used '.' instead of '/'.

@DieGardine
Copy link

@FriesischScott

Wie hast du es zum Laufen bekommen?

@jdi-apt
Copy link

jdi-apt commented Jul 10, 2017

@DieGardine

Genau wie es FriesischScott beschrieben hat.

$locale = New-Object System.Globalization.CultureInfo("en-US");
$timeString = $now.ToString("yyyy/MM/dd/HHmmss", $locale)

Hinter Zeile 22 einfügen und Script ausführen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment