最近OpenAIのChatGPTや、Google Geminiなどが流行っているが、使いどころによっては結構便利である。
例えば何らかのスクリプトを作らせたいときには、こんなことをしたい、というと作ってくれる。
数時間測定しっぱなしの測定装置のスクリーンショットをとりまくるとかをPowerShellで作ってくれとか、
「
Windows10のpowershellで、スクリーンショットを10秒ごとに保存したいです。
C:\temp\screenshots\
というフォルダに、yyyymmddhhmmss.pngという形式で保存するスクリプトを作ってください。
」
と投げればある程度作ってくれる。Geminiはこんなアウトプットをしてくくれる。
# スクリーンショットの保存先フォルダ
$outputDir = "C:\temp\screenshots"
# フォルダが存在しない場合は作成する
if (!(Test-Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir | Out-Null
}
# 10秒ごとにスクリーンショットを保存するループ
while ($true) {
# ファイル名生成 (yyyymmddhhmmss.png 形式)
$fileName = (Get-Date -Format yyyymmddhhmmss) + ".png"
$filePath = Join-Path -Path $outputDir -ChildPath $fileName
# スクリーンショット取得
$screen = [System.Windows.Forms.Screen]::PrimaryScreen
$bitmap = New-Object System.Drawing.Bitmap -ArgumentList $screen.Bounds.Width, $screen.Bounds.Height
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.CopyFromScreen(0, 0, 0, 0, $screen.Bounds.Size)
# スクリーンショット保存
$bitmap.Save($filePath, [System.Drawing.Imaging.ImageFormat]::Png)
# 10秒待機
Start-Sleep -Seconds 10
}
いま、Ubuntuなので、動くかわからないけどw
Ubuntuの場合、左右2画面にしておいて、スクリーンショットをとって左右分割してそれぞれの画像にとかも結構かんたんに作ってくれる。
#!/bin/sh
# 一時ディレクトリを作成
mkdir a
mkdir b
# mogrifyコマンドを実行
for image in *.png; do
width=$(convert "$image" -format "%w" info:)
height=$(convert "$image" -format "%h" info:)
left_width=$((width / 2))
right_width=$((width - left_width))
# ファイル名、幅、高さを出力
echo "Image: $image, Width: $width, Height: $height"
mogrify -crop "${left_width}x${height}+0+0" -path 'b' "$image"
mogrify -crop "${right_width}x${height}+${left_width}+0" -path 'a' "$image"
done
# ファイル名変更
rename 's/.jpg/a.png/' a/*.png
rename 's/.jpg/b.png/' b/*.png
ちょっとこんなことを自動化したいな、といったことについては結構かんたんにやってくれるので、便利である。