python - Convert PIL image to OpenCV2 image -
i'm using entirescreen=imagegrab.grab()
create screengrab , conduct analyses opencv2 test whether screen contains template images. templates loaded template = cv2.imread(name,0)
.
i have following problem: when comparing screenshot templates need first save screenshot :
entirescreen.save('pics/screenshot.png', format='png')
and reload :
cv2.imread('screenshot.png',0)
otherwise following not work:
res = cv2.matchtemplate(img,template,method)
i error message one:
typeerror: image not numpy array, neither scalar
my question: how can convert screenshot entirescreen=imagegrab.grab()
format compatible opencv2, without having save , reload cv2.imread.
on linux systems, 1 can use pyscreenshot
docs
state, replacement imagegrab module, works on windows only.
so, on linux system did -
import pyscreenshot imagegrab
then, can grab screenshot , have access directly in memory space numpy array without having save onto disk , read imread
, -
img = np.array(imagegrab.grab().convert('rgb'))
this img
used cv2.matchtemplate
is.
a step-by-step sample run , verification of output -
in [32]: import pyscreenshot imagegrab in [33]: img = np.array(imagegrab.grab().convert('rgb')) in [34]: img.shape out[34]: (768, 1366, 3) in [35]: img.dtype out[35]: dtype('uint8')
here's sample run showing how cv2.matchtemplate
used img
-
in [41]: res = cv2.matchtemplate(img[:,:,0],img[10:40,50:80,0],cv2.tm_sqdiff) in [42]: np.where(res<10) # 10 threshold matching out[42]: (array([10]), array([50]))
Comments
Post a Comment