简体   繁体   中英

python-opencv How i can convert file-object to numpy.ndarray

url = 'http://www.xxxx.1.jpg'
fobj = urllib2.urlopen(url).read()
f = open('1.jpg','wb')
f.write(fobj)

img = cv2.imread('1.jpg')

Can I have any better way?I don't want to save file everytime!

You can use cv2.imdecode() to directly read the image data. But it needs to be transformed into a numpy.ndarray first:

jpeg_array = bytearray(fobj)
img = cv2.imdecode(np.asarray(jpeg_array), cv2.CV_LOAD_IMAGE_COLOR)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM