Freigeben über


Image Conversion

 

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

Comments

  • Anonymous
    October 20, 2005
    Wow! That looks awful!

    NetPBM and a shell script can do this type of thing in three lines of code:

    for f in *.gif; do
    giftopnm $f | pnmtopng > ${f%.gif}.png
    done

    It's not that much more difficult using WinNT batch language and the same toolset. The script you wrote might as well be C++ or C#.

    (I like the idea of Monad; I tried to come up with something more positive to say, but really can't.)
  • Anonymous
    October 21, 2005
    Nice script,

    (not mentioned on posting) you have to load the DLL first :

    [reflection.assembly]::LoadWithPartialName("System.drawing")

    gr //o//
  • Anonymous
    October 21, 2005
    yah - it's not that pretty, but if you had those two tools as windows executables (I didn't), the script would be:
    foreach( $f in ls *.gif -name )
    {
    giftopnm $f|pnmtopng >($f -replace ".gif",".png")
    }

    which is pretty close
  • Anonymous
    October 22, 2005
    The really important thing to notice about this script is that it effectively implements the giftopnm utility in script. It loads a library designed to be used by C# (or whatever) and just uses it. You don't have to create a separate C# program, compile it then use that executable from the script. It's all done in the script itself. This illustrates an important principle in Monad - progressive capability. Simple things are simple (e.g. calling a utility program) and complex things are possible (like directly using a .NET library).

    -bruce

    Bruce Payette [MSFT]
  • Anonymous
    October 23, 2005
    Yeah, that makes a lot more sense.

    Re: Bruce's post, that will be a nice capability to have...
  • Anonymous
    October 29, 2005
    I really like Monad, but would it be possible to use a normal Windows frame with the ability to maximize to full screen?
  • Anonymous
    May 06, 2006
    PolyView® is a fast and powerful image viewer, conversion, and printing utility for 32 bit Windows (95/98/ME/NT/2000/XP). PolyView® is designed to be easy and intuitive to use for beginners, but has the advanced image manipulation, printing, and management features needed for dealing with large image collections.

    http://www.yaodownload.com/video-design/imageviewers/polyview_imageviewers.htm
  • Anonymous
    May 08, 2006
    The comment has been removed