cv2 imdecode returns none with base64 string












-1















When i try to convert base64 string into image i am getting none in CV2.
Following is my code



import cv2
import numpy as np

def bsae62toimage(imgvalue):
imge = "R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"

nparr = np.fromstring(imge,np.uint8)
print(nparr.shape)
img1 = cv2.imdecode(nparr,cv2.IMREAD_UNCHANGED)
print(img1.shape)


It is printing the numpy array but cv2.imdecode is returning None. Can you help me to find the problem in my code?



My Findings: np.fromstring it returns 1D array. I assume it should it should return 3D array. But i may be wrong.










share|improve this question























  • dear downvoters - leave your valuable comments

    – Backtrack
    Nov 26 '18 at 8:16
















-1















When i try to convert base64 string into image i am getting none in CV2.
Following is my code



import cv2
import numpy as np

def bsae62toimage(imgvalue):
imge = "R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"

nparr = np.fromstring(imge,np.uint8)
print(nparr.shape)
img1 = cv2.imdecode(nparr,cv2.IMREAD_UNCHANGED)
print(img1.shape)


It is printing the numpy array but cv2.imdecode is returning None. Can you help me to find the problem in my code?



My Findings: np.fromstring it returns 1D array. I assume it should it should return 3D array. But i may be wrong.










share|improve this question























  • dear downvoters - leave your valuable comments

    – Backtrack
    Nov 26 '18 at 8:16














-1












-1








-1








When i try to convert base64 string into image i am getting none in CV2.
Following is my code



import cv2
import numpy as np

def bsae62toimage(imgvalue):
imge = "R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"

nparr = np.fromstring(imge,np.uint8)
print(nparr.shape)
img1 = cv2.imdecode(nparr,cv2.IMREAD_UNCHANGED)
print(img1.shape)


It is printing the numpy array but cv2.imdecode is returning None. Can you help me to find the problem in my code?



My Findings: np.fromstring it returns 1D array. I assume it should it should return 3D array. But i may be wrong.










share|improve this question














When i try to convert base64 string into image i am getting none in CV2.
Following is my code



import cv2
import numpy as np

def bsae62toimage(imgvalue):
imge = "R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"

nparr = np.fromstring(imge,np.uint8)
print(nparr.shape)
img1 = cv2.imdecode(nparr,cv2.IMREAD_UNCHANGED)
print(img1.shape)


It is printing the numpy array but cv2.imdecode is returning None. Can you help me to find the problem in my code?



My Findings: np.fromstring it returns 1D array. I assume it should it should return 3D array. But i may be wrong.







linux python-3.x opencv base64 cv2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 5:22









BacktrackBacktrack

5,60533580




5,60533580













  • dear downvoters - leave your valuable comments

    – Backtrack
    Nov 26 '18 at 8:16



















  • dear downvoters - leave your valuable comments

    – Backtrack
    Nov 26 '18 at 8:16

















dear downvoters - leave your valuable comments

– Backtrack
Nov 26 '18 at 8:16





dear downvoters - leave your valuable comments

– Backtrack
Nov 26 '18 at 8:16












1 Answer
1






active

oldest

votes


















0














Since it is unclear as from where have you obtained the imge = "R0lGODlhEA..." variable. I would present an ideal flow for converting OpenCV image to base64 string and converting it back to OpenCV img as:



import cv2
import base64
import numpy as np


def to_base64(img):
_, buf = cv2.imencode(".png", img)

return base64.b64encode(buf)


def from_base64(buf):
buf_decode = base64.b64decode(buf)

buf_arr = np.fromstring(buf_decode, dtype=np.uint8)

return cv2.imdecode(buf_arr, cv2.IMREAD_UNCHANGED)


img = cv2.imread("/path/to/img.png")
img_base64 = to_base64(img)
img_decoded = from_base64(img_base64)

print img_decoded.shape


Also the documentation states that:




If the buffer is too short or contains invalid data, the empty
matrix/image is returned.




It seems to me that imge = "R0lGODlhEA..." is invalid.






share|improve this answer
























  • I am getting this string from JAVA. I am trying to integrate the model written in Tensorflow into the JAVA application. The JAVA application emits the image into base64 string

    – Backtrack
    Nov 26 '18 at 8:14











  • I think you should read the Java method documentation carefully and compare the results of Python and Java on same image, to see if base64 encoded data is consistent on both sides for a smaller image say 10x10. Also share the Java code for base64 conversion if possible

    – ZdaR
    Nov 26 '18 at 8:22













  • So, What i am doing is - I render a webpage using selenium and take the screenshot using following code - String imagecontent = null; if(screenshotenabled){ imagecontent = driver.getScreenshotAs(OutputType.BASE64); }

    – Backtrack
    Nov 26 '18 at 8:25











  • Also in the code you have given i made a small change - def to_base64(filepath): with open(filepath, "rb") as imageFile: bstr = base64.b64encode(imageFile.read()) return bstr when i run the script with this change it is giving me wrong size : (2184, 1280, 4).

    – Backtrack
    Nov 26 '18 at 8:32











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',
autoActivateHeartbeat: false,
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%2f53475135%2fcv2-imdecode-returns-none-with-base64-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Since it is unclear as from where have you obtained the imge = "R0lGODlhEA..." variable. I would present an ideal flow for converting OpenCV image to base64 string and converting it back to OpenCV img as:



