From JPG to b64encode to cv2.imread()
up vote
3
down vote
favorite
For a program I am writing, I am transferring an image from one computer - using base64.b64encode(f.read(image)) - and trying to read it in the receiving script without saving it to hard drive (in an effort to minimize process time). I'm having a hard time figuring out how to read the image into OpenCV without saving it locally.
Here is what my code for sending the image looks like:
f = open(image.jpg)
sendthis = f.read()
f.close()
databeingsent = base64.b64encode(sendthis)
client.publish('/image',databeingsent,0)
# this is an MQTT publish, details for SO shouldn't be relevant
Meanwhile, here is the code receiving it. (This is in an on_message function, since I'm using MQTT for the transfer.)
def on_message(client, userdata, msg): # msg.payload is incoming data
img = base64.b64decode(msg.payload)
source = cv2.imread(img)
cv2.imshow("image", source)
After the message decodes, I have the error:
"TypeError: Your input type is not a numpy array".
I've done some searching, and I can't seem to find a relevant solution - some exist regarding converting from text files to numpy using b64, but none really relate to using an image and immediately reading that decoded data into OpenCV without the intermediary step of saving it to the harddrive (using the inverse process used to read the file in the "send" script).
I'm still pretty new to Python and OpenCV, so if there's a better encoding method to send the image - whatever solves the problem. How the image is sent is irrelevant, so long as I can read it in on the receiving end without saving it as a .jpg to disk.
Thanks!
python opencv numpy
|
show 5 more comments
up vote
3
down vote
favorite
For a program I am writing, I am transferring an image from one computer - using base64.b64encode(f.read(image)) - and trying to read it in the receiving script without saving it to hard drive (in an effort to minimize process time). I'm having a hard time figuring out how to read the image into OpenCV without saving it locally.
Here is what my code for sending the image looks like:
f = open(image.jpg)
sendthis = f.read()
f.close()
databeingsent = base64.b64encode(sendthis)
client.publish('/image',databeingsent,0)
# this is an MQTT publish, details for SO shouldn't be relevant
Meanwhile, here is the code receiving it. (This is in an on_message function, since I'm using MQTT for the transfer.)
def on_message(client, userdata, msg): # msg.payload is incoming data
img = base64.b64decode(msg.payload)
source = cv2.imread(img)
cv2.imshow("image", source)
After the message decodes, I have the error:
"TypeError: Your input type is not a numpy array".
I've done some searching, and I can't seem to find a relevant solution - some exist regarding converting from text files to numpy using b64, but none really relate to using an image and immediately reading that decoded data into OpenCV without the intermediary step of saving it to the harddrive (using the inverse process used to read the file in the "send" script).
I'm still pretty new to Python and OpenCV, so if there's a better encoding method to send the image - whatever solves the problem. How the image is sent is irrelevant, so long as I can read it in on the receiving end without saving it as a .jpg to disk.
Thanks!
python opencv numpy
2
useimdecode
instead ofimread
– Miki
Nov 4 '15 at 12:27
"buf is not a numpy array, neither a scalar" tried passing the b64decode step here, still got this error. so, still asking for a numpy array
– godfreap
Nov 4 '15 at 12:34
See if this helps. Then you need imdecode, because imread loads an image from disk.
– Miki
Nov 4 '15 at 12:38
1
probablynp.array(list(img), dtype=np.uint8)
ornp.fromstring(img, dtype=np.uint8)
will do the trick. But I don't know python. My advice is just that you need imdecode. How to get parameters type right it's up to you :D
– Miki
Nov 4 '15 at 12:55
1
Glad it helped. Thanks for the working block :D
– Miki
Nov 4 '15 at 13:07
|
show 5 more comments
up vote
3
down vote
favorite
up vote
3
down vote
favorite
For a program I am writing, I am transferring an image from one computer - using base64.b64encode(f.read(image)) - and trying to read it in the receiving script without saving it to hard drive (in an effort to minimize process time). I'm having a hard time figuring out how to read the image into OpenCV without saving it locally.
Here is what my code for sending the image looks like:
f = open(image.jpg)
sendthis = f.read()
f.close()
databeingsent = base64.b64encode(sendthis)
client.publish('/image',databeingsent,0)
# this is an MQTT publish, details for SO shouldn't be relevant
Meanwhile, here is the code receiving it. (This is in an on_message function, since I'm using MQTT for the transfer.)
def on_message(client, userdata, msg): # msg.payload is incoming data
img = base64.b64decode(msg.payload)
source = cv2.imread(img)
cv2.imshow("image", source)
After the message decodes, I have the error:
"TypeError: Your input type is not a numpy array".
I've done some searching, and I can't seem to find a relevant solution - some exist regarding converting from text files to numpy using b64, but none really relate to using an image and immediately reading that decoded data into OpenCV without the intermediary step of saving it to the harddrive (using the inverse process used to read the file in the "send" script).
I'm still pretty new to Python and OpenCV, so if there's a better encoding method to send the image - whatever solves the problem. How the image is sent is irrelevant, so long as I can read it in on the receiving end without saving it as a .jpg to disk.
Thanks!
python opencv numpy
For a program I am writing, I am transferring an image from one computer - using base64.b64encode(f.read(image)) - and trying to read it in the receiving script without saving it to hard drive (in an effort to minimize process time). I'm having a hard time figuring out how to read the image into OpenCV without saving it locally.
Here is what my code for sending the image looks like:
f = open(image.jpg)
sendthis = f.read()
f.close()
databeingsent = base64.b64encode(sendthis)
client.publish('/image',databeingsent,0)
# this is an MQTT publish, details for SO shouldn't be relevant
Meanwhile, here is the code receiving it. (This is in an on_message function, since I'm using MQTT for the transfer.)
def on_message(client, userdata, msg): # msg.payload is incoming data
img = base64.b64decode(msg.payload)
source = cv2.imread(img)
cv2.imshow("image", source)
After the message decodes, I have the error:
"TypeError: Your input type is not a numpy array".
I've done some searching, and I can't seem to find a relevant solution - some exist regarding converting from text files to numpy using b64, but none really relate to using an image and immediately reading that decoded data into OpenCV without the intermediary step of saving it to the harddrive (using the inverse process used to read the file in the "send" script).
I'm still pretty new to Python and OpenCV, so if there's a better encoding method to send the image - whatever solves the problem. How the image is sent is irrelevant, so long as I can read it in on the receiving end without saving it as a .jpg to disk.
Thanks!
python opencv numpy
python opencv numpy
asked Nov 4 '15 at 12:25
godfreap
93212
93212
2
useimdecode
instead ofimread
– Miki
Nov 4 '15 at 12:27
"buf is not a numpy array, neither a scalar" tried passing the b64decode step here, still got this error. so, still asking for a numpy array
– godfreap
Nov 4 '15 at 12:34
See if this helps. Then you need imdecode, because imread loads an image from disk.
– Miki
Nov 4 '15 at 12:38
1
probablynp.array(list(img), dtype=np.uint8)
ornp.fromstring(img, dtype=np.uint8)
will do the trick. But I don't know python. My advice is just that you need imdecode. How to get parameters type right it's up to you :D
– Miki
Nov 4 '15 at 12:55
1
Glad it helped. Thanks for the working block :D
– Miki
Nov 4 '15 at 13:07
|
show 5 more comments
2
useimdecode
instead ofimread
– Miki
Nov 4 '15 at 12:27
"buf is not a numpy array, neither a scalar" tried passing the b64decode step here, still got this error. so, still asking for a numpy array
– godfreap
Nov 4 '15 at 12:34
See if this helps. Then you need imdecode, because imread loads an image from disk.
– Miki
Nov 4 '15 at 12:38
1
probablynp.array(list(img), dtype=np.uint8)
ornp.fromstring(img, dtype=np.uint8)
will do the trick. But I don't know python. My advice is just that you need imdecode. How to get parameters type right it's up to you :D
– Miki
Nov 4 '15 at 12:55
1
Glad it helped. Thanks for the working block :D
– Miki
Nov 4 '15 at 13:07
2
2
use
imdecode
instead of imread
– Miki
Nov 4 '15 at 12:27
use
imdecode
instead of imread
– Miki
Nov 4 '15 at 12:27
"buf is not a numpy array, neither a scalar" tried passing the b64decode step here, still got this error. so, still asking for a numpy array
– godfreap
Nov 4 '15 at 12:34
"buf is not a numpy array, neither a scalar" tried passing the b64decode step here, still got this error. so, still asking for a numpy array
– godfreap
Nov 4 '15 at 12:34
See if this helps. Then you need imdecode, because imread loads an image from disk.
– Miki
Nov 4 '15 at 12:38
See if this helps. Then you need imdecode, because imread loads an image from disk.
– Miki
Nov 4 '15 at 12:38
1
1
probably
np.array(list(img), dtype=np.uint8)
or np.fromstring(img, dtype=np.uint8)
will do the trick. But I don't know python. My advice is just that you need imdecode. How to get parameters type right it's up to you :D– Miki
Nov 4 '15 at 12:55
probably
np.array(list(img), dtype=np.uint8)
or np.fromstring(img, dtype=np.uint8)
will do the trick. But I don't know python. My advice is just that you need imdecode. How to get parameters type right it's up to you :D– Miki
Nov 4 '15 at 12:55
1
1
Glad it helped. Thanks for the working block :D
– Miki
Nov 4 '15 at 13:07
Glad it helped. Thanks for the working block :D
– Miki
Nov 4 '15 at 13:07
|
show 5 more comments
2 Answers
2
active
oldest
votes
up vote
9
down vote
accepted
You can get a numpy array from you decoded data using:
import numpy as np
...
img = base64.b64decode(msg.payload)
npimg = np.fromstring(img, dtype=np.uint8)
Then you need imdecode to read the image from a buffer in memory. imread is meant to load an image from a file.
So:
import numpy as np
...
def on_message(client, userdata, msg): # msg.payload is incoming data
img = base64.b64decode(msg.payload);
npimg = np.fromstring(img, dtype=np.uint8);
source = cv2.imdecode(npimg, 1)
you may want to delete the very last line with cv2.imshow - it locks up when I do that. but that wasn't something critical to the program - it was just an intermediary step I inserted to make the code in the original question make more sense. the important point is that everything prior to that resolves the issue I was having, and now everything is fiiiiine! Thanks!
– godfreap
Nov 4 '15 at 13:09
ok thanks. You probably need cv2.waitKey(1) to see the imshowed image and avoid blocking.
– Miki
Nov 4 '15 at 13:11
yeah, I had that and it still locked up. the part of my program that interacts with this does essentially the same thing, and that's the 'new issue' I alluded to in the question's comments. I think it's just a data type issue and should be easy to resolve. thanks again! :D
– godfreap
Nov 4 '15 at 13:14
add a comment |
up vote
3
down vote
From the OpenCV documentation we can see that:
imread : Loads an image from a file.
imdecode : Reads an image from a buffer in memory.
Seem a better way to do what you want.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
You can get a numpy array from you decoded data using:
import numpy as np
...
img = base64.b64decode(msg.payload)
npimg = np.fromstring(img, dtype=np.uint8)
Then you need imdecode to read the image from a buffer in memory. imread is meant to load an image from a file.
So:
import numpy as np
...
def on_message(client, userdata, msg): # msg.payload is incoming data
img = base64.b64decode(msg.payload);
npimg = np.fromstring(img, dtype=np.uint8);
source = cv2.imdecode(npimg, 1)
you may want to delete the very last line with cv2.imshow - it locks up when I do that. but that wasn't something critical to the program - it was just an intermediary step I inserted to make the code in the original question make more sense. the important point is that everything prior to that resolves the issue I was having, and now everything is fiiiiine! Thanks!
– godfreap
Nov 4 '15 at 13:09
ok thanks. You probably need cv2.waitKey(1) to see the imshowed image and avoid blocking.
– Miki
Nov 4 '15 at 13:11
yeah, I had that and it still locked up. the part of my program that interacts with this does essentially the same thing, and that's the 'new issue' I alluded to in the question's comments. I think it's just a data type issue and should be easy to resolve. thanks again! :D
– godfreap
Nov 4 '15 at 13:14
add a comment |
up vote
9
down vote
accepted
You can get a numpy array from you decoded data using:
import numpy as np
...
img = base64.b64decode(msg.payload)
npimg = np.fromstring(img, dtype=np.uint8)
Then you need imdecode to read the image from a buffer in memory. imread is meant to load an image from a file.
So:
import numpy as np
...
def on_message(client, userdata, msg): # msg.payload is incoming data
img = base64.b64decode(msg.payload);
npimg = np.fromstring(img, dtype=np.uint8);
source = cv2.imdecode(npimg, 1)
you may want to delete the very last line with cv2.imshow - it locks up when I do that. but that wasn't something critical to the program - it was just an intermediary step I inserted to make the code in the original question make more sense. the important point is that everything prior to that resolves the issue I was having, and now everything is fiiiiine! Thanks!
– godfreap
Nov 4 '15 at 13:09
ok thanks. You probably need cv2.waitKey(1) to see the imshowed image and avoid blocking.
– Miki
Nov 4 '15 at 13:11
yeah, I had that and it still locked up. the part of my program that interacts with this does essentially the same thing, and that's the 'new issue' I alluded to in the question's comments. I think it's just a data type issue and should be easy to resolve. thanks again! :D
– godfreap
Nov 4 '15 at 13:14
add a comment |
up vote
9
down vote
accepted
up vote
9
down vote
accepted
You can get a numpy array from you decoded data using:
import numpy as np
...
img = base64.b64decode(msg.payload)
npimg = np.fromstring(img, dtype=np.uint8)
Then you need imdecode to read the image from a buffer in memory. imread is meant to load an image from a file.
So:
import numpy as np
...
def on_message(client, userdata, msg): # msg.payload is incoming data
img = base64.b64decode(msg.payload);
npimg = np.fromstring(img, dtype=np.uint8);
source = cv2.imdecode(npimg, 1)
You can get a numpy array from you decoded data using:
import numpy as np
...
img = base64.b64decode(msg.payload)
npimg = np.fromstring(img, dtype=np.uint8)
Then you need imdecode to read the image from a buffer in memory. imread is meant to load an image from a file.
So:
import numpy as np
...
def on_message(client, userdata, msg): # msg.payload is incoming data
img = base64.b64decode(msg.payload);
npimg = np.fromstring(img, dtype=np.uint8);
source = cv2.imdecode(npimg, 1)
edited Nov 4 '15 at 13:10
answered Nov 4 '15 at 13:06
Miki
29.1k851131
29.1k851131
you may want to delete the very last line with cv2.imshow - it locks up when I do that. but that wasn't something critical to the program - it was just an intermediary step I inserted to make the code in the original question make more sense. the important point is that everything prior to that resolves the issue I was having, and now everything is fiiiiine! Thanks!
– godfreap
Nov 4 '15 at 13:09
ok thanks. You probably need cv2.waitKey(1) to see the imshowed image and avoid blocking.
– Miki
Nov 4 '15 at 13:11
yeah, I had that and it still locked up. the part of my program that interacts with this does essentially the same thing, and that's the 'new issue' I alluded to in the question's comments. I think it's just a data type issue and should be easy to resolve. thanks again! :D
– godfreap
Nov 4 '15 at 13:14
add a comment |
you may want to delete the very last line with cv2.imshow - it locks up when I do that. but that wasn't something critical to the program - it was just an intermediary step I inserted to make the code in the original question make more sense. the important point is that everything prior to that resolves the issue I was having, and now everything is fiiiiine! Thanks!
– godfreap
Nov 4 '15 at 13:09
ok thanks. You probably need cv2.waitKey(1) to see the imshowed image and avoid blocking.
– Miki
Nov 4 '15 at 13:11
yeah, I had that and it still locked up. the part of my program that interacts with this does essentially the same thing, and that's the 'new issue' I alluded to in the question's comments. I think it's just a data type issue and should be easy to resolve. thanks again! :D
– godfreap
Nov 4 '15 at 13:14
you may want to delete the very last line with cv2.imshow - it locks up when I do that. but that wasn't something critical to the program - it was just an intermediary step I inserted to make the code in the original question make more sense. the important point is that everything prior to that resolves the issue I was having, and now everything is fiiiiine! Thanks!
– godfreap
Nov 4 '15 at 13:09
you may want to delete the very last line with cv2.imshow - it locks up when I do that. but that wasn't something critical to the program - it was just an intermediary step I inserted to make the code in the original question make more sense. the important point is that everything prior to that resolves the issue I was having, and now everything is fiiiiine! Thanks!
– godfreap
Nov 4 '15 at 13:09
ok thanks. You probably need cv2.waitKey(1) to see the imshowed image and avoid blocking.
– Miki
Nov 4 '15 at 13:11
ok thanks. You probably need cv2.waitKey(1) to see the imshowed image and avoid blocking.
– Miki
Nov 4 '15 at 13:11
yeah, I had that and it still locked up. the part of my program that interacts with this does essentially the same thing, and that's the 'new issue' I alluded to in the question's comments. I think it's just a data type issue and should be easy to resolve. thanks again! :D
– godfreap
Nov 4 '15 at 13:14
yeah, I had that and it still locked up. the part of my program that interacts with this does essentially the same thing, and that's the 'new issue' I alluded to in the question's comments. I think it's just a data type issue and should be easy to resolve. thanks again! :D
– godfreap
Nov 4 '15 at 13:14
add a comment |
up vote
3
down vote
From the OpenCV documentation we can see that:
imread : Loads an image from a file.
imdecode : Reads an image from a buffer in memory.
Seem a better way to do what you want.
add a comment |
up vote
3
down vote
From the OpenCV documentation we can see that:
imread : Loads an image from a file.
imdecode : Reads an image from a buffer in memory.
Seem a better way to do what you want.
add a comment |
up vote
3
down vote
up vote
3
down vote
From the OpenCV documentation we can see that:
imread : Loads an image from a file.
imdecode : Reads an image from a buffer in memory.
Seem a better way to do what you want.
From the OpenCV documentation we can see that:
imread : Loads an image from a file.
imdecode : Reads an image from a buffer in memory.
Seem a better way to do what you want.
edited Nov 4 '15 at 13:14
Miki
29.1k851131
29.1k851131
answered Nov 4 '15 at 12:41
A.H
876413
876413
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33521891%2ffrom-jpg-to-b64encode-to-cv2-imread%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
use
imdecode
instead ofimread
– Miki
Nov 4 '15 at 12:27
"buf is not a numpy array, neither a scalar" tried passing the b64decode step here, still got this error. so, still asking for a numpy array
– godfreap
Nov 4 '15 at 12:34
See if this helps. Then you need imdecode, because imread loads an image from disk.
– Miki
Nov 4 '15 at 12:38
1
probably
np.array(list(img), dtype=np.uint8)
ornp.fromstring(img, dtype=np.uint8)
will do the trick. But I don't know python. My advice is just that you need imdecode. How to get parameters type right it's up to you :D– Miki
Nov 4 '15 at 12:55
1
Glad it helped. Thanks for the working block :D
– Miki
Nov 4 '15 at 13:07