0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

png2jpg.ps1

Posted at

1. スクリプト概要

スクリプト名: png2jpg.ps1
指定したフォルダ内のPNGファイルをJPGファイルに一括変換します。

2. 処理と目的

処理の流れ
 1. PNGファイルが存在するフォルダをフルパスで指定します
 2. 指定されたフォルダ内からPNGファイルを検索します
 3. PNGファイルを読み込みます
 4. 読み込んだPNGファイルをJPG形式で保存します。
 5. 手順3~4をすべてのPNGファイルに対して繰り返し実行します

目的
 PNGファイルはJPGファイルと比較してファイルサイズが大きいため、本スクリプトにより一括変換することでストレージ容量の節約するため

3. 動作環境と要件

PowerShellのバージョン
7.0以上

OS
Windows10

必要なモジュール
特になし

必要な権限
特になし

その他の設定
特になし

4. 使用方法

基本的な実行方法
スクリプトコードを拡張子ps1で保存してPowershellで実行してください。
ファイルを保存する際は、文字コードをUTF8 BOM付にしてください。

パラメータ
なし

使用例

  1. コマンドラインでpwsh png2jpg.ps1を実行します
  2. スクリプトが変換対象フォルダを問い合わせるので、対象フォルダのフルパスを入力します

5. スクリプトコード

$host.UI.RawUI.WindowTitle = ([IO.Path]::GetFilenameWithoutExtension($PSCommandPath))
$ErrorActionPreference = 'Stop'

echo "------------------------------------------------------------------"
Write-Host "対象フォルダ内のpngファイルをjpgファイルに置き換えます" -ForegroundColor White -BackGroundColor Black
echo "------------------------------------------------------------------"

$pngFiles = $null
do {
    $selectDir = Read-Host "対象のフォルダ"
    $selectDir = $selectDir.trim('"')

    if(Test-Path -literal $selectDir){
      Write-Progress -Activity "searching..." -Status $selectDir
      $pngFiles = ls -literal $selectDir -Recurse -Filter '*.png'
      Write-Progress -Completed
      if(($pngFiles|Measure).count -eq 0){
        echo "対象のフォルダ内にpngファイルがありません"
      }
    }
} while( ($pngFiles|Measure).count -eq 0)

Write-Host "$(($pngFiles|Measure).count)個のpngファイルを対象フォルダ内に確認しました。全て変換しますか?"
pause

[void][Reflection.Assembly]::LoadWithPartialName("System.Drawing")

#保存形式と保存画質の初期設定
$quality = 100
$myEncoder=[System.Drawing.Imaging.Encoder]::Quality
$encoderParams=New-Object System.Drawing.Imaging.EncoderParameters(1)
$encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($myEncoder, $quality)
#画像保存形式jpgの読み込み
$myImageCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders()|?{$_.MimeType -eq 'image/jpeg'}

$pngFileCount = ($pngFiles |Measure).Count
$index = 0
foreach( $imgfile in $pngFiles){
  $index += 1
  Write-Progress -Activity "convert..." -Status "($($index)/$($pngFileCount))$($imgfile.fullname)" -PercentComplete ($index/$pngFileCount*100)

  $jpgDirPath = $imgfile.DirectoryName
  if(-not (Test-Path -literal $jpgDirPath)){
    $null = mkdir $jpgDirPath
  }


  $jpgFilepath = Join-Path $jpgDirPath ($imgfile.baseName+'.jpg')
    
  if(-not (Test-Path -literal $jpgFilepath)){
    $bmpImage = [System.Drawing.Image]::FromFile($imgfile.FullName)
    $bmpImage.Save($jpgFilepath, $myImageCodecInfo,$encoderParams)
    $bmpImage.Dispose()
    
    if((Test-Path -literal $jpgFilepath)){
      remove-Item -literal $imgfile.FullName
    }
  }
  
  #他の処理の邪魔をしないように休止を挟む
  Start-Sleep -Milliseconds 150

}
Write-Progress -Completed

pause

6. 注意事項と既知の問題

制約事項
大量のファイルやサイズの大きいファイルを処理する場合、完了までに時間がかかる可能性があります。

既知のバグ
もしバグを発見された場合は、コメントでご報告ください。

トラブルシューティング
・ps1ファイルのエンコーディングには注意してください。
・Powershell7未満では動きません

7. 免責事項

本スクリプトにはいかなる保証もありません。使用は自己責任で行ってください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?