import cv2
import base64
import numpy as np


def to_base64(img):
_, buf = cv2.imencode(".png", img)

return base64.b64encode(buf)


def from_base64(buf):
buf_decode = base64.b64decode(buf)

buf_arr = np.fromstring(buf_decode, dtype=np.uint8)

return cv2.imdecode(buf_arr, cv2.IMREAD_UNCHANGED)


img = cv2.imread("/path/to/img.png")
img_base64 = to_base64(img)
img_decoded = from_base64(img_base64)

print img_decoded.shape


Also the documentation states that:




If the buffer is too short or contains invalid data, the empty
matrix/image is returned.




It seems to me that imge = "R0lGODlhEA..." is invalid.






share|improve this answer
























  • I am getting this string from JAVA. I am trying to integrate the model written in Tensorflow into the JAVA application. The JAVA application emits the image into base64 string

    – Backtrack
    Nov 26 '18 at 8:14











  • I think you should read the Java method documentation carefully and compare the results of Python and Java on same image, to see if base64 encoded data is consistent on both sides for a smaller image say 10x10. Also share the Java code for base64 conversion if possible

    – ZdaR
    Nov 26 '18 at 8:22













  • So, What i am doing is - I render a webpage using selenium and take the screenshot using following code - String imagecontent = null; if(screenshotenabled){ imagecontent = driver.getScreenshotAs(OutputType.BASE64); }

    – Backtrack
    Nov 26 '18 at 8:25











  • Also in the code you have given i made a small change - def to_base64(filepath): with open(filepath, "rb") as imageFile: bstr = base64.b64encode(imageFile.read()) return bstr when i run the script with this change it is giving me wrong size : (2184, 1280, 4).

    – Backtrack
    Nov 26 '18 at 8:32
















0














Since it is unclear as from where have you obtained the imge = "R0lGODlhEA..." variable. I would present an ideal flow for converting OpenCV image to base64 string and converting it back to OpenCV img as:



import cv2
import base64
import numpy as np


def to_base64(img):
_, buf = cv2.imencode(".png", img)

return base64.b64encode(buf)


def from_base64(buf):
buf_decode = base64.b64decode(buf)

buf_arr = np.fromstring(buf_decode, dtype=np.uint8)

return cv2.imdecode(buf_arr, cv2.IMREAD_UNCHANGED)


img = cv2.imread("/path/to/img.png")
img_base64 = to_base64(img)
img_decoded = from_base64(img_base64)

print img_decoded.shape


Also the documentation states that:




If the buffer is too short or contains invalid data, the empty
matrix/image is returned.




It seems to me that imge = "R0lGODlhEA..." is invalid.






share|improve this answer
























  • I am getting this string from JAVA. I am trying to integrate the model written in Tensorflow into the JAVA application. The JAVA application emits the image into base64 string

    – Backtrack
    Nov 26 '18 at 8:14











  • I think you should read the Java method documentation carefully and compare the results of Python and Java on same image, to see if base64 encoded data is consistent on both sides for a smaller image say 10x10. Also share the Java code for base64 conversion if possible

    – ZdaR
    Nov 26 '18 at 8:22













  • So, What i am doing is - I render a webpage using selenium and take the screenshot using following code - String imagecontent = null; if(screenshotenabled){ imagecontent = driver.getScreenshotAs(OutputType.BASE64); }

    – Backtrack
    Nov 26 '18 at 8:25











  • Also in the code you have given i made a small change - def to_base64(filepath): with open(filepath, "rb") as imageFile: bstr = base64.b64encode(imageFile.read()) return bstr when i run the script with this change it is giving me wrong size : (2184, 1280, 4).

    – Backtrack
    Nov 26 '18 at 8:32














0












0








0







Since it is unclear as from where have you obtained the imge = "R0lGODlhEA..." variable. I would present an ideal flow for converting OpenCV image to base64 string and converting it back to OpenCV img as:



import cv2
import base64
import numpy as np


def to_base64(img):
_, buf = cv2.imencode(".png", img)

return base64.b64encode(buf)


def from_base64(buf):
buf_decode = base64.b64decode(buf)

buf_arr = np.fromstring(buf_decode, dtype=np.uint8)

return cv2.imdecode(buf_arr, cv2.IMREAD_UNCHANGED)


img = cv2.imread("/path/to/img.png")
img_base64 = to_base64(img)
img_decoded = from_base64(img_base64)

print img_decoded.shape


Also the documentation states that:




If the buffer is too short or contains invalid data, the empty
matrix/image is returned.




It seems to me that imge = "R0lGODlhEA..." is invalid.






share|improve this answer













