Sign in to follow this  
Followers 0
greggklein

finding width of a jpg

15 posts in this topic

How would one have autoit find the width and height of a local jpg file?

I am trying to have a GUI update based on that size

Thanks

Share this post


Link to post
Share on other sites



How would one have autoit find the width and height of a local jpg file?

I am trying to have a GUI update based on that size

Thanks

need <GUIPlus.au3>

Can't seem to work

Share this post


Link to post
Share on other sites

Hi, you can also use shell object to retrieve the image width/height.

Here's a crude example.

ConsoleWrite(_GetImageDimension(@WindowsDir & "\Web\Wallpaper\Ascent.jpg", "x") & @LF)
ConsoleWrite(_GetImageDimension(@WindowsDir & "\Web\Wallpaper\Ascent.jpg", "y") & @LF)

;-----------------------------------------------------------------
; Function Name: _GetImageDimension($iPath, $iDim)
; Parameters: $iPath = Path to file
;             $iDim = Dimension to get ("x" or "y")
; Returns: Success returns the requested Dimension in pixels.
;          Failure returns 0 and sets @error
;          @error 1 = Invalid file path parameter
;          @error 2 = Invalid dimension parameter
;          @error 3 = The requested dimension returned 0
;-----------------------------------------------------------------

Func _GetImageDimension($iPath, $iDim)
    Local $oShellApp, $oDir, $oFile,$sDim
    If Not FileExists($iPath) Then
        SetError(1)
        Return 0
    Else
        $oShellApp = ObjCreate ("shell.application")
        $oDir = $oShellApp.NameSpace (StringTrimRight($iPath, (StringLen($iPath) - StringInStr($iPath, "\", 0, -1))))
        $oFile = $oDir.Parsename (StringTrimLeft($iPath, StringInStr($iPath, "\", 0, -1)))
        If $iDim = "x" Then
            $sDim = $oDir.GetDetailsOf ($oFile, 30)
            If $sDim <> "" Then Return StringTrimRight($sDim, 7)
            SetError(3)
            Return 0
        ElseIf $iDim = "y" Then
            $sDim = $oDir.GetDetailsOf ($oFile, 31)
            If $sDim <> "" Then Return StringTrimRight($sDim, 7)
            SetError(3)
            Return 0
        Else
            SetError(2)
            Return 0
        EndIf   
    EndIf
EndFunc

Cheers

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You can download my Filex.au3 file from

my AutoIt website.

In the left side bar, Click >> Code >> My Extra UDFs >> Filex.au3. The function you want is _File_Ext_PropertiesGet(), At the bottom of the page there are the options to copy the code to the clipboard or to download the file. For your use call it like this

$iDim =  _File_Ext_PropertiesGet("path\myimage.jpg", "dimensions")
If NOT @Error Then 
   $aDim = StringSplit(StringStripWS($iDim, 8), "x")
   If IsArray($aDim) Then
      $Width = $aDim[1]
      $Height = $aDim[2]
   EndIf
Else
   MsgBox(0, "Error", $iDim)
EndIf

Edit: Code change

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

GEOSoft, that code doesn't work.

#include <Filex.au3>
$iDim =  _File_Ext_PropertiesGet(@DesktopDir & "\test.jpg", "dimensions")
If NOT @Error Then
   $aDim = StringSplit(StringStripWS($iDim, 8), "x")
   If IsArray($aDim) Then
      $Width = $aDim[1]
      $Height = $aDim[2]
   EndIf
Else
   MsgBox(0, "Error", $iDim)
EndIf

MsgBox(64, "Hmm", $Width & @CR & $Height)

Share this post


Link to post
Share on other sites

GEOSoft, that code doesn't work.

#include <Filex.au3>
$iDim =  _File_Ext_PropertiesGet(@DesktopDir & "\test.jpg", "dimensions")
If NOT @Error Then
   $aDim = StringSplit(StringStripWS($iDim, 8), "x")
   If IsArray($aDim) Then
      $Width = $aDim[1]
      $Height = $aDim[2]
   EndIf
Else
   MsgBox(0, "Error", $iDim)
EndIf

MsgBox(64, "Hmm", $Width & @CR & $Height)
It's working for me. I did have a File Doesn't Exist warning until I realized that the test.jpg file on my desktop was really a link to the file. I'll add some error checking in to handle that.

Here is the exact code I used and I will include a copy of the file.

#include <Filex.au3>
$File = @DesktopDir & "\test.jpg"
$iDim =  _File_Ext_PropertiesGet($File, "dimensions")

If NOT @Error Then
   $aDim = StringSplit(StringStripWS($iDim, 8), "x")
   If IsArray($aDim) Then
      $Width = $aDim[1]
      $Height = $aDim[2]
   EndIf
Else
   MsgBox(0, "Error", $iDim)
EndIf

MsgBox(64, "Hmm", $Width & @CR & $Height)

Just extract the file from test.zip to your desktop and try again.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Still won't work. I get an array error @ $Height = $aDim[2]

Then if I do Dim $aDim[3] I don't get the error but the message box remains blank.

Share this post


Link to post
Share on other sites

Still won't work. I get an array error @ $Height = $aDim[2]

Then if I do Dim $aDim[3] I don't get the error but the message box remains blank.

I sent my copy to you in case there is a file error in the download file. Please check it and let me know. I've used that function quite a bit and have never had an error from it except when I caused one with a bad file name etc. Run the func using "all" instead of "dimension" and let me know what you get.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I was searching and i saw this topic.

This is what i want:

Open a picture and if i press Zoom in 2x then must the pic be twice as big. ( like old 100*100 then new 200*200)

This is my script

#include <GUIConstants.au3>
Global $file
GUICreate("My GUI",250,200,100,100,$WS_POPUP)
$filemenu = GUICtrlCreateMenu ("&File")
$openitem = GUICtrlCreateMenuitem ("Open",$filemenu)
$exititem = GUICtrlCreateMenuitem ("Exit",$filemenu)
$pic = GUICtrlCreatePic("",0,20,0,0)
$zoom = GUICtrlCreateButton("Zoom in 2x",0,0,150,20)
GUISetState (@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    
    If $msg = $exititem Then ExitLoop
    If $msg = $zoom Then _zoomin()
    If $msg = $openitem Then _pic()
Wend

Func _zoomin()  
    $oldwidth = _GetImagePixel($file, "Width")
    $oldHeight = _GetImagePixel($file, "Height")
    $oldwidth = $newwidth * 2
    $oldHeight = $newHeight * 2
    GUICtrlSetPos($pic,0,0,$newwidth,$newHeight)
EndFunc

Func _pic()
    $file = FileOpenDialog("Picture",@ScriptDir,"All Pictures(*.jpg;*.bmp;*.gif)|JPG file (*.jpg)|BMP file (*.bmp)|GIF file (*.gif)|All files (*.*)")
    If @error Then
        Sleep(10)
    Else
        GUICtrlSetImage($pic,$file)
    EndIf
EndFunc

Edit:

i tried the script in this topic but don't work, _GDIPlus_ImageGetHeight works also not, then i get a msgbox with: Fatal Error AVector: []: out of bounds

Edited by UQOII

[center]uqoii.nl[/center]

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I was searching and i saw this topic.

This is what i want:

Open a picture and if i press Zoom in 2x then must the pic be twice as big. ( like old 100*100 then new 200*200)

This is my script

#include <GUIConstants.au3>
Global $file
GUICreate("My GUI",250,200,100,100,$WS_POPUP)
$filemenu = GUICtrlCreateMenu ("&File")
$openitem = GUICtrlCreateMenuitem ("Open",$filemenu)
$exititem = GUICtrlCreateMenuitem ("Exit",$filemenu)
$pic = GUICtrlCreatePic("",0,20,0,0)
$zoom = GUICtrlCreateButton("Zoom in 2x",0,0,150,20)
GUISetState (@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    
    If $msg = $exititem Then ExitLoop
    If $msg = $zoom Then _zoomin()
    If $msg = $openitem Then _pic()
Wend

Func _zoomin()  
    $oldwidth = _GetImagePixel($file, "Width")
    $oldHeight = _GetImagePixel($file, "Height")
    $oldwidth = $newwidth * 2
    $oldHeight = $newHeight * 2
    GUICtrlSetPos($pic,0,0,$newwidth,$newHeight)
EndFunc

Func _pic()
    $file = FileOpenDialog("Picture",@ScriptDir,"All Pictures(*.jpg;*.bmp;*.gif)|JPG file (*.jpg)|BMP file (*.bmp)|GIF file (*.gif)|All files (*.*)")
    If @error Then
        Sleep(10)
    Else
        GUICtrlSetImage($pic,$file)
    EndIf
EndFunc

Edit:

i tried the script in this topic but don't work, _GDIPlus_ImageGetHeight works also not, then i get a msgbox with: Fatal Error AVector: []: out of bounds

Just for starters this function is wrong

Func _zoomin()  
    $oldwidth = _GetImagePixel($file, "Width")
    $oldHeight = _GetImagePixel($file, "Height")
    $oldwidth = $newwidth * 2
    $oldHeight = $newHeight * 2
    GUICtrlSetPos($pic,0,0,$newwidth,$newHeight)
EndFunc

Should probably be

Func _zoomin()  
    $oldwidth = _GetImagePixel($file, "Width")
    $oldHeight = _GetImagePixel($file, "Height")
    $newwidth = $oldwidth * 2
    $newHeight = $oldHeight * 2
    GUICtrlSetPos($pic,0,0,$newwidth,$newHeight)
EndFunc

Also you are not calling your Pic() function.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Just for starters this function is wrong

Func _zoomin()  
    $oldwidth = _GetImagePixel($file, "Width")
    $oldHeight = _GetImagePixel($file, "Height")
    $oldwidth = $newwidth * 2
    $oldHeight = $newHeight * 2
    GUICtrlSetPos($pic,0,0,$newwidth,$newHeight)
EndFunc

Should probably be

Func _zoomin()  
    $oldwidth = _GetImagePixel($file, "Width")
    $oldHeight = _GetImagePixel($file, "Height")
    $newwidth = $oldwidth * 2
    $newHeight = $oldHeight * 2
    GUICtrlSetPos($pic,0,0,$newwidth,$newHeight)
EndFunc

Also you are not calling your Pic() function.

yupsz i was already so far to change that.

i must create _GetImagePixel() to get the width and height but i dont know how.

and that is why i need help, to create that func


[center]uqoii.nl[/center]

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Hi,

#include <GUIConstants.au3>
#include <GDIPlus.au3>
Global $file, $WH

$Gui = GUICreate("My GUI",250,200,0,0,$WS_POPUP)
$filemenu = GUICtrlCreateMenu ("&File")
$openitem = GUICtrlCreateMenuitem ("Open",$filemenu)
$exititem = GUICtrlCreateMenuitem ("Exit",$filemenu)
$pic = GUICtrlCreatePic("",0,20,0,0)
GUICtrlSetState(-1, $GUI_DISABLE)
$zoom = GUICtrlCreateButton("Zoom in 2x",0,0,150,20)
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState (@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE, $exititem
            Exit
        Case $zoom
            _zoomin()
        Case $openitem
            _pic()
    EndSwitch
Wend

Func _zoomin()
    If GUICtrlRead($zoom) = "Zoom in 2x" Then
        WinMove($Gui, "", 0, 0, $WH[0] * 2, $WH[1] * 2)
        GUICtrlSetPos($pic, 0, 0, $WH[0] * 2, $WH[1] * 2)
        GUICtrlSetData($zoom, "Zoom out 2x")
    Else
        WinMove($Gui, "", 0, 0, $WH[0], $WH[1])
        GUICtrlSetPos($pic, 0, 0, $WH[0], $WH[1])
        GUICtrlSetData($zoom, "Zoom in 2x")
    EndIf   
EndFunc

Func _pic()
    $file = FileOpenDialog("Picture",@ScriptDir,"All Pictures(*.jpg;*.bmp;*.gif)|JPG file (*.jpg)|BMP file (*.bmp)|GIF file (*.gif)|All files (*.*)")
    If Not @error Then
        $WH = GetDimension($file)
        WinMove($Gui, "", 0, 0, $WH[0], $WH[1])
        GUICtrlSetPos($pic, 0, 0, $WH[0], $WH[1])
        GUICtrlSetImage($pic,$file)
        GUICtrlSetState(-1, $GUI_ENABLE)
    EndIf   
EndFunc

Func GetDimension($iPath)
    Local $aDim[2]
    If Not FileExists($iPath) Then
        SetError(1)
        Return $aDim
    EndIf   
     _GDIPlus_Startup()
    Local $hImage, $aDim[2]
    $hImage = _GDIPlus_ImageLoadFromFile($file)
    $aDim[0] = _GDIPlus_ImageGetWidth($hImage)
    $aDim[1] = _GDIPlus_ImageGetHeight($hImage)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
    Return $aDim
EndFunc

Cheers

Edited by smashly

Share this post


Link to post
Share on other sites

Hi,

#include <GUIConstants.au3>
#include <GDIPlus.au3>
Global $file, $WH

$Gui = GUICreate("My GUI",250,200,0,0,$WS_POPUP)
$filemenu = GUICtrlCreateMenu ("&File")
$openitem = GUICtrlCreateMenuitem ("Open",$filemenu)
$exititem = GUICtrlCreateMenuitem ("Exit",$filemenu)
$pic = GUICtrlCreatePic("",0,20,0,0)
GUICtrlSetState(-1, $GUI_DISABLE)
$zoom = GUICtrlCreateButton("Zoom in 2x",0,0,150,20)
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState (@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE, $exititem
            Exit
        Case $zoom
            _zoomin()
        Case $openitem
            _pic()
    EndSwitch
Wend

Func _zoomin()
    If GUICtrlRead($zoom) = "Zoom in 2x" Then
        WinMove($Gui, "", 0, 0, $WH[0] * 2, $WH[1] * 2)
        GUICtrlSetPos($pic, 0, 0, $WH[0] * 2, $WH[1] * 2)
        GUICtrlSetData($zoom, "Zoom out 2x")
    Else
        WinMove($Gui, "", 0, 0, $WH[0], $WH[1])
        GUICtrlSetPos($pic, 0, 0, $WH[0], $WH[1])
        GUICtrlSetData($zoom, "Zoom in 2x")
    EndIf   
EndFunc

Func _pic()
    $file = FileOpenDialog("Picture",@ScriptDir,"All Pictures(*.jpg;*.bmp;*.gif)|JPG file (*.jpg)|BMP file (*.bmp)|GIF file (*.gif)|All files (*.*)")
    If Not @error Then
        $WH = GetDimension($file)
        WinMove($Gui, "", 0, 0, $WH[0], $WH[1])
        GUICtrlSetPos($pic, 0, 0, $WH[0], $WH[1])
        GUICtrlSetImage($pic,$file)
        GUICtrlSetState(-1, $GUI_ENABLE)
    EndIf   
EndFunc

Func GetDimension($iPath)
    Local $aDim[2]
    If Not FileExists($iPath) Then
        SetError(1)
        Return $aDim
    EndIf   
     _GDIPlus_Startup()
    Local $hImage, $aDim[2]
    $hImage = _GDIPlus_ImageLoadFromFile($file)
    $aDim[0] = _GDIPlus_ImageGetWidth($hImage)
    $aDim[1] = _GDIPlus_ImageGetHeight($hImage)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
    Return $aDim
EndFunc

Cheers

>"I:\Programma's\AutoIt\install\SciTe\..\autoit3.exe" /ErrorStdOut "I:\Test map\test.au3"

I:\Programma's\AutoIt\install\Include\GDIPlus.au3 (2688) : ==> Subscript used with non-Array variable.:

Return $aResult[0] <> 0

Return $aResult^ ERROR

>Exit code: 1 Time: 11.900

Is this because im using windows 2000 ? (I cant remember but with my vista or XP i had the same error)


[center]uqoii.nl[/center]

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  
Followers 0