PDA

Просмотр полной версии : Поиск изображения на экране pyautogui


Shelok_Kholmes
09.12.2023, 04:53
Ща сдохну, сижу туплю как нубло, у меня есть картинка, например возьмём калькулятор, там есть цифры, к примеру возьмём это:





https://forum.antichat.xyz/attachments/28422056/img_aff07af66c.png
, мне нужно сделать, чтоб когда эта картинка находится на экране в определённой области, с помощью модуля keyboard нажималась цифра, к примеру 8, НО, если в области поиска будет кнопка, не 8, а НАПРИМЕР
https://forum.antichat.xyz/attachments/28422056/img_8421fde6df.png
, то уже с помощью keyboardнажалась другая кнопка, например "Б".

Dickson
26.12.2023, 19:50
это ?

test.py:






import
cv2
import
pyautogui
import
numpy
as
np
import
time
# Загрузка изображений
img_vosem
=
cv2
.
imread
(
'vosem.png'
,
0
)
img_dva
=
cv2
.
imread
(
'dva.png'
,
0
)
def
locate_image
(
template
,
threshold
=
0.8
)
:
screenshot
=
pyautogui
.
screenshot
(
)
screenshot
=
np
.
array
(
screenshot
)
screenshot
=
cv2
.
cvtColor
(
screenshot
,
cv2
.
COLOR_RGB2BGR
)
screenshot_gray
=
cv2
.
cvtColor
(
screenshot
,
cv2
.
COLOR_BGR2GRAY
)
result
=
cv2
.
matchTemplate
(
screenshot_gray
,
template
,
cv2
.
TM_CCOEFF_NORMED
)
min_val
,
max_val
,
min_loc
,
max_loc
=
cv2
.
minMaxLoc
(
result
)
if
max_val
>=
threshold
:
return
(
max_loc
[
0
]
,
max_loc
[
1
]
)
else
:
return
None
while
True
:
vosem_loc
=
locate_image
(
img_vosem
)
if
vosem_loc
:
print
(
"Нашел 8. Корды:"
,
vosem_loc
)
time
.
sleep
(
1
)
pyautogui
.
press
(
'8'
)
dva_loc
=
locate_image
(
img_dva
)
if
dva_loc
:
print
(
"Нашел 2. Корды:"
,
dva_loc
)
time
.
sleep
(
1
)
pyautogui
.
press
(
'2'
)

Dickson
30.12.2023, 20:28
так то, или все-таки я тебя неправильно понял ?

Shelok_Kholmes
30.12.2023, 21:08
так то, или все-таки я тебя неправильно понял ?


Та я сам уже разобрался, но спсик.)



так то, или все-таки я тебя неправильно понял ?


Сделал по другому.

Anarch
30.12.2023, 21:33
это ?

test.py:






import
cv2
import
pyautogui
import
numpy
as
np
import
time
# Загрузка изображений
img_vosem
=
cv2
.
imread
(
'vosem.png'
,
0
)
img_dva
=
cv2
.
imread
(
'dva.png'
,
0
)
def
locate_image
(
template
,
threshold
=
0.8
)
:
screenshot
=
pyautogui
.
screenshot
(
)
screenshot
=
np
.
array
(
screenshot
)
screenshot
=
cv2
.
cvtColor
(
screenshot
,
cv2
.
COLOR_RGB2BGR
)
screenshot_gray
=
cv2
.
cvtColor
(
screenshot
,
cv2
.
COLOR_BGR2GRAY
)
result
=
cv2
.
matchTemplate
(
screenshot_gray
,
template
,
cv2
.
TM_CCOEFF_NORMED
)
min_val
,
max_val
,
min_loc
,
max_loc
=
cv2
.
minMaxLoc
(
result
)
if
max_val
>=
threshold
:
return
(
max_loc
[
0
]
,
max_loc
[
1
]
)
else
:
return
None
while
True
:
vosem_loc
=
locate_image
(
img_vosem
)
if
vosem_loc
:
print
(
"Нашел 8. Корды:"
,
vosem_loc
)
time
.
sleep
(
1
)
pyautogui
.
press
(
'8'
)
dva_loc
=
locate_image
(
img_dva
)
if
dva_loc
:
print
(
"Нашел 2. Корды:"
,
dva_loc
)
time
.
sleep
(
1
)
pyautogui
.
press
(
'2'
)





А че там в pygui

- скриншотишь изображение

- вставляешь ссылку

пример:
https://forum.antichat.xyz/attachments/28432942/img_1ac1bbb7f3.png

Dickson
31.12.2023, 07:44
А че там в pygui
- скриншотишь изображение
- вставляешь ссылку
пример:


какая ссылка

Anarch
31.12.2023, 20:03
какая ссылка



https://forum.antichat.xyz/attachments/28433453/img_e1194ba41e.png

Shelok_Kholmes
31.12.2023, 23:08
А че там в pygui
- скриншотишь изображение
- вставляешь ссылку
пример:


Дополню, что нужно использовать try except, чтоб ошибку не выдавало.

Dickson
01.01.2024, 23:31
я тебя не понял честно говоря

swap commends
02.01.2024, 16:33
я тебя не понял честно говоря


а как реализовать что бы после нажатия на кнопку, отправлялось сообщение в тг?

Dickson
03.01.2024, 15:52
а как реализовать что бы после нажатия на кнопку, отправлялось сообщение в тг?



code:




test.py:






import
cv2
import
pyautogui
import
numpy
as
np
import
time
import
telegram
import
asyncio

bot
=
telegram
.
Bot
(
token
=
'token tg'
)
chat_id
=
'chat id tg'
# Загрузка изображений
img_vosem
=
cv2
.
imread
(
'vosem.png'
,
0
)
img_dva
=
cv2
.
imread
(
'dva.png'
,
0
)
async
def
send_message_async
(
chat_id
,
message
)
:
await
bot
.
send_message
(
chat_id
=
chat_id
,
text
=
message
)
def
locate_image
(
template
,
threshold
=
0.8
)
:
screenshot
=
pyautogui
.
screenshot
(
)
screenshot
=
np
.
array
(
screenshot
)
screenshot
=
cv2
.
cvtColor
(
screenshot
,
cv2
.
COLOR_RGB2BGR
)
screenshot_gray
=
cv2
.
cvtColor
(
screenshot
,
cv2
.
COLOR_BGR2GRAY
)
result
=
cv2
.
matchTemplate
(
screenshot_gray
,
template
,
cv2
.
TM_CCOEFF_NORMED
)
min_val
,
max_val
,
min_loc
,
max_loc
=
cv2
.
minMaxLoc
(
result
)
if
max_val
>=
threshold
:
return
(
max_loc
[
0
]
,
max_loc
[
1
]
)
else
:
return
None
async
def
main
(
)
:
while
True
:
vosem_loc
=
locate_image
(
img_vosem
)
if
vosem_loc
:
message
=
f"Нашел 8. Корды:{vosem_loc}"
await
send_message_async
(
chat_id
,
message
)
time
.
sleep
(
5
)
pyautogui
.
press
(
'8'
)
time
.
sleep
(
5
)
dva_loc
=
locate_image
(
img_dva
)
if
dva_loc
:
message
=
f"Нашел 2. Корды:{dva_loc}"
await
send_message_async
(
chat_id
,
message
)
time
.
sleep
(
5
)
pyautogui
.
press
(
'2'
)
time
.
sleep
(
5
)
loop
=
asyncio
.
get_event_loop
(
)
loop
.
run_until_complete
(
main
(
)
)

swap commends
04.01.2024, 22:13
bot = telegram.Bot(token='token tg')




AttributeError: module 'telegram' has no attribute 'Bot'



# Загрузка изображений
img_vosem = cv2.imread('vosem.png', 0)
img_dva = cv2.imread('dva.png', 0)


я же могу тут вставить адресс скриншота где у меня он лежит?

Dickson
05.01.2024, 01:43
AttributeError: module 'telegram' has no attribute 'Bot'


pip install python-telegram-bot



я же могу тут вставить адресс скриншота где у меня он лежит?


скрин должен лежать там же, где и скрипт, просто замени название

swap commends
05.01.2024, 16:55
Python:






import
cv2
import
pyautogui
import
numpy
as
np
import
time
import
telegram
import
asyncio

bot
=
telegram
.
Bot
(
token
=
'---'
)
chat_id
=
'chat id tg'
# Загрузка изображений
img_accept
=
cv2
.
imread
(
'accept.png'
,
0
)
img_ready
=
cv2
.
imread
(
'ready.png'
,
0
)
async
def
send_message_async
(
chat_id
,
message
)
:
await
bot
.
send_message
(
chat_id
=
chat_id
,
text
=
message
)
def
locate_image
(
template
,
threshold
=
0.8
)
:
screenshot
=
pyautogui
.
screenshot
(
)
screenshot
=
np
.
array
(
screenshot
)
screenshot
=
cv2
.
cvtColor
(
screenshot
,
cv2
.
COLOR_RGB2BGR
)
screenshot_gray
=
cv2
.
cvtColor
(
screenshot
,
cv2
.
COLOR_BGR2GRAY
)
result
=
cv2
.
matchTemplate
(
screenshot_gray
,
template
,
cv2
.
TM_CCOEFF_NORMED
)
min_val
,
max_val
,
min_loc
,
max_loc
=
cv2
.
minMaxLoc
(
result
)
if
max_val
>=
threshold
:
return
(
max_loc
[
0
]
,
max_loc
[
1
]
)
else
:
return
None
async
def
main
(
)
:
while
True
:
accept_loc
=
locate_image
(
img_accept
)
if
accept_loc
:
message
=
f"Нашел игру. Корды:{accept_loc}"
await
send_message_async
(
chat_id
,
message
)
await
asyncio
.
sleep
(
5
)
pyautogui
.
press
(
'ready'
)
await
asyncio
.
sleep
(
5
)
ready_loc
=
locate_image
(
img_ready
)
if
ready_loc
:
message
=
f"Нашел готовность. Корды:{ready_loc}"
await
send_message_async
(
chat_id
,
message
)
await
asyncio
.
sleep
(
5
)
pyautogui
.
press
(
'ready'
)
await
asyncio
.
sleep
(
5
)
asyncio
.
run
(
main
(
)
)






Exception ignored in:
Traceback (most recent call last):
File "C:\Users\adm1n\AppData\Local\Programs\Python\Pytho n310\lib\asyncio\proactor_events.py", line 116, in __del__
File "C:\Users\adm1n\AppData\Local\Programs\Python\Pytho n310\lib\asyncio\proactor_events.py", line 108, in close
File "C:\Users\adm1n\AppData\Local\Programs\Python\Pytho n310\lib\asyncio\base_events.py", line 745, in call_soon
File "C:\Users\adm1n\AppData\Local\Programs\Python\Pytho n310\lib\asyncio\base_events.py", line 510, in _check_closed
RuntimeError: Event loop is closed

Dickson
05.01.2024, 18:56
Python:






import
cv2
import
pyautogui
import
numpy
as
np
import
time
import
telegram
import
asyncio

bot
=
telegram
.
Bot
(
token
=
'---'
)
chat_id
=
'chat id tg'
# Загрузка изображений
img_accept
=
cv2
.
imread
(
'accept.png'
,
0
)
img_ready
=
cv2
.
imread
(
'ready.png'
,
0
)
async
def
send_message_async
(
chat_id
,
message
)
:
await
bot
.
send_message
(
chat_id
=
chat_id
,
text
=
message
)
def
locate_image
(
template
,
threshold
=
0.8
)
:
screenshot
=
pyautogui
.
screenshot
(
)
screenshot
=
np
.
array
(
screenshot
)
screenshot
=
cv2
.
cvtColor
(
screenshot
,
cv2
.
COLOR_RGB2BGR
)
screenshot_gray
=
cv2
.
cvtColor
(
screenshot
,
cv2
.
COLOR_BGR2GRAY
)
result
=
cv2
.
matchTemplate
(
screenshot_gray
,
template
,
cv2
.
TM_CCOEFF_NORMED
)
min_val
,
max_val
,
min_loc
,
max_loc
=
cv2
.
minMaxLoc
(
result
)
if
max_val
>=
threshold
:
return
(
max_loc
[
0
]
,
max_loc
[
1
]
)
else
:
return
None
async
def
main
(
)
:
while
True
:
accept_loc
=
locate_image
(
img_accept
)
if
accept_loc
:
message
=
f"Нашел игру. Корды:{accept_loc}"
await
send_message_async
(
chat_id
,
message
)
await
asyncio
.
sleep
(
5
)
pyautogui
.
press
(
'ready'
)
await
asyncio
.
sleep
(
5
)
ready_loc
=
locate_image
(
img_ready
)
if
ready_loc
:
message
=
f"Нашел готовность. Корды:{ready_loc}"
await
send_message_async
(
chat_id
,
message
)
await
asyncio
.
sleep
(
5
)
pyautogui
.
press
(
'ready'
)
await
asyncio
.
sleep
(
5
)
asyncio
.
run
(
main
(
)
)






дай тг свой