PowerShell中进行图像格式转换
2008-07-25 22:02
1006 查看
挖坟是不太好的事情, 不过从PowerShell团队上看到下面的代码, 非常好. 这段代码是由: James W. Truher[MS] 编写. 将其存储成ps1文件.
执行:
PS C:/Documents and Settings/Administrator> ./Convert-Image.ps1 -inFile 01.jpg
GAC Version Location
--- ------- --------
True v2.0.50727 C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
LastWriteTime : 2008-7-25 22:05:36
Length : 135580
Name : 01.gif
代码如下:
# Convert one graphic image to another
param ( $inFile, $type = "gif", $outFile, [switch]$force )
[reflection.assembly]::LoadWithPartialName("System.drawing")
#
# 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
执行:
PS C:/Documents and Settings/Administrator> ./Convert-Image.ps1 -inFile 01.jpg
GAC Version Location
--- ------- --------
True v2.0.50727 C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
LastWriteTime : 2008-7-25 22:05:36
Length : 135580
Name : 01.gif
代码如下:
# Convert one graphic image to another
param ( $inFile, $type = "gif", $outFile, [switch]$force )
[reflection.assembly]::LoadWithPartialName("System.drawing")
#
# 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
相关文章推荐
- 使用ffmpeg进行图像格式转换以及图像缩放/sws_scale/linux/c++/c/rgb-yuv420
- Control study -> 图像格式转换 如:将*.jpg -> *.gif进行转换
- Control study -> 图像格式转换 如:将*.jpg -> *.gif进行转换
- 使用ffmpeg进行图像格式转换以及图像缩放/sws_scale/linux/c++/c/rgb-yuv420
- 使用ffmpeg进行图像格式转换以及图像缩放/sws_scale/linux/c++/c/rgb-yuv420
- 使用ffmpeg进行图像格式转换以及图像缩放/sws_scale/linux/c++/c/rgb-yuv420
- Control study -> 图像格式转换 如:将*.jpg -> *.gif进行转换
- Linux下使用RecordMyDesktop进行屏幕录像 输出图像格式转换
- Python中判断图片格式并进行转换,将转换的图像保存到生成的文件夹中
- 图像视频编码和FFmpeg(3)-----用FFmpeg进行图像格式转换和AVFrame简介
- 对索引像素格式的图片进行Setpixel(具有索引像素格式的图像不支持SetPixel)解决方案
- [转]caffe下为图像加标签,转换为可执行文件.lmdb格式
- GDI+图像格式转换功能
- opencv 3.0 图像格式转换 cvtColor
- RGB与YUV图像视频格式的相互转换
- 使用base64 对图像进行 转换的小程序。附上对视频进行截图的功能程序。
- fmri数据分析图像格式及转换问题——基于spm讨论
- SQL 将非标准日期格式转换成标准格式,进行条件判断
- 嵌入式专题: S5PV210: 用FIMC进行图像转换
- 使用openssl进行证书格式转换