From JPG to b64encode to cv2.imread()











up vote
3
down vote

favorite
2












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!










share|improve this question


















  • 2




    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










  • 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) 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




    Glad it helped. Thanks for the working block :D
    – Miki
    Nov 4 '15 at 13:07















up vote
3
down vote

favorite
2












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!










share|improve this question


















  • 2




    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










  • 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) 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




    Glad it helped. Thanks for the working block :D
    – Miki
    Nov 4 '15 at 13:07













up vote
3
down vote

favorite
2









up vote
3
down vote

favorite
2






2





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!










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 4 '15 at 12:25









godfreap

93212




93212








  • 2




    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










  • 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) 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




    Glad it helped. Thanks for the working block :D
    – Miki
    Nov 4 '15 at 13:07














  • 2




    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










  • 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) 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




    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












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)





share|improve this answer























  • 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


















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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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)





    share|improve this answer























    • 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















    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)





    share|improve this answer























    • 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













    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)





    share|improve this answer














    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)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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


















    • 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












    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.






    share|improve this answer



























      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.






      share|improve this answer

























        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 4 '15 at 13:14









        Miki

        29.1k851131




        29.1k851131










        answered Nov 4 '15 at 12:41









        A.H

        876413




        876413






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)