1
1

More than 1 year has passed since last update.

【Python】pyautoguiの画像検索範囲は必ずint型にすべし

Last updated at Posted at 2022-08-03

<初めに>
pyautoguiを活用して、RPA化を粛々と行っていたが
急にpyautoguiの画像検索が出来なくなりエラー回避に1時間半費やしたので
今回はそのエラー履歴を残しておく

<エラーが発生した原因>
初めにエラー発生原因は、
pyautogui画像検索で画像検索範囲を絞る(ragion=target_range)を
使用しているのだが、target_rangeに入っている
画像検索範囲(left, top, weidh, height)の数値が「float型」だった為、
今回のエラーが発生した

<エラー文>
↓エラー文はこれ↓

例外が発生しました: TypeError
slice indices must be integers or None or have an __index__ method

このエラーを調べるとスライスがどうとか初歩的なものばかりで
pyautoguiに繋がるエラー回避方法は検索でヒットせず原因が分からなかった…

<今後>
今回は、画像検索範囲をcsvファイルで管理していたため
起こったエラーなので(読みだすときにfloat型になった)
今後は、csvファイルから読みだす際は必ずint型にしてから
画像検索範囲を設定しようと心に決めた。。。。(1時間半も無駄にしたのでね…)
↓error文↓

target_range = (0.0, 0.0, 1000.0, 700.0)

p = pyautogui.locateOnScreen('./test.png', grayscale=True, confidence=0.6, region=target_range)
x, y = pyautogui.center(p)
print(x, y)

↓error回避文↓

target_range = (0, 0, 1000, 700)

p = pyautogui.locateOnScreen('./test.png', grayscale=True, confidence=0.6, region=target_range)
x, y = pyautogui.center(p)
print(x, y)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1