Swind / pure-python-adb

This is pure-python implementation of the ADB client.
MIT License
553 stars 104 forks source link

請問screencap可以有辦法改存成array讓python調用嗎? #66

Closed Zhen-Bo closed 3 years ago

Zhen-Bo commented 3 years ago

我想要把擷取到的圖片不儲存直接output並轉成opencv2可用的array 能請問下要怎麼改嗎? 現在卡在不知道怎麼把device.shell("screencap -p")的output存成字串

bdelhaisse commented 3 years ago

If I correctly understood your question, you want to directly use the screenshot as a numpy array to be used with opencv2, right? If that's the case, check the following code:

from ppadb.client import Client
import cv2
import numpy as np

client = Client(host="127.0.0.1", port=5037)
devices = client.devices()
if not devices:
    print("error: no devices detected.")
    exit()
device = devices[0]  # take the first device

# get a screenshot (as a byte array that we will need to convert)
image_byte_array = device.screencap()

# convert the byte array to a numpy array (RGB)
image = cv2.imdecode(np.fromstring(bytes(image_byte_array), np.uint8), cv2.IMREAD_COLOR)

print(image.shape)

# show the image
cv2.imshow('my image', image)
cv2.waitKey(0)
cv2.destroyAllWindows() 
Zhen-Bo commented 3 years ago

Yes,I’ll try it later Thanks a lot

bdelhaisse notifications@github.com 於 2021年1月28日 週四 下午4:43 寫道:

If I correctly understood your question, you want to directly use the screenshot as a numpy array to be used with opencv2, right? If that's the case, check the following code:

from ppadb.client import Clientimport cv2import numpy as np client = Client(host="127.0.0.1", port=5037)devices = client.devices()if not devices: print("error: no devices detected.") exit()device = devices[0] # take the first device

get a screenshot (as a byte array that we will need to convert)image_byte_array = device.screencap()

convert the byte array to a numpy array (RGB)image = cv2.imdecode(np.fromstring(bytes(image_byte_array), np.uint8), cv2.IMREAD_COLOR)

print(image.shape)

show the imagecv2.imshow('my image', image)cv2.waitKey(0)cv2.destroyAllWindows()

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Swind/pure-python-adb/issues/66#issuecomment-768894680, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHMQG7OLB3COUFEJQNVKNSLS4EPTNANCNFSM4VORBZBA .

Zhen-Bo commented 3 years ago

If I correctly understood your question, you want to directly use the screenshot as a numpy array to be used with opencv2, right? If that's the case, check the following code:

from ppadb.client import Client
import cv2
import numpy as np

client = Client(host="127.0.0.1", port=5037)
devices = client.devices()
if not devices:
    print("error: no devices detected.")
    exit()
device = devices[0]  # take the first device

# get a screenshot (as a byte array that we will need to convert)
image_byte_array = device.screencap()

# convert the byte array to a numpy array (RGB)
image = cv2.imdecode(np.fromstring(bytes(image_byte_array), np.uint8), cv2.IMREAD_COLOR)

print(image.shape)

# show the image
cv2.imshow('my image', image)
cv2.waitKey(0)
cv2.destroyAllWindows() 

It works thanks a lot