rony2006

Image to binary problem

11 posts in this topic

Hello,

Why $f1 = binary(_INetGetSource("http://icons.iconarchive.com/icons/seanau/email/256/Compose-icon.png")) ;image 1 returns a huge number and

$f1 = binary(@scriptdir & "\1.png") ;image 1 returs a very low number and after I use $f1 in my script I get an error

1.png is the image from http://icons.iconarchive.com/icons/seanau/email/256/Compose-icon.png but saved on my computer.

 

So how I can resolve this please?

My code for testing is:

;~ #include <_PixelGetColor.au3>
#include <GDIPlus.au3>
#include <inet.au3>
#include <array.au3>
$hDll = DllOpen("gdi32.dll")
_GDIPlus_Startup()

Global $image1, $image2

$f1 = binary(_INetGetSource("http://icons.iconarchive.com/icons/seanau/email/256/Compose-icon.png")) ;image 1
$f2 = binary(_INetGetSource("http://icons.iconarchive.com/icons/seanau/email/256/Delete-icon.png")) ;image 2 brighter

;here we use the default step options
$t1 = TimerInit()
$image1 = _Capturepixels($f1) ;get image 1 pixel
ConsoleWrite(TimerDiff($t1)/1000 &' Default Step'&@CRLF)
$t1 = TimerInit()
$image2 = _CapturePixels($f2) ;get image 2 pixels
ConsoleWrite(TimerDiff($t1)/1000& ' Default Step'&@CRLF)
$timer = TimerInit()
$compare = _datacompare($image1[0], $image2[0]);compare them
ConsoleWrite($compare[0]&'% Different(Localized) '&$compare[1]&'% Different(Global)'&@CRLF&'Took '&(TimerDiff($timer)/1000)&' seconds. Default Step'&@CRLF)

;here we double them, notice the preformance increase
$t1 = TimerInit()
$image1 = _Capturepixels($f1, 4, 8) ;get image 1 pixels
ConsoleWrite(TimerDiff($t1)/1000 &' Double Step'&@CRLF)
$t1 = TimerInit()
$image2 = _CapturePixels($f2,4 , 8) ;get image 2 pixels
ConsoleWrite(TimerDiff($t1)/1000& ' Double Step'&@CRLF)
$timer = TimerInit()
$compare = _datacompare($image1[0], $image2[0])
ConsoleWrite($compare[0]&'% Different(Localized) '&$compare[1]&'% Different(Global)'&@CRLF&'Took '&(TimerDiff($timer)/1000)&' seconds. Default Step'&@CRLF)
sleep(5000)


;~ $t1 = TimerInit()
;~ $diffarray = _mapchange($image1[0], $image2[0], $image1[1], $image1[2]) ;compare two images for difference
;~ ConsoleWrite(TimerDiff($t1)/1000& ' _mapchange'&@CRLF)
;~ $t1 = TimerInit()
;~ $image = _toimage($diffarray) ;here we turn the array of colors back into an image
;~ ConsoleWrite(TimerDiff($t1)/1000& ' _toimage'&@CRLF)
;~ _GDIPlus_ImageSaveToFile($image, @scriptdir&'\test.jpg') ;write it to a file
;~ shellexecute(@scriptdir&'\test.jpg')


#cs
Function _datacompare($data1, $data2, [$declimal])
    -$data1: A string of data, any length.
    -$data2: A string of data, must be the same length as $data1
    -$decimal: 1=Binary 9=Base-10, 15=Base-16, 31=Base-32

    Note: If you just want to compare two sets of binary data
    you probably want to use base-16. Unless you are sure your
    binary is in 1's and 0's.

    Returns: An array containing two floats. The first
    value is the localized change, and the second is the global change
#ce
func _datacompare($data1, $data2, $decimal=15)
    Local $difference
$data1 = StringSplit($data1, "")
$data2 = StringSplit($data2, "")
$difference = 0
$found = 0
for $i=1 to $data1[0]
if $data1[$i] <> $data2[$i] Then
    $temp = Abs(_tonum($data1[$i]) - _tonum($data2[$i]))
    $difference += $temp
    $found +=1