Since it is unclear as from where have you obtained the imge = "R0lGODlhEA..." variable. I would present an ideal flow for converting OpenCV image to base64 string and converting it back to OpenCV img as:



import cv2
import base64
import numpy as np


def to_base64(img):
_, buf = cv2.imencode(".png", img)

return base64.b64encode(buf)


def from_base64(buf):
buf_decode = base64.b64decode(buf)

buf_arr = np.fromstring(buf_decode, dtype=np.uint8)

return cv2.imdecode(buf_arr, cv2.IMREAD_UNCHANGED)


img = cv2.imread("/path/to/img.png")
img_base64 = to_base64(img)
img_decoded = from_base64(img_base64)

print img_decoded.shape


Also the documentation states that:




If the buffer is too short or contains invalid data, the empty
matrix/image is returned.




It seems to me that imge = "R0lGODlhEA..." is invalid.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 6:53









ZdaRZdaR

13.4k33554




13.4k33554













  • I am getting this string from JAVA. I am trying to integrate the model written in Tensorflow into the JAVA application. The JAVA application emits the image into base64 string

    – Backtrack
    Nov 26 '18 at 8:14











  • I think you should read the Java method documentation carefully and compare the results of Python and Java on same image, to see if base64 encoded data is consistent on both sides for a smaller image say 10x10. Also share the Java code for base64 conversion if possible

    – ZdaR
    Nov 26 '18 at 8:22













  • So, What i am doing is - I render a webpage using selenium and take the screenshot using following code - String imagecontent = null; if(screenshotenabled){ imagecontent = driver.getScreenshotAs(OutputType.BASE64); }

    – Backtrack
    Nov 26 '18 at 8:25











  • Also in the code you have given i made a small change - def to_base64(filepath): with open(filepath, "rb") as imageFile: bstr = base64.b64encode(imageFile.read()) return bstr when i run the script with this change it is giving me wrong size : (2184, 1280, 4).

    – Backtrack
    Nov 26 '18 at 8:32



















  • I am getting this string from JAVA. I am trying to integrate the model written in Tensorflow into the JAVA application. The JAVA application emits the image into base64 string

    – Backtrack
    Nov 26 '18 at 8:14











  • I think you should read the Java method documentation carefully and compare the results of Python and Java on same image, to see if base64 encoded data is consistent on both sides for a smaller image say 10x10. Also share the Java code for base64 conversion if possible

    – ZdaR
    Nov 26 '18 at 8:22













  • So, What i am doing is - I render a webpage using selenium and take the screenshot using following code - String imagecontent = null; if(screenshotenabled){ imagecontent = driver.getScreenshotAs(OutputType.BASE64); }

    – Backtrack
    Nov 26 '18 at 8:25











  • Also in the code you have given i made a small change - def to_base64(filepath): with open(filepath, "rb") as imageFile: bstr = base64.b64encode(imageFile.read()) return bstr when i run the script with this change it is giving me wrong size : (2184, 1280, 4).

    – Backtrack
    Nov 26 '18 at 8:32

















I am getting this string from JAVA. I am trying to integrate the model written in Tensorflow into the JAVA application. The JAVA application emits the image into base64 string

– Backtrack
Nov 26 '18 at 8:14





I am getting this string from JAVA. I am trying to integrate the model written in Tensorflow into the JAVA application. The JAVA application emits the image into base64 string

– Backtrack
Nov 26 '18 at 8:14













I think you should read the Java method documentation carefully and compare the results of Python and Java on same image, to see if base64 encoded data is consistent on both sides for a smaller image say 10x10. Also share the Java code for base64 conversion if possible

– ZdaR
Nov 26 '18 at 8:22







I think you should read the Java method documentation carefully and compare the results of Python and Java on same image, to see if base64 encoded data is consistent on both sides for a smaller image say 10x10. Also share the Java code for base64 conversion if possible

– ZdaR
Nov 26 '18 at 8:22















So, What i am doing is - I render a webpage using selenium and take the screenshot using following code - String imagecontent = null; if(screenshotenabled){ imagecontent = driver.getScreenshotAs(OutputType.BASE64); }

– Backtrack
Nov 26 '18 at 8:25





So, What i am doing is - I render a webpage using selenium and take the screenshot using following code - String imagecontent = null; if(screenshotenabled){ imagecontent = driver.getScreenshotAs(OutputType.BASE64); }

– Backtrack
Nov 26 '18 at 8:25













Also in the code you have given i made a small change - def to_base64(filepath): with open(filepath, "rb") as imageFile: bstr = base64.b64encode(imageFile.read()) return bstr when i run the script with this change it is giving me wrong size : (2184, 1280, 4).

– Backtrack
Nov 26 '18 at 8:32





Also in the code you have given i made a small change - def to_base64(filepath): with open(filepath, "rb") as imageFile: bstr = base64.b64encode(imageFile.read()) return bstr when i run the script with this change it is giving me wrong size : (2184, 1280, 4).

– Backtrack
Nov 26 '18 at 8:32




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475135%2fcv2-imdecode-returns-none-with-base64-string%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

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga