【发布时间】:2012-03-22 01:58:00
【问题描述】:
如何从 PowerShell 中的特定 .png 文件中获取详细信息?
像尺寸、位深度和大小。
【问题讨论】:
标签: powershell png
如何从 PowerShell 中的特定 .png 文件中获取详细信息?
像尺寸、位深度和大小。
【问题讨论】:
标签: powershell png
您可以从文件扩展属性中获取大部分信息,如下所示:
$path = 'D:\image.png'
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $path
$file = Split-Path $path -Leaf
$shellfolder = $shell.Namespace($folder)
$shellfile = $shellfolder.ParseName($file)
$width = 27
$height = 28
$Dimensions = 26
$size = 1
$shellfolder.GetDetailsOf($shellfile, $width)
$shellfolder.GetDetailsOf($shellfile, $height)
$shellfolder.GetDetailsOf($shellfile, $Dimensions)
$shellfolder.GetDetailsOf($shellfile, $size)
也可以通过(Get-Item D:\image.png).Length / 1KB等其他方式获取大小。
位深度属性似乎没有列在扩展属性中,尽管当您右键单击文件时它可用。
更新另一个选择是使用 .NET 来避免使用 COM:
add-type -AssemblyName System.Drawing
$png = New-Object System.Drawing.Bitmap 'D:\image.png'
$png.Height
$png.Width
$png.PhysicalDimension
$png.HorizontalResolution
$png.VerticalResolution
更新 2 PixelFormat 属性为您提供位深度。
$png.PixelFormat
该属性是可能格式的枚举。您可以查看完整列表here。
例如,Format32bppArgb 定义为
指定格式为每像素 32 位;每个使用 8 位 用于 alpha、红色、绿色和蓝色分量。
【讨论】:
System.Drawing.Bitmap 方法需要将整个图像加载到内存中(建议在使用后调用$png.Dispose())。从好的方面来说,它支持多种格式:.bmp、.gif、.exif、.jpg、.png和.tiff,而Shell.Application的索引COM 对象的.GetDetailsOf() 方法似乎(大部分)是.png-specific。
您可能想要使用包含 get-image 的 PowershellPack Module:
PS D:\> import-module psimagetools
PS D:\> get-item .\fig410.png | get-image
FullName : D:\fig410.png
FormatID : {B96B3CAF-0728-11D3-9D7B-0000F81EF32E}
FileExtension : png
FileData : System.__ComObject
ARGBData : System.__ComObject
Height : 450
Width : 700
HorizontalResolution : 96,0119934082031
VerticalResolution : 96,0119934082031
PixelDepth : 32
IsIndexedPixelFormat : False
IsAlphaPixelFormat : True
IsExtendedPixelFormat : False
IsAnimated : False
FrameCount : 1
ActiveFrame : 1
Properties : System.__ComObject
或者您可以通过这种方式直接使用 Wia.ImageFile(get-image 函数就是这样做的):
PS D:\> $image = New-Object -ComObject Wia.ImageFile
PS D:\> $image.loadfile("D:\fig410.png")
PS D:\> $image
FormatID : {B96B3CAF-0728-11D3-9D7B-0000F81EF32E}
FileExtension : png
FileData : System.__ComObject
ARGBData : System.__ComObject
Height : 450
Width : 700
HorizontalResolution : 96,0119934082031
VerticalResolution : 96,0119934082031
PixelDepth : 32
IsIndexedPixelFormat : False
IsAlphaPixelFormat : True
IsExtendedPixelFormat : False
IsAnimated : False
FrameCount : 1
ActiveFrame : 1
Properties : System.__ComObject
【讨论】:
脚本专家写了一篇关于基于 Shell.Application https://blogs.technet.microsoft.com/heyscriptingguy/2008/08/14/hey-scripting-guy-how-can-i-find-files-metadata/获取文件元数据的文章
【讨论】:
另一种使用.net框架加载图片然后获取宽度的方法:
Add-Type -AssemblyName System.Drawing
$image = [System.Drawing.Image]::FromFile('C:\image.png')
$image.Width
【讨论】: