Jump to content
Sign in to follow this  
Steveiwonder

Getting image size

Recommended Posts

Steveiwonder

Hey All,

How can i get Height / Width of an image in pixels?

Searched and found this post:

http://www.autoitscript.com/forum/index.php?showtopic=28746

Which led me too this post:

http://www.autoitscript.com/forum/index.php?showtopic=25859

How ever that function doesnt seem to work:

#include <ExtProp.au3>

$width = _GetExtProperty("C:\Users\Steveiwonder\Pictures\Untitled.png", 27)
$height = _GetExtProperty("C:\Users\Steveiwonder\Pictures\Untitled.png", 28)

ConsoleWrite($width & @CRLF)
ConsoleWrite($height & @CRLF)

Output this:

>"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Users\Steveiwonder\Desktop\New AutoIt v3 Script.au3"    
0
0
>Exit code: 0    Time: 0.337

Looking at the post it was last edited @ "This post has been edited by Simucal: 27 July 2006 - 06:55 PM " So im getting its not up to date.

Is there any better way to get these properties of an image file? I've had a look at the help file but not getting anywhere.

Appreciate any help.

Thanks.

Steve.

Edited by Steveiwonder

They call me MrRegExpMan

Share this post


Link to post
Share on other sites
Mat

#Include <GDIPlus.au3>

_GDIPlus_Startup ()

Local $hImage = _GDIPlus_ImageLoadFromFile("C:\Users\Steveiwonder\Pictures\Untitled.png")
If @error Then
    MsgBox(16, "Error", "Does the file exist?")
    Exit 1
EndIf

ConsoleWrite(_GDIPlus_ImageGetWidth($hImage) & @CRLF)
ConsoleWrite(_GDIPlus_ImageGetHeight($hImage) & @CRLF)

_GDIPlus_ImageDispose ($hImage)

_GDIPlus_ShutDown ()

Edited by Mat

Share this post


Link to post
Share on other sites
Melba23

Steveiwonder,

This code works for me:

$sPassed_File_Name = "Fred.PNG"

Local $sDir_Name = StringRegExpReplace($sPassed_File_Name, "(^.*\\)(.*)", "\1")
Local $sFile_Name = StringRegExpReplace($sPassed_File_Name, "^.*\\", "")
Local $sDOS_Dir = FileGetShortName($sDir_Name, 1)

Local $oShellApp = ObjCreate("shell.application")
If IsObj($oShellApp) Then
    Local $oDir = $oShellApp.NameSpace($sDOS_Dir)
    If IsObj($oDir) Then
        Local $oFile = $oDir.Parsename($sFile_Name)
        If IsObj($oFile) Then

            ConsoleWrite($oDir.GetDetailsOf($oFile, 31) & @CRLF)

        Else
            $iError = 3
        EndIf
    Else
        $iError = 2
    EndIf
Else
    $iError = 1
EndIf

It gives me ?567 x 282? as an output - needs a bit of formatting, but it is correct. The value 31 should work in Vista and Win 7 - in XP use 26.

I hope this helps. :D

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
wakillon

$sPassed_File_Name = 'D:\Wallpapers\NEW\minhkoi.net-813-06.jpg'
Local $sDir_Name = StringRegExpReplace($sPassed_File_Name, "(^.*\\)(.*)", "\1")
Local $sFile_Name = StringRegExpReplace($sPassed_File_Name, "^.*\\", "")
Local $sDOS_Dir = FileGetShortName($sDir_Name, 1)
Local $oShellApp = ObjCreate ( "shell.application" )

If IsObj ( $oShellApp ) Then
    Local $oDir = $oShellApp.NameSpace ( $sDOS_Dir )
    If IsObj ( $oDir ) Then
        Local $oFile = $oDir.Parsename ( $sFile_Name )
        If IsObj ( $oFile ) Then
            For $_I = 1 To 35
            $_Details = $oDir.GetDetailsOf ( $oFile, $_I )
            If $_Details <> '' Then ConsoleWrite ( $_I & ' : ' & $_Details & @CRLF)
            Next
        Else
            $iError = 3
        EndIf
    Else
        $iError = 2
    EndIf
