2012-02-21 48 views
8

我是新來的Python,這是我劇本我第一次的事情,我只是想知道我能做些什麼來消除這種警告:RuntimeWarning:溢出ubyte_scalars遇到

Warning (from warnings module): 
    File "C:\Users\Luri\Desktop\Bot Stuff\ImageSaver.py", line 76 
    currentdiff=abs(anread[w,h])-abs(bnread[w,h]) 
RuntimeWarning: overflow encountered in ubyte_scalars 

我我試着用谷歌搜索的答案,並沒有什麼是我清楚瞭解到這一點。

我在試圖編寫一個程序,它將比較從我的光標周圍的矩形中取出的不斷更新的圖像與正在搜索的參考圖像。

然後根據光標相對於目標圖像的區域,它會相應地進行調整。

感謝您給予任何幫助!

-J

代碼如下:

import os 
import sys 
import time 
import Image 
import ImageGrab 
import win32api 
import numpy, scipy 

def mousePos(): 
#--------------------------------------------------------- 
#User Settings: 
    SaveDirectory=r'C:\Users\Luri\Desktop\Bot Stuff' 
    ImageEditorPath=r'C:\WINDOWS\system32\mspaint.exe' 
#Here is another example: 
#ImageEditorPath=r'C:\Program Files\IrfanView\i_view32.exe' 
#--------------------------------------------------------- 
    i,j = win32api.GetCursorPos() 
    print 'Your Cusor Position is:', i,j 
    time.sleep(1) 
    size = 112, 58 
#------------------- 
#data is defined as | x0y0 = [0,0] = (xpos-56,ypos-29) | x0y1 = [0,1] = (xpos-56,ypos+29) | x1y1 = [1,1] = (xpos+56,ypos+29) | x1y0 = [1,0] = (xpos+56,ypos-29) 
#Take In Image In Rectangle around cursor position to locate text of name 
    pixeldiff=0 
    currentdiff=0 
    NQ1=193395 
    NQ2=166330 
    NQ3=171697 
    NQ4=168734 
    NAC=190253 
    NBC=205430 
    x0=i-56 
    y0=j-29 
    x1=i+56 
    y1=j+29 
    box=[x0, y0, x1, y1] 
    img=ImageGrab.grab() 
    saveas=os.path.join(SaveDirectory,'fullscreen.jpg') 
    img.save(saveas) 
    editorstring='""%s" "%s"'% (ImageEditorPath,saveas) 
#Crop box around cursor 
    cursorbox=img.crop(box) 
    saveas=os.path.join(SaveDirectory,'cursorbox.jpg') 
    cursorbox.save(saveas) 
#Converts the given cursor rectangle to 8bit grayscale from RGB 
    out = cursorbox.convert("L") 
    saveas=os.path.join(SaveDirectory,'lmodecurbox.jpg') 
    out.save(saveas) 
#Takes the converted grayscale picture and converts it to an array 
    a=numpy.asarray(out) 
    aarray=Image.fromarray(a) 
    sizea = a.shape 
# print sizea 
# print a 
    anread=a[:] 
#Loads the reference image 
    reference=Image.open("referencecold.png") 
#Converts the given cursor rectangle to 8bit grayscale from RGB 
    refout = reference.convert("L") 
    saveas=os.path.join(SaveDirectory,'lmoderefbox.jpg') 
    refout.save(saveas) 
#Takes the converted grayscale picture and converts it to an array 
    b=numpy.asarray(refout) 
    barray=Image.fromarray(b) 
    sizeb = b.shape 
# print sizeb 
# print b 
# print size 
    bnread=b[:] 
# print bnread 
#Realized you can determine position based on this single quadrant 
#Loop Quadrant 1 x0y1 to xmym 
    for h in range(0,29): 
    for w in range(0,55): 
     #currentdiff=0 
     currentdiff=abs(anread[w,h])-abs(bnread[w,h]) 
     pixeldiff=pixeldiff+currentdiff 
# print pixeldiff 
#Test Above 
    if pixeldiff<198559 and pixeldiff>190253: 
    #Test Left 
    if pixeldiff > 175000: 
    #Move Above and Left 
     print ('Go Up and Left') 
    else: 
    #Move Above Right 
     print ('Go Up and Right') 
    if pixeldiff>198559 and pixeldiff<205430: 
    if pixeldiff < 185000: 
    #Move Below and Left 
     print ('Go Down and Left') 
    else: 
    #Move Below and Right 
     print ('Go Down and Right') 
""" 
#Nominal Q1=193395 Variance low = 188408 Variance high = 203194 
#Nominal Q2=166330 Variance low = 181116 Variance high = 199208 
#Nominal Q3=171697 Variance low = 172279 Variance high = 201816 
#Nominal Q4=168734 Variance low = 190644 Variance high = 191878 
#Nominal Center = 198559 
#Nominal Above Center = 190253 
#Nominal Below Center = 205430 
#Loop Quadrant 2 xmy1 to x1ym 
    for h in range(0,29): 
    for w in range(55,111): 
     difference=abs(a(w,h)-b(w,h)) 
     currentdiff=abs(anread[w,h])-abs(bnread[w,h]) 
     pixeldiff=pixeldiff+currentdiff 
#Loop Quadrant 3 x0ym to xmy0 
    for h in range(29,57): 
    for w in range(0,55): 
     difference=abs(a(w,h)-b(w,h)) 
     currentdiff=abs(anread[w,h])-abs(bnread[w,h]) 
     pixeldiff=pixeldiff+currentdiff 
#Loop Quadrant 4 xmym to x1y0 
    for h in range(29,57): 
    for w in range(55,111): 
     difference=abs(a(w,h)-b(w,h)) 
     currentdiff=abs(anread[w,h])-abs(bnread[w,h]) 
     pixeldiff=pixeldiff+currentdiff 
#Fine Nominal Values for Each quadrant pixeldiff 
#Compare which is similar and then move cursor in center of that quadrant 
""" 

def main(): 
# while True: 
    mousePos() 

if __name__ == "__main__": 
    main() 




#Compare image to constantly updating image of rectangle around cursor (maybe per second?) by searching for the quadrant with most similarity 

#------------------- 

#Based on comparison, move cursor to middle (x and y value) of matched quadrant by population of similar features and repeat 

回答

1

我有一個類似的問題,我用我的初始化數組numpy的作爲int64數據類型解析:

imAnchor = array(Image.open(imList[10]), dtype='int64') 
+0

這可能會增加圖像佔用的內存 – BiA 2015-03-23 09:00:05

0

我認爲你的問題源於此行:

pixeldiff=pixeldiff+currentdiff 

請記住像素通常保存在uint8數據類型中,該數據類型爲0到255.因此,如果您嘗試添加兩個像素並超過255,則會失敗。

做這樣的事情:

pixeldiff = (pixeldiff+currentdiff)/2 

你仍然會得到的相關數據,但將被壓縮到0-255大小合適。

+0

問題在於此之前的行。它需要在那裏轉換爲'int()'。 'pixeldiff'行已經太晚了。 – blubberdiblub 2016-05-01 07:49:13

8

您正在將兩個uint8值相加,從而導致uint8值。您需要在計算中轉換您的數據類型。我建議你試試這個:

pixeldiff = (int(ipxeldiff)+int(currentdiff)/2 

這應該有效。

+0

問題在此之前已經存在。它需要在那裏轉換爲'int()'。 'pixeldiff'行已經太晚了。 – blubberdiblub 2016-05-01 07:46:31

相關問題