RuntimeWarning:ubyte_scalars 中遇到溢出

发布于 2025-01-07 09:47:51 字数 4723 浏览 0 评论 0原文

我是Python新手,这是我编写的第一件事,我只是想知道我能做些什么来删除这个警告:

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

I'm new to Python and this is my first ever thing I've scripted and I'm just wondering what I can do to remove this warning:

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

I've tried Googling the answer and nothing that was clear to me came up as far as fixing this.

I'm trying to write a program that will compare a continuously updating image that is taken from a rectangle around my cursor with a reference image that I'm searching for.

Then depending on what region the cursor is in relative to the target image, it will adjust accordingly.

Thank you for any help you can give!

-J

Code is below:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青衫负雪 2025-01-14 09:47:51

您将两个 uint8 值相加,得到一个 uint8 值。您需要在计算中转换数据类型。我建议你试试这个:

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

这应该有效。

编辑:平衡括号

You are adding two uint8 values together resulting in an uint8 value. You need to transform your data types in the calculation. I suggest you try this:

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

This should work.

Edit: balanced bracketts

以酷 2025-01-14 09:47:51

我遇到了类似的问题,我通过将 numpy 数组初始化为 int64 数据类型来解决:

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

I had a similar problem that I resolved by initializing my numpy arrays as an int64 datatype:

imAnchor = array(Image.open(imList[10]), dtype='int64')
和我恋爱吧 2025-01-14 09:47:51

我认为你的问题源于这一行:

pixeldiff=pixeldiff+currentdiff

记住像素通常保存在uint8数据类型中,即0到255。因此,如果你尝试添加其中两个并且它超过255,它将失败。

这样做:

pixeldiff = (pixeldiff+currentdiff)/2

您仍然会获得关系数据,但它将被压缩为 0-255 的正确大小。

I think your problem stems from this line:

pixeldiff=pixeldiff+currentdiff

Remember that pixels are normally saved in uint8 datatype, which is 0 to 255. So if you try to add two of them and it goes over 255, it will fail.

Do something like this:

pixeldiff = (pixeldiff+currentdiff)/2

You will still get the relational data, but it will be compressed into the right size of 0-255.

筱武穆 2025-01-14 09:47:51

RuntimeWarning:ubyte_scalars 中遇到的溢出是由于数据类型造成的。因此更改 anread 和 bnread 的数据类型一次:
anread = anread.astype('int32')
bnread = bnread.astype('int32')

RuntimeWarning: overflow encountered in ubyte_scalars is because of datatype. so change the datatype of anread and bnread once:
anread = anread.astype('int32')
bnread = bnread.astype('int32')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文