Else
    $iError = 1
EndIf

On XP It give this

1 : 334 KB

2 : Image JPEG

3 : 15/03/2007 13:24

4 : 23/06/2010 20:32

5 : 15/07/2010 22:47

6 : A

7 : Connecté

8 : 334 KB

12 : S-1-5-21-725345543-362288127-1177238915-500

17 : 1

30 : 1680 x 1050

31 : 1680 pixels

32 : 1050 pixels

35 : 2007:03:15 13:24:41

Now I know why I obtain different result than bovanshi on his post ! Posted Image

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
KaFu
Malkey

The three results that differ having run wakillon's script on my XP are:-

26 : w x h

27 : w pixels

28 : h pixels

wakillon's results on XP:

30 : w x h

31 : w pixels

32 : h pixels

Melba23's equivalent result on Vista and Win 7 (8 months ago):

31 : w x h

Melba23's equivalent result on XP:

26 : w x h (same as mine)

It appears this "object.GetDetailsOf()" method to obtain an image's size is subject to error.

Test this _ImageSize() function. It is a stand alone function using gdiplus dllcalls, and works fine on my XP.

;
Local $FullFileName = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir") & "\Examples\GUI\logo4.gif"
;Local $FullFileName = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir") & "\Examples\GUI\sampleAVI.avi"

Local $a = _ImageSize($FullFileName)

If @error Then
    MsgBox(0, "Results", '"' & $FullFileName & '" file does not exist, or is not a valid image file.')
Else
    MsgBox(0, "Results", '"' & $FullFileName & '" - Size: ' & $a[0] & " x " & $a[1])
EndIf

; Returns Array[0] - width of image.
;   Array[1] - heigth of image.
Func _ImageSize($sFileName)
    Local $aRet[2], $sExt = StringRegExpReplace($sFileName, "^.*\.", "")
    If (FileExists($sFileName) = 0) Or _
            ((StringLen($sExt) = 3) And (StringRegExp($sExt, "(?i)(bmp|gif|jpg|png|tif|emf|wmf)") = 0)) Or _
            ((StringLen($sExt) = 4) And (StringRegExp($sExt, "(?i)(tiff|jpeg)") = 0)) Then _
            Return SetError(1, 0, $aRet)
    Local $ghGDIPDll = DllOpen("GDIPlus.dll")
    Local $tInput = DllStructCreate("int Version;ptr Callback;int NoThread;int NoCodecs")
    Local $tToken = DllStructCreate("ulong_ptr Data")
    DllStructSetData($tInput, "Version", 1)
    DllCall($ghGDIPDll, "int", "GdiplusStartup", "ptr", DllStructGetPtr($tToken), "ptr", DllStructGetPtr($tInput), "ptr", 0)
    Local $aImage = DllCall($ghGDIPDll, "int", "GdipLoadImageFromFile", "wstr", $sFileName, "ptr*", 0)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipGetImageWidth", "handle", $aImage[2], "uint*", -1)
    $aRet[0] = $aResult[2]
    $aResult = DllCall($ghGDIPDll, "int", "GdipGetImageHeight", "handle", $aImage[2], "uint*", 0)
    $aRet[1] = $aResult[2]
    DllCall($ghGDIPDll, "int", "GdipDisposeImage", "handle", $aImage[2])
    DllCall($ghGDIPDll, "none", "GdiplusShutdown", "ptr", DllStructGetData($tToken, "Data"))
    DllClose($ghGDIPDll)
    Return SetError(0, 0, $aRet)
EndFunc ;==>_ImageSize

Share this post


Link to post
Share on other sites
wakillon

This one by Lazycat works fine too using winapi dllcalls.

Thanks KaFu and Malkey your 2 functions works fine ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.