如何确定 tiff 文件的图像尺寸?

发布于 2024-10-15 23:14:25 字数 158 浏览 8 评论 0原文

我有一个 vbscript 脚本,我需要仔细研究 .tif 文件的目录。对于每个文件,我需要确定该文件的比例是横向还是纵向。基本上,我需要能够获取每个文件的图像尺寸。因此,我见过一些人们读取文件文件头以从 jpg 或 bmp 文件中提取此类信息的示例。有人做过同样的事情来提取 tiff 文件的尺寸吗?

I have a vbscript script and I need to chew through a directory of .tif files. For each file, I need to determine if the file is proportioned as a landscape or a portrait. Basically, I need to be able to get the image dimensions for each file. So for, I have seen some examples of people reading the file file headers to extract this sort of information from jpg or bmp files. Has anyone done the same thing to extract the dimensions for a tiff file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2024-10-22 23:14:25

在 VBScript 中,您可以通过两种方式确定图像尺寸:

  1. 使用WIA 自动化库 (下载链接MSDN 文档一个出色的嘿,脚本家伙!< /em> 关于该主题的文章)。注册wiaaut.dll库后,您可以使用以下简单代码:

    Set oImage = CreateObject("WIA.ImageFile")
    oImage.LoadFile“C:\Images\MyImage.tif”
    
    WScript.Echo“宽度:”& oImage.Width & vbNewLine & _
                 “身高:”& o图像.高度
    

  2. 使用GetDetailsOf方法读取对应的扩展文件属性。这是本机 Windows 脚本方法,因此不需要外部库;但代码较长:

    常量维度 = 31
    常量宽度 = 162
    常量高度 = 164
    
    设置 oShell = CreateObject("Shell.Application")
    设置 oFolder = oShell.Namespace ("C:\Images")
    设置 oFile = oFolder.ParseName("MyImage.tif")
    
    strDimensions = oFolder.GetDetailsOf(oFile, DIMENSIONS)    
    strWidth = oFolder.GetDetailsOf(oFile, WIDTH)
    strHeigth = oFolder.GetDetailsOf(oFile, HEIGTH)
    
    WScript.Echo“尺寸:”& strDimensions & 尺寸vbNewLine & _
                 “宽度:”& str宽度& vbNewLine & _
                 “身高:”&结构高度
    

    该脚本输出如下内容:

    <块引用>

    尺寸:2464 x 3248
    宽度:2464 像素
    高度:3248 像素

    因此,如果您需要纯数字,则必须从返回的字符串中提取它们。

    此方法还有另一个问题 - 属性索引(脚本开头的那些常量)在不同的 Windows 版本中是不同的,正如我在 这个答案。上述脚本适用于 Windows 7,但如果您使用其他 Windows 版本或者希望该脚本在不同的 Windows 版本上运行,则需要使用特定于版本的索引。最完整的可用属性索引列表位于此处 .

In VBScript, you can determine the image dimensions in two ways:

  1. Using the WIA Automation Library (download link, MSDN documentation, an excellent Hey, Scripitng Guy! article on the subject). Once you have the wiaaut.dll library registered, you can use the following simple code:

    Set oImage = CreateObject("WIA.ImageFile")
    oImage.LoadFile "C:\Images\MyImage.tif"
    
    WScript.Echo "Width: "  & oImage.Width & vbNewLine & _
                 "Height: " & oImage.Height
    

  2. Using the GetDetailsOf method to read the corresponding extended file properties. This is a native Windows scripting method, so no external libraries are required; but the code is longer:

    Const DIMENSIONS = 31
    CONST WIDTH  = 162
    CONST HEIGTH = 164
    
    Set oShell  = CreateObject ("Shell.Application")
    Set oFolder = oShell.Namespace ("C:\Images")
    Set oFile   = oFolder.ParseName("MyImage.tif")
    
    strDimensions = oFolder.GetDetailsOf(oFile, DIMENSIONS)    
    strWidth  = oFolder.GetDetailsOf(oFile, WIDTH)
    strHeigth = oFolder.GetDetailsOf(oFile, HEIGTH)
    
    WScript.Echo "Dimensions: " & strDimensions & vbNewLine & _
                 "Width: "      & strWidth      & vbNewLine & _
                 "Height: "     & strHeigth
    

    This script outputs something like:

    Dimensions: 2464 x 3248
    Width: 2464 pixels
    Height: 3248 pixels

    so if you need plain numbers, you'll have to extract them from the returned strings.

    There's also another problem with this method - the property indexes (those constants in the beginning on the script) are different in different Windows versions, as I explained in this answer. The above script is for Windows 7, but if you use another Windows versions or if you want the script to work on different Windows versions, you'll need to use version-specific indexes. The most complete list of available property indexes is here.

自我难过 2024-10-22 23:14:25

我建议使用 Atalasoft 的 DotImage Photo它是免费的!但是,它是一个 .NET 包,因此您需要做一些 Regasm让它发挥作用的魔法。在开始之前,请先查看在 VBScript 中使用 vb.net

这是获取尺寸所需的代码。

Public Function GetHeight(path As String) As Integer

    Using stm As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim decoder As New TiffDecoder()
        If Not decoder.IsValidFormat(stm) Then
            Throw New Exception("not a TIFF")
        End If
        Dim image As AtalaImage = decoder.Read(stm, Nothing)
        Return image.Height
            ' Return image.Width --- To return the Width.

    End Using
End Function

I would recommend using Atalasoft's DotImage Photo Its powerful & it's free! But, it's a .NET package, so you'll have do some Regasm Magic to make it work. Go check out Use vb.net in VBScript before you get started.

Here is the code you'll need to get the dimensions.

Public Function GetHeight(path As String) As Integer

    Using stm As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim decoder As New TiffDecoder()
        If Not decoder.IsValidFormat(stm) Then
            Throw New Exception("not a TIFF")
        End If
        Dim image As AtalaImage = decoder.Read(stm, Nothing)
        Return image.Height
            ' Return image.Width --- To return the Width.

    End Using
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文