Jump to content
Sign in to follow this  
corgano

Search google for terms based on image.

Recommended Posts

corgano

I am trying to write a macro to auto-tag images in my library. I am looking for a way to search google images, but NOT trying to get images - I instead want to search an image and get possible words for it.

Things I've tried:

  • I've searched the forums, but almost every hit was searching a phrase to get images - I want to do the opposite.
  • I found two threads on it, One had no answer, and the other used the winhttp udf - which I can never get to work properly :/
  • I could try using IE functions, but it's messy and there's no way to do it completely in the background, which is much preferred :/

Does anyone have any suggestions? Or should I suck it up and use IE?


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites
corgano

I know how much you guys love code. I found a pics,it example on the forum, and tried changing it to work for the google search-by-image url / request. This is what I got

#include <WinHTTP.au3>
#include <zlib.au3>

;~ http://pics.lt/index.php?lang=english

Global $sBoundary = '--' & _GenerateAlphaNumericRandomString ( 50 )

$sImagePath = @ScriptDir&"\0.jpg"
If Not FileExists ( $sImagePath ) Then Exit 1

$sDirectLink = _PicsItUpload ( $sImagePath )
ConsoleWrite ( 'Direct Link : ' & $sDirectLink & @Crlf )
If $sDirectLink Then ShellExecute ( $sDirectLink )

Func _PicsItUpload ( $sImagePath )
    If FileGetSize ( $sImagePath ) > 10*1024*1024 Then Return -1 ; max 10 MB
    Local $ReceivedData = Binary ( '' ), $chunk, $iExtended


    Local $hOpen = _WinHttpOpen ()
    Local $hConnect = _WinHttpConnect ( $hOpen, 'images.google.ca' )
    Local $hRequest = _WinHttpOpenRequest ( $hConnect, 'POST', '/searchbyimage/upload', 'HTTP/1.1' )
    _WinHttpAddRequestHeaders ( $hRequest, 'Referer: http://images.google.ca/' )
    _WinHttpAddRequestHeaders ( $hRequest, 'Accept-Encoding: gzip, deflate' )
    Local $sDataToSend = _PicsItSetDataToSend ( $sImagePath )
    If Not @error Then
        _WinHttpAddRequestHeaders ( $hRequest, 'Content-Length: ' & StringLen ( $sDataToSend ) )
        Local $sContentType = 'Content-Type: multipart/form-data; boundary=' & StringTrimLeft ( $sBoundary, 2 )
        _WinHttpSendRequest ( $hRequest, $sContentType, $WINHTTP_NO_REQUEST_DATA, StringLen ( $sDataToSend ) )
        _UploadProgress ( $hRequest, $sImagePath, $sDataToSend )
        If Not @error Then
            _WinHttpReceiveResponse ( $hRequest )
            If _WinHttpQueryDataAvailable ( $hRequest ) Then

                Local $sHeader = _WinHttpQueryHeaders($hRequest) ; ...get full header
                While 1
                    $chunk = _WinHttpReadData ( $hRequest, 2, 1024 )
                    If Not @extended Then ExitLoop
                    $ReceivedData &= $chunk
                WEnd
                $ReceivedData = BinaryToString ( _ZLIB_GZUncompress ( $ReceivedData ), 1 )
            EndIf
        EndIf
    EndIf
    _WinHttpCloseHandle ( $hRequest )
    _WinHttpCloseHandle ( $hConnect )
    _WinHttpCloseHandle ( $hOpen )
    If $sHeader Then
        ConsoleWrite("Recieved"&@CRLF&$sHeader&@CRLF)
;~         Local $aRet = StringRegExp ( $ReceivedData, '(?s)(?i)codedirect" value="(.*?)"', 3 )
;~         If Not @error Then Return $aRet[0]
    EndIf
EndFunc ;==> _PicsItUpload ()

Func _PicsItSetDataToSend ( $sFilePath )
    If Not FileExists ( $sFilePath ) Then Return SetError ( -1 )
    Local $hFile = FileOpen ( $sFilePath )
    If $hFile = -1 Then Return SetError ( 1, 0, 0 )
    Local $sData = FileRead ( $hFile )
    FileClose ( $hFile )
    Return $sBoundary & @CRLF & _
        'Content-Disposition: form-data; name="fileName[]"' & @CRLF & @CRLF & $sFilePath & @CRLF & $sBoundary & @CRLF & _
        'Content-Disposition: form-data; name="file[]"; filename="' & _FileGetFullNameByFullPath ( $sFilePath ) & '"' & @CRLF & _
        'Content-Type: image/jpeg' & @CRLF & @CRLF & $sData & @CRLF & $sBoundary & @CRLF & _
        'Content-Disposition: form-data; name="alt[]"' & @CRLF & @CRLF & _FileGetNameWithoutExtByFullPath ( $sFilePath ) & @CRLF & $sBoundary & @CRLF & _
        'Content-Disposition: form-data; name="submit"' & @CRLF & @CRLF & 'Upload' & @CRLF & $sBoundary
EndFunc ;==> _PicsItSetDataToSend ()

