从PowerShell中的.png文件获取详细信息
如何从PowerShell中的特定.png文件中获取详细信息?从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 =
例如尺寸、位深度和大小。您可以从以下文件扩展属性中获取大部分信息:
$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)
您还可以通过其他方式获取大小,例如(获取项目D:\image.png)。长度/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
该属性是可能格式的枚举。您可以在此处查看完整列表:
例如,Format32bppArgb
被定义为
指定格式为每像素32位;每个使用8位
用于alpha、红色、绿色和蓝色组件
编写脚本的家伙写了一篇关于基于Shell.Application获取文件元数据的文章
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
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
使用.net framework加载图像并获取宽度的另一种方法:
Add-Type -AssemblyName System.Drawing
$image = [System.Drawing.Image]::FromFile('C:\image.png')
$image.Width
谢谢你的这篇文章,非常有趣。在我的例子中,我更喜欢使用Wia.ImageFile,它更简单,更适合我的需要。谢谢你的回答。@Etienne Wia.ImageFile是Com谢谢你的回答。我最终选择使用Wia.ImageFile。通过它,我可以更简单地获得一些信息(比如比特深度)。@Etienne我添加了另一个选项。您可以使用.NET并避免使用COM。感谢您上次的更新。我是PowerShell的初学者,所以如果我了解,最好使用.NET而不是COM?通过Wia.ImageFile,我还找到了IsIndexedPixelFormat、IsAlphaPixelFormat、IsExtendedPixelFormat和IsAnimated,它们对我的需求很有意思。我还没有用System.Drawing找到它们。非常感谢您的回答,非常感谢。Etienne.@Etienne您可以使用COM,但有些COM对象是随特定应用程序安装的,有些仅在特定版本的Windows中可用,因此在分发脚本时必须注意这些对象。未找到链接,有其他选择吗?没有正式链接,但可能会有所帮助