Archive

Articles taggués ‘Images’

Réduire la taille de toutes les images d’un dossier.

illex

J’ai trouvé un script très utile permettant de réduire la taille de toutes les images contenu dans un dossier.
Ça marche nickel.

?View Code POWERSHELL
[reflection.assembly]::LoadWithPartialName("System.Drawing")
$toresize = "..\images"
$SizeLimit=1024          # required size of picture's long side
$logfile="resizelog.txt" # log file for errors
 
Get-ChildItem -recurse $toresize -include *.jpg  | foreach {
$parent = $_.Directory.name
$backupdir = ".\backup\" + $parent + "\"
If (-not (Test-Path $backupdir)) { New-Item -ItemType Directory -Name $backupdir }
$backup = $backupdir + "\" + $_.Name
Copy-Item -Path $_.fullname -Destination $backup
 
  $OldBitmap = new-object System.Drawing.Bitmap $_.FullName # open found jpg
  if ($error.count -ne 0) {                                 # error handling
    $error | out-file $logfile -append -encoding default
    $error[($error.count-1)].TargetObject | out-file $logfile -append -encoding default
    echo $_>>$logfile
    $error.clear()
  }
  $LongSide=$OldBitmap.Width                                # locating long side of picture
  if ($OldBitmap.Width -lt $OldBitmap.Height) { $LongSide=$OldBitmap.Height }
  if ($LongSide -gt $SizeLimit) {                           # if long side is greater than our limit, process jpg
    if ($OldBitmap.Width -lt $OldBitmap.Height) {           # calculate dimensions of picture resize to
      $newH=$SizeLimit
      $newW=[int]($OldBitmap.Width*$SizeLimit/$OldBitmap.Height)
    } else {
      $newW=$SizeLimit
      $newH=[int]($OldBitmap.Height*$newW/$OldBitmap.Width)
    }
    $NewBitmap = new-object System.Drawing.Bitmap $newW,$newH # create new bitmap
    $g=[System.Drawing.Graphics]::FromImage($NewBitmap)
    $g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic # use high quality resize algorythm
    $g.DrawImage($OldBitmap, 0, 0, $newW, $newH)              # resize our picture
 
    $name=$_.DirectoryName+"\"+$_.name+".new"                 # generating name for new picture
    $NewBitmap.Save($name, ([system.drawing.imaging.imageformat]::jpeg)) # save newly created resized jpg
    $NewBitmap.Dispose()                                      # cleaning up our mess
    $OldBitmap.Dispose()
    $n=get-childitem $name
    if ($n.length -ge $_.length) {                            # if resized jpg has greater size than original
      Write-host -NoNewLine "+"                               # draw "+"
      $n.delete()                                             # and delete it
    } else {                                                  # if resized jpg is smaller than original
      if ($n.Exists -and $_.Exists) {
        $name=$_.FullName
        $_.Delete()                                           # delete original
        $n.MoveTo($name)                                      # rename new file to original name (replace old file with new)
        echo ($Name + " " + $LongSide)                        # write its name for visual control
      }
    }
 
  } else {                                                    # if long side is smaller than limit, draw dot for visual
    Write-host -NoNewLine "."
    $OldBitmap.Dispose()
  }
}
Categories: Powershell Tags: ,

Supprimer les photos en portrait

Je n’aime pas le photos prise en portrait. A chacun ces goûts.
Voici le moyen que j’ai trouver pour toutes les supprimer d’un coup.

?View Code POWERSHELL
$image = New-Object -ComObject Wia.ImageFile
Get-ChildItem -Recurse -Include *.jpg -Path "..\images" | foreach {
$file = $_.fullname
$image.LoadFile($file)
if ( $image.Width -gt $image.Height) {
Remove-Item -Path $file -Force
}
}
Categories: Powershell Tags: ,