Copy all images in folder recursively and prefix them with their average red value (RGB)

using namespace System.Drawing
Add-Type -AssemblyName System.Drawing # Specify System.Drawing.Common on PS Core

$pathToFolder =  'C:\Users\Michael\Pictures\AllPictures\'
$pathToTargetFolder = $pathToFolder + 'prefixedPictures_' + $((Get-Date).ToString("yyyyMMdd_HHmmss"))
New-Item $pathToTargetFolder -ItemType Directory

$Files = @(Get-ChildItem -Path $pathToFolder -Filter *.jpg)
foreach ($i in $Files){
	# Copy action
	$fileName = $i.Basename
	$fileExtension = $i.Extension
	$fullFileName = $fileName + $fileExtension
	$fullPathOfFileInSourceFolder = $pathToFolder + $fullFileName
	$fullPathOfFileToTargetFolder = $pathToTargetFolder + '\' + $fullFileName
	Copy-Item $i.FullName -Destination $fullPathOfFileToTargetFolder
	
	# get average red value of picture
	$BitMap = [Bitmap]::FromFile((Resolve-Path $fullPathOfFileInSourceFolder).ProviderPath)

	$rotWerte = 0
	$anzahlPixel = 0
	foreach($h in 1..$BitMap.Height){
		foreach($w in 1..$BitMap.Width) {
			$rotWerte = $rotWerte + $BitMap.GetPixel($w - 1,$h - 1).R
			$anzahlPixel = $anzahlPixel + 1
		}
	}
	$rotDurchschnittsWert = $rotWerte / $anzahlPixel
	
	# rename files in target folder
	$newFullFileName = 'red' + $rotDurchschnittsWert + "__" + $fullFileName
	Rename-Item $fullPathOfFileToTargetFolder $newFullFileName
	echo 'new' + $newFullFileName + 'was created.'
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.