Func _UploadProgress ( $hRequest, $sPicPath, $sDataToSend )
    If Not FileExists ( $sPicPath ) Then Return SetError ( -1 )
    Local $sDatas = StringToBinary ( $sDataToSend )
    Local $iNumberOfBytesToSend = 1024
    Local $Bin, $iStart = 1, $iExtended = 0
    While 1
        $Bin = BinaryMid ( $sDatas, $iStart, $iNumberOfBytesToSend )
        If BinaryLen ( $Bin ) = 0 Then Return
        _WinHTTPWriteData ( $hRequest, $Bin, 1 )
        If Not @error Then
            $iExtended += @extended
            ConsoleWrite ( '+ $iPercent : ' & Round ( ( $iExtended  ) / ( StringLen ( $sDataToSend ) ) * 100, 2 ) & @Crlf )
            $iStart += $iNumberOfBytesToSend
        Else
            $Bin = 0
            Return SetError ( 1 )
        EndIf
    WEnd
EndFunc ;==> _UploadProgress ()

Func _FileGetNameWithoutExtByFullPath ( $sFullPath )
    Local $aPathSplit = StringSplit ( $sFullPath, '\' )
    If Not @error Then Return StringLeft ( $aPathSplit[UBound($aPathSplit)-1], StringInStr ( $aPathSplit[UBound($aPathSplit)-1], '.', 0, -1 ) - 1 )
EndFunc ;==> _FileGetNameWithoutExtByFullPath ()

Func _FileGetFullNameByFullPath ( $sFullPath )
    Local $aFileName = StringSplit ( $sFullPath, '\' )
    If Not @error Then Return $aFileName[$aFileName[0]]
EndFunc ;==> _FileGetFullNameByFullPath ()

Func _GenerateAlphaNumericRandomString ( $iLen )
    Local $sString = ''
    Do
        Switch Random ( 1, 3, 1 )
            Case 1
                $sString &= Chr ( Random ( 48, 57, 1 ) )  ; 0 to 9
            Case 2
                $sString &= Chr ( Random ( 65, 90, 1 ) )  ; A to Z
            Case 3
                $sString &= Chr ( Random ( 97, 122, 1 ) ) ; a to z
        EndSwitch
    Until StringLen ( $sString ) = $iLen
    Return $sString
EndFunc ;==> _GenerateAlphaNumericRandomString ()

It appears to work. When I look at  it gives me

HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Date: Mon, 07 Jul 2014 07:51:09 GMT
Transfer-Encoding: chunked
Content-Type: text/html; charset=ISO-8859-1
Expires: -1
Server: gws
Set-Cookie: PREF=ID=c64ec6a73aa70b5d:U=9a78a2518fc3dcb0:FF=0:TM=1404719469:LM=1404719469:S=dndXYXDxpnyGV3G1; expires=Wed, 06-Jul-2016 07:51:09 GMT; path=/; domain=.google.ca
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic

when what I should get is

Alternate-Protocol:80:quic
Cache-Control:public, max-age=21600
Content-Length:1759
Content-Type:text/html; charset=UTF-8
Date:Mon, 07 Jul 2014 07:14:32 GMT
Expires:Mon, 07 Jul 2014 13:14:32 GMT
Location:http://images.google.ca/search?tbs=sbi:AMhZZitHkEabunchofcraphere
Server:quimby_frontend
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-XSS-Protection:1; mode=block 

What I need that I'm missing is the Location:http://images.google.ca/search?tbs=sbi:AMhZZitHkEabunchofcraphere, because that tells me where to go next. I'm thinking my request must not be right, but I can't tell hoe. Any ideas?

EDIT: Further investigation makes me think it's not returning the location, because the upload isnt working, because something wrong with how I formatted the request payload / content, so I'm trying to make it look exactly how chrome tells me the request looks.See next post

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites
corgano

I've been messing around the wth the payload data of the request, trying to get it to match what chrome shows is sent, but I hit a snag at 'Content-Type: application/octet-stream'.

What I'm supposed to be sending is this:

 

but when I fileread() the image, all I get is this:

ÿØÿà

But it FileGetSize says the size is indeed 2933. How do I read the image, and what do I do to the format to get it to look like the above example?

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites
corgano

Revisiting old posts, and this is still something I'm wondering how to do. Does anyone know a way to search for tags for an image (using google or something else?)


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites
SadBunny

Well, about your fileread issue: you'll need to specify binary file reading mode when reading a file you know/expect to be binary:

#include <FileConstants.au3>

Local $hFileOpen = FileOpen("c:\temp\test.png", $FO_READ + $FO_BINARY)
Local $sFileRead = FileRead($hFileOpen)
MsgBox(0, "", "Contents of the file:" & @CRLF & $sFileRead)
FileClose($hFileOpen)

No idea about those other things. Good luck :)


Roses are FF0000, violets are 0000FF... All my base are belong to you.

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  

  • Similar Content

    • Xandy
      By Xandy
      MapIt is a tile world editor.  MapIt was built around the concept of reversing Dragon Warrior map images.  MapIt can take image input and produce a tile and world array.
       
      Changing and replacing tile / world data is easy.  B/c tile world editor.

      CTRL+R in image above to signal replace tile action and I use "G" to Get the tile under mouse.
      A full list of hotkeys can be assigned in the: Help Menu\Hotkeys
      Update: Aug 18 2018
      Draw world to pre-rendered layers.  (For separating layers and providing quick blit operations)
      Map with auto-color detection.
      Delete tiles using lasers.  ()


      MapParser is a C++ project that scans images for unique tiles.  MapIt can be downloaded without MapParser, or toggled off in the Scan_Image_Dialog.
      Without MapParser MapIt will use the AutoIt Scan_Tiles() function which is 100 * slower Idk.
      If MapParser.exe will not run for you.
      Installing the Visual C++ Redistributable for Visual Studio 2015 should fix it: https://www.microsoft.com/en-us/download/details.aspx?id=48145  The map images used to generate world and tiles are from here: http://www.realmofdarkness.net/dq/games/nes/dw3/maps/world
      For download, videos, and example of created world file data; please visit the MapIt webpage: http://songersoft.com/programming/mapit/mapit_about.phtml
      Special thanks: AdmiralAlkex, Melba23, MrCrearoR, Dragon Warrior 3
       
    • nooneclose
      By nooneclose
      My program has to first search for names in Column D that do not match up with column C. I got that search to work using arrays. It was slow and I could not figure out how to delete them so I just manually put coded the names that do not belong. I found their cell location but I do not know how to store that location and delete it.
      This is what I have so far.
      Local $NameToDelete1[6]  = _Excel_RangeFind($OpenWorkbook, "Smith, Bill") _ArrayDisplay($NameToDelete1, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") _Excel_RangeDelete($OpenWorkbook.ActiveSheet, $NameToDelete1[2], $xlShiftUp)  
      Please help, I wanted to have this program done yesterday but I did not see this problem until yesterday. 
    • AndyS19
      By AndyS19
      I'm trying to implement a Ctl-F popup box that looks something like the one that Notepad uses, but I'm not havine much luck.  I intend to get it working, then beef up the popup's contents to add several checkboxes, buttons and radio boxes.
      What my example code does is to use InputBox(), but that's not what I want.
      Here is my test code:
      #include <Array.au3> #include <GUIConstantsEx.au3> #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 Opt("GUICloseOnESC", 1) Opt("GUIOnEventMode", 1) Opt('MustDeclareVars', 1) OnAutoItExitRegister("ExitStageLeft") Opt("WinTitleMatchMode", -2) Global $hGUI _Main() Func _Main() $hGUI = GUICreate("Test ^F", 300, 200) setupSpecialKeysHandlers() GUISetOnEvent($GUI_EVENT_CLOSE, "Event_GUIClose") GUISetState() While (1) Sleep(157) WEnd EndFunc ;==>_Main Func handle_CTRL_F_key() Local $str $str = InputBox("Search", "Enter the string to search for:") ConsoleWrite("+++: $str ==>" & $str & "<==" & @CRLF) EndFunc ;==>handle_CTRL_F_key Func ExitStageLeft() Exit (99) EndFunc ;==>ExitStageLeft Func Event_GUIClose() Exit (1) EndFunc ;==>Event_GUIClose Func setupSpecialKeysHandlers() Local $ar, $parts, $key, $handler, $id Local $aAccelKeys[1][2] ; Create a table of Special keys and their handlers $ar = StringSplit("", "") _ArrayAdd($ar, "^f - handle_CTRL_F_key ") ReDim $aAccelKeys[UBound($ar) - 1][2] ; Now, create $aAccelKeys array with the table data. ; For each entry, create a Dummy GUI and associate its ; ID with the special key. For $ndx = 1 To UBound($ar) - 1 $parts = StringSplit($ar[$ndx], "-", 2) $key = StringStripWS($parts[0], 8) $handler = StringStripWS($parts[1], 8) $id = GUICtrlCreateDummy() $aAccelKeys[$ndx - 1][0] = $key $aAccelKeys[$ndx - 1][1] = $id GUICtrlSetOnEvent($id, $handler) Next GUISetAccelerators($aAccelKeys) ; Setup the Special keys hooks EndFunc ;==>setupSpecialKeysHandlers  
    • Atoxis
      By Atoxis
      Howdy, I've gone through a lot of au3 forums, and I once had a working Imagesearch script that I got from here.  However, and i'm just totally not sure how but my imagesearch scripts aren't working anymore.
      I'm not new to au3 but i'm not the most experienced with it's syntax/commands.

      Anyways, I've looked over the big threads involving imagesearch.

      Does anyone have a working Imagesearch x64 for win10 that is currently working as of the date with the post.

      Dll's and what not is fine, just when I tell the script to run, I want to be able to find the image on the screen!
      Can't find a working copy so if anyone has one please send it my way lol.

      I've taken all the imagesearch downloads and what not and have played with them but I can't get any of them working on my end, despite others saying they're working.
      Thanks.
×