Image Conversion

Image Conversion

Rate This
  • Comments 2
 
I needed to convert some images from bmp to gif for a web site I've been working on.  I figured that I could script this pretty easy - here's what I came up with.  It's a little more general than I needed, and I thought it might be useful to others.  Anyway, it's useful to me. 
 
 
# Convert one graphic image to another
param ( $inFile, $type = "gif", $outFile, [switch]$force )
#
# First check to see if our input file exists
$iFile = (resolve-path $inFile -ea silentlycontinue ).path
if ( ! $iFile ) { "File '$inFile' not found, exiting" ; exit }
 
# now check to see if the output file exists, if force
# we will continue, otherwise we exit
if ( ! $outFile )
{
    $tFile = get-item (resolve-path $inFile)
    $outFile = $tFile.Fullname -replace ($tFile.Extension + "`$"),".$type"
}
if ( (test-path $outFile) -and (! $force) ) { "File '$outFile' exists, exiting"; exit }
 
# make sure we have an encoder before changing anything on
# the filesystem
$codecs = [drawing.imaging.ImageCodecInfo]::GetImageEncoders() |
        foreach { $h = @{} } { $h.($_.formatdescription) = $_ } { $h }
$encoder = $codecs.$type
if ( ! $encoder ) 
{
        "No encoder of type '$type', exiting";
        "Available encodings are: " + [string]::Join(", ", $h.keys)
        exit
}
 
# This hoop is needed because resolve-path needs
# the file to actually exist.  We shouldn't get here
# unless the file doesn't exist, or we're going to remove it
# by force.
if ( test-path $outFile ) { remove-item $outFile }
[void](new-item -type file $outFile)
$outFile = (resolve-path $outFile).path
remove-item $outFile
 
# read the image
$image = [system.drawing.image]::FromFile($iFile)
$image.Save($outFile, $encoder.FormatId)
$image.Dispose()
# Get the file we just created
get-item $outFile
 
Leave a Comment
  • Please add 1 and 6 and type the answer here:
  • Post
  • I have a problem with [system.drawing.image]::FromFile

    I loaded the drawing namespace via [system.reflection.assembly], because it still reported errors otherwise. However when the class Image was finally available, it didn't work.

    It works for bitmap images, but not for icons, nor the JPEGs throwing this:

    Exception calling "FromFile" with "1" argument(s): "mainicon.ico"

    At line:1 char:40

    + $img = [system.drawing.image]::FromFile( <<<< "mainicon.ico")

    Or this:

    Exception calling "FromFile" with "1" argument(s): "35-souboj.jpg"

    At line:1 char:40

    + $img = [system.drawing.image]::FromFile( <<<< "35-souboj.jpg")

    Any idea why? (It's issue only in PowerShell, in C# compiled application it works perfectly.)

    Thank you!

  • Hey, Pepa - I know I'm a little late on this, but this was the only result I found on the Interwebs about this issue.

    Turns out that this method required me to use the full path to a file for it to work - I.E.: $file = [System.Drawing.Image]::FromFile( "F:\Media Folder\Main Pictures Folder\1256705287365.jpg")

    Made me feel like a moron once I used a full path instead of a relative one. Hope this helps others that may stumble into this.

Page 1 of 1 (2 items)