EndIf
Next
dim $ret[2]
$ret[0] = ((($difference/$found))/$decimal)*100
$ret[1] = ((($difference/$data1[0]))/$decimal)*100
Return $ret
EndFunc

#cs
Function: _mapchange($base, $new, $x, $y, [$decimal])
    $base: Base data to compare from
    $new: Data to compare
    $x: Width of output data (should be returned by capturepixels)
    $y: Height of outout data (should be returned by capturepixels)
    $decimal: Decimal system, you shouldn't change this

    Note: Use _toimage on data returned by this function to visually see
    a image of the change. (The lighter the color the more change the occured)

    Returns an 2D array of data. Each value of the array represents
    one pixel of the image. Each value is a percent between 0-100
    representing the change that occured in that pixel
#ce
func _mapchange($base, $new, $y, $x, $decimal = 10)
    Local $difference, $xx = 0, $yy = 0
    dim $result[1][$x+1]
    $t1 = TimerInit()
$data1 = _StringequalSplit($base, 8)
$data2 = _StringequalSplit($new, 8)
$difference = 0
for $i=1 to UBound($data1)-1
    if $xx > $x Then
        $xx=0
        $yy+=1
        ConsoleWrite($yy&'/'&$y&' ('&($yy/$y)*100&') '&@CRLF)
        redim $result[$yy+1][$x+1]
    EndIf
    if $data1[$i] <> $data2[$i] Then
    $values1 = StringSplit($data1[$i], "")
    $values2 = stringSplit($data2[$i], "")
    $diff = ""
    for $ix=1 to $values1[0]
    $diff += round((Abs(_tonum($values1[$ix]) - _tonum($values2[$ix]))/$decimal)*100)
    Next
    $diff = Round($diff/$values1[0])
    $result[$yy][$xx] = $diff
    Else
    $result[$yy][$xx] = 0
    EndIf
    $xx += 1
Next
return $result
EndFunc


#cs
Function _tonum($info)
    -$info: A single digit or carachter.

    Returns: A 0-based value.
#ce
func _tonum($info)
if $info+0 > 0 Then Return $info
$info = StringLower($info)
$return = asc($info)-87
switch $return
    Case -39
        Return 0
    Case Else
        Return $return
EndSwitch
EndFunc

#cs
Function _CapturePixels($data, [[$stepy], $stepx])
    -$data: Binary Data
    -$stepy: How often to skip a row of pixelxs. 1 = Never
    -$stepx: How often to skip a single pixel. 1 = Nevere
    Note: Use higher steps for larger images and lower steps
    for smaller images.
#ce
Func _CapturePixels($data, $stepy = 2, $stepx = 2)
    $ret = ""
    $HBITMAP2 = _GDIPlus_BitmapCreateFromMemory($data)
    $y=_GDIPlus_ImageGetWidth($HBITMAP2)
    $x=_GDIPlus_ImageGetHeight($HBITMAP2)

    For $iY = 0 To $x step $stepy
        For $iX = 0 To $y step $stepx
            $rety = StringRight(hex(_GDIPlus_BitmapGetPixel($hBitmap2, $ix, $iy)),8) ;get current pixel color
            $ret &= $rety
;~          ConsoleWrite($iy&'/'&$x&' '&$rety&@CRLF)
            Next
    Next


       ;For $x = 0 To _GDIPlus_ImageGetWidth($HBITMAP2)
       ;  For $y = 0 To _GDIPlus_ImageGetHeight($HBITMAP2)
       ;      $ret &= _PixelGetColor_GetPixel($vDC, $x, $y, $hDll)
      ;   Next
     ;Next
    _WinAPI_DeleteObject($HBITMAP2)
    dim $retx[3]
    $retx[0] = $ret
    $retx[1] = $x/$stepx
    $retx[2] = $y/$stepy
    Return $retx
