hayate

一个转换图片格式的script

也是在monad的 team blog上看到的

原code 如下
# 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 

但是有一点问题 就是使用前必须[reflection.assembly]::LoadWithPartialName("System.drawing"),加在脚本里也行
使用方法 ./script.msh xxxx.bmp [gif] [new.gif] [-force]
如果省略后面三个参数 默认就是用相同文件名 以gif生成,此外还支持TIFF, JPEG, PNG, BMP。-force是当生成文件存在时 强制覆盖

有人评论说,这种脚本太复杂了,一些命令行工具加上一个简单的批处理就够了。确实如此,但是要注意的是,这个的脚本的特点就是不需任何executable,它调用那些只有C#(或别的什么)才能使用的功能,无须编译。
monad team的Bruce Payette说: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). 这显示了monad的一个重要原则,自我改进的能力。简单的事情固然简单(比如,monad也可以调用可执行文件),复杂的事情也变得可能(直接使用.net类库)。

分类:

技术点:

相关文章:

  • 2022-01-09
  • 2022-01-08
  • 2022-12-23
  • 2021-07-13
猜你喜欢
  • 2021-10-25
  • 2021-11-01
  • 2021-09-19
  • 2021-12-16
  • 2021-11-10
  • 2021-12-19
相关资源
相似解决方案