jeudi 10 octobre 2013

ImageMagick et PowerShell

Voici la fin d'un périple d'automatisation de capture d'image pour notre catalogue :-) ...

Donc, après les découvertes suivantes:
  • la capture d'image automatisée sur un Nikon D5000 et 
  • la découverte d'ImageMagick (voir les deux posts précédents)
Voici un script PowerShell qui:
  • Réduit automatiquement l'image en 3 tailles différentes (600x600, 200x200, 100x100)... 
  • Sélectionne la partie centrale (pour avoir un beau carré)
  • Dégrade la qualité à 93% (le meilleur rapport entre taille et qualité)
prêtes à être intégrées dans notre base de donnée PostGreSql:-)
Le reste se fera en clipper compilé à l'aide d'Harbour Project (compilateur Clipper libre)


# ----------------------------------------
# Configuration
$srcfolder = "C:\Stock\Input\CatPhoto.in"
$destfolder = "C:\Stock\Input\CatPhoto.in"
$im_convert_exe = "C:\Program Files (x86)\ImageMagick-6.8.7-Q16\convert.exe"
$src_filter = "pict_*.jpg"
$dest_ext = "jpg"
# Image source = 4282 x 2848, target = ? x 600 (to be cropped at 600x600) 
# Width=903 <-- data-blogger-escaped-final="" data-blogger-escaped-height="600" data-blogger-escaped-source=""> ratio = 4.746 => Width for Height = 600 => 4282 / 4.746 => 903px
# Intermediate image would be resized to 903x600 BEFORE croping to 600x600
$optionsCatImage = "-resize 903 -crop 600x600+151+0 -quality 93" 
$optionsCatThumb = "-resize 302 -crop 200x200+51+0 -quality 93"
$optionsCatMiniThumb  = "-resize 150 -crop 100x100+25+0 -quality 93"
$logfile = "c:\Stock\Input\CatPhoto.in\convert_image.txt"
# ----------------------------------------

$fp = New-Item -ItemType file $logfile -force
$count=0
foreach ($srcitem in $(Get-ChildItem $srcfolder -include $src_filter -recurse))
{
    $srcname = $srcitem.fullname

    # Construct the filename and filepath for the output
    $partial = $srcitem.FullName.Substring( $srcfolder.Length )
    $destCatname = $destfolder + $partial.replace( "pict_", "cat_" )
    $destThumbname = $destfolder + $partial.replace( "pict_", "thumb_" )
    $destMiniThumbname = $destfolder + $partial.replace( "pict_", "mini_" )
    $destCatname= [System.IO.Path]::ChangeExtension( $destCatname , $dest_ext )
    $destThumbname= [System.IO.Path]::ChangeExtension( $destThumbname , $dest_ext )
    $destMiniThumbname= [System.IO.Path]::ChangeExtension( $destMiniThumbname , $dest_ext )
    $destpath = [System.IO.Path]::GetDirectoryName( $destCatname )

    # Create the destination path if it does not exist
    # if (-not (test-path $destpath))
    # {
    #     New-Item $destpath -type directory | Out-Null
    # }

    #--- CAT IMAGE ---
    # Perform the conversion by calling an external tool
    $cmdline =  "& `""+$im_convert_exe+"`""+ " `"" + $srcname  + "`" " + $optionsCatImage + " `"" + $destCatname + "`" "
    echo $cmdline
    invoke-expression -command $cmdline    

    # Get information about the output file    
    $destitem = Get-item $destCatname

    # Show and record information comparing the input and output files
    $info = [string]::Format( "{0} `t {1} `t {2} `t {3} `t {4} `t {5}", $count, 
 $partial, $srcname, $destCatname, $srcitem.Length ,  $destitem.Length)
    echo $info
    Add-Content $fp $info

    $count=$count+1

    #--- CAT THUMB ---
    # Perform the conversion by calling an external tool
    $cmdline =  "& `""+$im_convert_exe+"`""+ " `"" + $srcname  + "`" " + $optionsCatThumb + " `"" + $destThumbname + "`" "
    echo $cmdline
    invoke-expression -command $cmdline    

    # Get information about the output file    
    $destitem = Get-item $destThumbname

    # Show and record information comparing the input and output files
    $info = [string]::Format( "{0} `t {1} `t {2} `t {3} `t {4} `t {5}", $count, 
 $partial, $srcname, $destThumbname, $srcitem.Length ,  $destitem.Length)
    echo $info
    Add-Content $fp $info

    $count=$count+1

    #--- CAT MINI THUMB ---
    # Perform the conversion by calling an external tool
    $cmdline =  "& `""+$im_convert_exe+"`""+ " `"" + $srcname  + "`" " + $optionsCatMiniThumb + " `"" + $destMiniThumbname + "`" "
    echo $cmdline
    invoke-expression -command $cmdline    

    # Get information about the output file    
    $destitem = Get-item $destMiniThumbname

    # Show and record information comparing the input and output files
    $info = [string]::Format( "{0} `t {1} `t {2} `t {3} `t {4} `t {5}", $count, 
 $partial, $srcname, $destMiniThumbname, $srcitem.Length ,  $destitem.Length)
    echo $info
    Add-Content $fp $info

    $count=$count+1
    
}

Aucun commentaire: