Jump to content

any way to get an image's dimensions (pixels in X & Y direction) ?


Go to solution Solved by ioa747,

Recommended Posts

Posted

Is there any way to get an image's dimensions (pixels in X & Y direction) ?
I can see the dimensions using one of the Windows File Explorer, details view, extra columns.

My images are pretty much all the same size for a 'session', so this isn't too important.
I can always 'hard code' this into each instance of my script and just put the dimensions in the filename.

 

Posted
On 7/19/2024 at 9:53 PM, Andreik said:

Check in help file _GDIPlus_ImageGetDimension(). There is also an example.

Unfortunately this doesn't work with JP2 (jpeg2000) files.

Posted
On 7/20/2024 at 12:19 AM, Nine said:

Alternatively, you can use _WinAPI_GetBitmapDimension (see help file).  But _WinAPI_LoadImage is much more restrictive in terms of file types you can read.

Tried but output dimensions using the example image showed as 0,0 instead of 400,400. 

  • Solution
Posted (edited)

 

Local $aDim =  _GetImageDimensions("C:\Program Files (x86)\AutoIt3\Examples\GUI\logo_autoit_210x72.gif")
ConsoleWrite($aDim[0] & "x" & $aDim[1] & @CRLF)

Func _GetImageDimensions($sPath)
    Local $sFile = StringTrimLeft($sPath, StringInStr($sPath, "\", 0, -1))
    Local $sDir = StringTrimRight($sPath, (StringLen($sPath) - StringInStr($sPath, "\", 0, -1)))
    Local $oShellApp = ObjCreate("shell.application")
    Local $oDir = $oShellApp.NameSpace ($sDir)
    Local $oFile = $oDir.Parsename ($sFile)
    Local $aDimension[2]
    $aDimension[0] = $oFile.ExtendedProperty("System.Image.HorizontalSize")
    $aDimension[1] = $oFile.ExtendedProperty("System.Image.VerticalSize")
    Return $aDimension
EndFunc   ;==>_GetProperty


More:

Edited by ioa747

I know that I know nothing

  • 1 year later...
Posted (edited)
Local $aDim =  _GetJP2ImageDimensions(@ScriptDir & "\JP2Image.jp2")
ConsoleWrite($aDim[0] & "x" & $aDim[1] & @CRLF)

Func _GetJP2ImageDimensions($sImagePath)
    Local $aDimensions[2] = [0, 0]
    Local $hFile = FileOpen($sImagePath, 16)
    If $hFile = -1 Then Return SetError(1, 0, $aDimensions)
    Local $bHeader = FileRead($hFile, 512)
    FileClose($hFile)
    ; String length = Binary length * 2 + 1 (1025 = 512 * 2 + 1)
    Local $iPos = StringInStr($bHeader, "69686472", 0, 1, 25, 1025)
    If Not $iPos Then Return SetError(2, 0, $aDimensions) ; IHDR box not found
    Local $iBytePos = Int($iPos / 2) - 1
    If $iBytePos + 16 <= BinaryLen($bHeader) Then
        Local $aData[2] = [BinaryMid($bHeader, $iBytePos + 9, 4), BinaryMid($bHeader, $iBytePos + 5, 4)]
        ; Convert 4-byte big-endian to number
        For $i = 0 To 1
            For $j = 1 To 4
                $aDimensions[$i] = BitOR(BitShift($aDimensions[$i], -8), Dec(Hex(BinaryMid($aData[$i], $j, 1))))
            Next
        Next
    EndIf
    Return $aDimensions
EndFunc

 

Edited by WarMan

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...