Jump to content
juannfox

Variable not being recognized as a number - (Moved)

Recommended Posts

Good morning, all. 

I have been working on this little proyect that scales images (for another OCR proyect) and I have bumped into an error:

The $Width variable (global) that has a numerical value is apparently not recognized as a number, since when I try to multiply it by the Scale factor (2) it results in 0. $Width is originated from string splitting the Resolution property of the chosen image. 

Code Below:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <ExtProp.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <AutoItConstants.au3>

Global $InputImage = @ScriptDir & "\Input.png"
Global $OutputImage = @ScriptDir & "\ScaledIMG.png"

GetImageSize ()
ScaleImage ()

;Obtiene dimensiones ANCHOxALTO de la imagen a procesar.
Func GetImageSize ()
    ;Obtiene propiedad nro. 31 (Windows 7) que contiene en un solo string ANCHOxALTO
    Global $ImageSize = _GetExtProperty($InputImage, 31)

    ;En caso de no obtener la resolución, será imposible luego determinar cuanto escalar la imagen.
    If $ImageSize = "0" Then
        Global $Scaleable = 0
        MsgBox ($MB_ICONERROR, "MDA-OCR", "Error al obtener dimensiones de imagen, no podrá escalarse la misma. La factura se procesará, pero su análisis no será preciso.")
    Else
        ;Separa los componentes de la dimensión en Ancho y Alto.
        Local $SizeComponent = StringSplit ($ImageSize, " x ", 1)

        Global $Scaleable = 1
        Global $Width = $SizeComponent[1]
        Global $Height = $SizeComponent[2]

    EndIf

EndFunc


Func ScaleImage ()

    Local $InfranViewExe = "IrfanViewPortable\IrfanViewPortable.exe"
;~     Local $InfranViewExe = "C:\Program Files\IrfanView\i_view64.exe"

    If $Scaleable = 1 Then
        Global $ScaleFactor = "2"
        Global $RezWidth = $Width * $ScaleFactor
        Global $RezHeight = $Height * $ScaleFactor

        RunWait ($InfranViewExe & " """ & $InputImage & """ /resize=(" & $RezWidth & "," & $RezHeight & ") /convert=""" & $OutputImage & """ /resample")

;This is the debug line, which outputs Resolution, then Width and Height (obtained from splitting resolution string) and the scaled W/H.
        MsgBox ($MB_SYSTEMMODAL, "Dimensiones", $ImageSize & "," & $Width & "," & $RezWidth & "," & $Height & "," & $RezHeight)
    Else
        FileMove ($InputImage, $OutputImage, $FC_OVERWRITE)
    EndIf

EndFunc

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

The debug Message Brings me everything but the $RezWidth, which instead of being the double of $Width is "0".

 

Credits to the creator of ExtProp UDF.

OutputMsg.PNG

Share this post


Link to post
Share on other sites

After many tries, I realized that converting it to binary and back to string, it would show a hidden character. So I solved it by trimming 1 character out of the string:


        Global $RawWidth = $SizeComponent[1]
        Global $Width = StringTrimLeft ($sWidth, 1)

Thanks for the help to those who took their time and read.

Share this post


Link to post
Share on other sites

You should never declare your global variables inside a function, just assign values to it in them. Second, don't declare variables inside an If structure, otherwise you might end up with undeclared variables later in the script if the comparison in the If structure fails for some reason. Declare them, then assign values to them in the If structure.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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

×
×
  • Create New...