EndFunc   ;==>Capturepixels

Func _toimage($colors)
    _GDIPlus_Startup()
    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0(UBound($colors, 2), UBound($colors, 1)) ;create an empty bitmap
    Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap) ;get the graphics context of the bitmap
    _GDIPlus_GraphicsSetSmoothingMode($hBmpCtxt, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
    _GDIPlus_GraphicsClear($hBmpCtxt, 0x00000000) ;clear bitmap with color white

    for $i=0 to UBound($colors)-1
        for $i2=0 to UBound($colors,2 )-1
;~          if $colors[$i][$i2] > 30 Then
;~          ConsoleWrite($i&","&$i2&' - '&$colors[$i][$i2]&@CRLF)
        _GDIPlus_BitmapSetPixel($hBitmap, $i2, $i, $colors[$i][$i2]&'0')
;~      ConsoleWrite($i2&','&$i&' '&$colors[$i][$i2]&@CRLF)
;~          EndIf
        Next
    Next

    return $hBitmap ;return bitmap
    ;cleanup GDI+ resources
    _GDIPlus_GraphicsDispose($hBmpCtxt)
EndFunc   ;==>Example

Func _StringEqualSplit($sString, $iNumChars)
    If (Not IsString($sString)) Or $sString = "" Then Return SetError(1, 0, 0)
    If (Not IsInt($iNumChars)) Or $iNumChars < 1 Then Return SetError(2, 0, 0)
    Return StringRegExp($sString, "(?s).{1," & $iNumChars & "}", 3)
EndFunc

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

--

 

Edited by rony2006

Share this post


Link to post
Share on other sites

Threads merged


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Try this:

#include <Array.au3>
#include <GDIPlus.au3>
#include <WindowsConstants.au3>

_GDIPlus_Startup()

Global $hBitmap1, $hBitmap2, $aResult

$hBitmap1 = _GDIPlus_BitmapCreateFromMemory(InetRead("http://icons.iconarchive.com/icons/seanau/email/256/Compose-icon.png"))
$hBitmap2 = _GDIPlus_BitmapCreateFromMemory(InetRead("http://icons.iconarchive.com/icons/seanau/email/256/Delete-icon.png"))

_WinAPI_BitmapsGetDiff($hBitmap1, $hBitmap2, @ScriptDir & "\Diff.png")
$aResult = _GDIPlus_ImageCompare($hBitmap1, $hBitmap2, False)
$aDim = _GDIPlus_ImageGetDimension($hBitmap1)

_GDIPlus_BitmapDispose($hBitmap1)
_GDIPlus_BitmapDispose($hBitmap1)
_GDIPlus_Shutdown()

ShellExecute(@ScriptDir & "\Diff.png")
MsgBox(0, "Diff", Round((UBound($aResult) - 1) / ($aDim[0] * $aDim[1]) * 100, 2) & "% difference")
_ArrayDisplay($aResult)

Func _GDIPlus_ImageCompare($hImage1, $hImage2, $bFastCmp = True)
    Local Const $iW = _GDIPlus_ImageGetWidth($hImage1), $iH = _GDIPlus_ImageGetHeight($hImage1)
    If ($iW <> _GDIPlus_ImageGetWidth($hImage2)) Then Return SetError(1, 0, 0)
    If ($iH <> _GDIPlus_ImageGetHeight($hImage2)) Then Return SetError(2, 0, 0)
    Local $t = TimerInit()
    Local $tBitmapData1 = _GDIPlus_BitmapLockBits($hImage1, 0, 0, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF32ARGB)
    Local $tBitmapData2 = _GDIPlus_BitmapLockBits($hImage2, 0, 0, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF32ARGB)

    Local $pScan1 = DllStructGetData($tBitmapData1, "Scan0")
    Local $tPixel1 = DllStructCreate("int[" & $iW * $iH & "];", $pScan1)
    Local $iStride = Abs(DllStructGetData($tBitmapData1, "Stride"))

    Local $pScan2 = DllStructGetData($tBitmapData2, "Scan0")
    Local $tPixel2 = DllStructCreate("int[" & $iW * $iH & "];", $pScan2)

    If $bFastCmp Then
        $iResult = DllCall("msvcrt.dll", "int:cdecl", "memcmp", "ptr", $pScan1, "ptr", $pScan2, "int", ($iH - 1) * $iStride + ($iW - 1) * 4)[0]
    Else
        Local $iX, $iY, $iRowOffset, $iPixel1, $iPixel2, $aDiff[$iW * $iH + 1][3], $c = 1
        For $iY = 0 To $iH - 1
            $iRowOffset = $iY * $iW + 1
            For $iX = 0 To $iW - 1
                $iPixel1 = DllStructGetData($tPixel1, 1, $iRowOffset + $iX) ;get pixel color
                $iPixel2 = DllStructGetData($tPixel2, 1, $iRowOffset + $iX) ;get pixel color
                If $iPixel1 <> $iPixel2 Then
                    $aDiff[$c][0] = $iX & ", " & $iY
                    $aDiff[$c][1] = "0x" & Hex($iPixel1, 8)
                    $aDiff[$c][2] = "0x" & Hex($iPixel2, 8)
                    $c += 1
                EndIf
            Next
        Next
        $aDiff[0][0] = TimerDiff($t)
        $aDiff[0][1] = $iW
        $aDiff[0][2] = $iH
    EndIf

    _GDIPlus_BitmapUnlockBits($hImage1, $tBitmapData1)
    _GDIPlus_BitmapUnlockBits($hImage2, $tBitmapData2)

    If $bFastCmp Then Return SetError(0, Int(TimerDiff($t)), $iResult = 0)

    ReDim $aDiff[$c][3]
    Return $aDiff
EndFunc

Func _WinAPI_BitmapsGetDiff($hBitmap1, $hBitmap2, $sSave, $iROP = $MERGEPAINT) ;coded by UEZ 2014-02-10
    Local Const $iW = _GDIPlus_ImageGetWidth($hBitmap1), $iH = _GDIPlus_ImageGetHeight($hBitmap1)
    If _GDIPlus_ImageGetWidth($hBitmap2) <> $iW Or _GDIPlus_ImageGetHeight($hBitmap2) <> $iH Then
        _GDIPlus_BitmapDispose($hBitmap1)
        _GDIPlus_BitmapDispose($hBitmap2)
        Return SetError(1, 0, 0)
    EndIf
    Local Const $hHBitmap1 = _WinAPI_CreateDIB($iW, $iH)
    Local Const $hD1 = _WinAPI_GetWindowDC(0)
    Local Const $hDC_backbuffer1 = _WinAPI_CreateCompatibleDC($hD1)
    Local Const $DC_obj1 = _WinAPI_SelectObject($hDC_backbuffer1, $hHBitmap1)
    Local Const $hGraphic1 = _GDIPlus_GraphicsCreateFromHDC($hDC_backbuffer1)
    _GDIPlus_GraphicsSetInterpolationMode($hGraphic1, 5)
    _GDIPlus_GraphicsDrawImageRect($hGraphic1, $hBitmap1, 0, 0, $iW, $iH)

    Local Const $hHBitmap2 = _WinAPI_CreateDIB($iW, $iH)
    Local Const $hD2 = _WinAPI_GetWindowDC(0)
    Local Const $hDC_backbuffer2 = _WinAPI_CreateCompatibleDC($hD2)
    Local Const $DC_obj2 = _WinAPI_SelectObject($hDC_backbuffer2, $hHBitmap2)
    Local Const $hGraphic2 = _GDIPlus_GraphicsCreateFromHDC($hDC_backbuffer2)
    _GDIPlus_GraphicsSetInterpolationMode($hGraphic2, 5)
    _GDIPlus_GraphicsDrawImageRect($hGraphic2, $hBitmap2, 0, 0, $iW, $iH)

    Local Const $hHBitmap3 = _WinAPI_CreateDIB($iW, $iH)
    Local Const $hD3 = _WinAPI_GetWindowDC(0)
    Local Const $hDC_backbuffer3 = _WinAPI_CreateCompatibleDC($hD3)
    Local Const $DC_obj3 = _WinAPI_SelectObject($hDC_backbuffer3, $hHBitmap3)

    _WinAPI_BitBlt($hDC_backbuffer3, 0, 0, $iW, $iH, $hDC_backbuffer1, 0, 0, $SRCCOPY)
    _WinAPI_BitBlt($hDC_backbuffer3, 0, 0, $iW, $iH, $hDC_backbuffer2, 0, 0, $iROP)

    $hBitmap3 = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap3)
    _GDIPlus_ImageSaveToFile($hBitmap3, $sSave)
    _GDIPlus_BitmapDispose($hBitmap3)

    _GDIPlus_GraphicsDispose($hGraphic1)
    _WinAPI_SelectObject($hD1, $DC_obj1)
    _WinAPI_DeleteDC($hDC_backbuffer1)
    _WinAPI_DeleteObject($hHBitmap1)
    _WinAPI_ReleaseDC(0, $hD1)

    _GDIPlus_GraphicsDispose($hGraphic2)
    _WinAPI_SelectObject($hD2, $DC_obj2)
    _WinAPI_DeleteDC($hDC_backbuffer2)
    _WinAPI_DeleteObject($hHBitmap2)
    _WinAPI_ReleaseDC(0, $hD2)

    _WinAPI_SelectObject($hD3, $DC_obj3)
    _WinAPI_DeleteDC($hDC_backbuffer3)
    _WinAPI_DeleteObject($hHBitmap3)
    _WinAPI_ReleaseDC(0, $hD3)
    Return 1
EndFunc

 


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

@UEZ Thank you for the reply but running your code I have the following error:

>"C:\Program Files\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\Marian\Desktop\origine2.au3" /UserParams    
+>22:01:39 Starting AutoIt3Wrapper v.16.306.1237.0 SciTE v.3.6.2.0   Keyboard:00000409  OS:WIN_7/Service Pack 1  CPU:X64 OS:X86  Environment(Language:0409)  CodePage:0  utf8.auto.check:4    # detect ascii high characters and if none found set default encoding to UTF8 and do not add BOM
+>         SciTEDir => C:\Program Files\AutoIt3\SciTE   UserDir => C:\Users\Marian\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\Marian\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.12.0)  from:C:\Program Files\AutoIt3  input:C:\Users\Marian\Desktop\origine2.au3
"C:\Users\Marian\Desktop\origine2.au3"(14,45) : error: _GDIPlus_ImageGetDimension(): undefined function.
$aDim = _GDIPlus_ImageGetDimension($hBitmap1)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Marian\Desktop\origine2.au3 - 1 error(s), 0 warning(s)
!>22:01:40 AU3Check ended. Press F4 to jump to next error.rc:2
+>22:01:40 AutoIt3Wrapper Finished.
>Exit code: 2    Time: 0.6094
 

 

The problem is that I want to use the script with pictures from my computer not from internet.

Share this post


Link to post
Share on other sites

Update AutoIt to latest verson.


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

@UEZ Is working, thank you

But tell me please, what I need to modify in first rows to compare images from script dir.

I tried: 

$hBitmap1 = _GDIPlus_BitmapCreateFromMemory("\1.png")
$hBitmap2 = _GDIPlus_BitmapCreateFromMemory("\2.png")

but is not working, is very anoying because i use the same type of image: png.

Share this post


Link to post
Share on other sites

Thanks, you saved me!

Share this post


Link to post
Share on other sites

@UEZ is possible to use your example to get a value for each picture?

Now the script says something like 50%, but I want for ex to get value 10000 for one image and value 15000 for second one.

Share this post


Link to post
Share on other sites

The differences are listed in the array -> x,y position|image1 color|image2 color


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

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