TypeError: expected bytes-like object, not str





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I know this question has been asked much times, but please look once in my problem.



I am sending base64 image data from angular to python flask but when I am processing that base64 data on flask server(python3) then it is giving me the error




TypeError: expected bytes-like object, not str




my Javascript code is:



window['__CANVAS'].toDataURL("image/png");



Output of the above line is:



"data:image/png;base64,iVBORw0KGgoAAAANSUhEUg....."



I am receiving same data on flask server as string.



Code on python server that is using above base 64 data is:



def convert_to_image(base64_code):
image_64_decode = base64.decodebytes(base64_code)
image_result = open('baseimage.jpg', 'wb')
image_result.write(image_64_decode)
img_rgb = cv2.imread('baseimage.jpg')
return img_rgb


then it is giving the following error trace:



File "/home/shubham/py-projects/DX/Web/app/base64toimage.py", line 10, in convert_to_image    
image_64_decode = base64.decodebytes(base64_code)
File "/usr/lib/python3.5/base64.py", line 552, in decodebytes
_input_type_check(s)
File "/usr/lib/python3.5/base64.py", line 521, in _input_type_check
raise TypeError(msg) from err
TypeError: expected bytes-like object, not str


above python function working fine if I am converting the image using this function



import base64

with open("t.png", "rb") as imageFile:
str = base64.b64encode(imageFile.read())
print str


please help me to solve this question? I am new to python.










share|improve this question




















  • 1





    b64encode returns bytes, that's why decodebytes works, because it is expecting bytes, not a string (as the error message clearly states). See: docs.python.org/3/library/base64.html To solve your problem, convert base64_code to bytes or pass a byte-like-object to the function instead of a string. Side note: you have successfully overriden the built-in str with str = base64.b64encode(... - don't do that!

    – Mike Scotty
    Nov 16 '18 at 15:21




















2















I know this question has been asked much times, but please look once in my problem.



I am sending base64 image data from angular to python flask but when I am processing that base64 data on flask server(python3) then it is giving me the error




TypeError: expected bytes-like object, not str




my Javascript code is:



window['__CANVAS'].toDataURL("image/png");



Output of the above line is:



"data:image/png;base64,iVBORw0KGgoAAAANSUhEUg....."



I am receiving same data on flask server as string.



Code on python server that is using above base 64 data is:



def convert_to_image(base64_code):
image_64_decode = base64.decodebytes(base64_code)
image_result = open('baseimage.jpg', 'wb')
image_result.write(image_64_decode)
img_rgb = cv2.imread('baseimage.jpg')
return img_rgb


then it is giving the following error trace:



File "/home/shubham/py-projects/DX/Web/app/base64toimage.py", line 10, in convert_to_image    
image_64_decode = base64.decodebytes(base64_code)
File "/usr/lib/python3.5/base64.py", line 552, in decodebytes
_input_type_check(s)
File "/usr/lib/python3.5/base64.py", line 521, in _input_type_check
raise TypeError(msg) from err
TypeError: expected bytes-like object, not str


above python function working fine if I am converting the image using this function



import base64

with open("t.png", "rb") as imageFile:
str = base64.b64encode(imageFile.read())
print str


please help me to solve this question? I am new to python.










share|improve this question




















  • 1





    b64encode returns bytes, that's why decodebytes works, because it is expecting bytes, not a string (as the error message clearly states). See: docs.python.org/3/library/base64.html To solve your problem, convert base64_code to bytes or pass a byte-like-object to the function instead of a string. Side note: you have successfully overriden the built-in str with str = base64.b64encode(... - don't do that!

    – Mike Scotty
    Nov 16 '18 at 15:21
















2












2








2








I know this question has been asked much times, but please look once in my problem.



I am sending base64 image data from angular to python flask but when I am processing that base64 data on flask server(python3) then it is giving me the error




TypeError: expected bytes-like object, not str




my Javascript code is:



window['__CANVAS'].toDataURL("image/png");



Output of the above line is:



"data:image/png;base64,iVBORw0KGgoAAAANSUhEUg....."



I am receiving same data on flask server as string.



Code on python server that is using above base 64 data is:



def convert_to_image(base64_code):
image_64_decode = base64.decodebytes(base64_code)
image_result = open('baseimage.jpg', 'wb')
image_result.write(image_64_decode)
img_rgb = cv2.imread('baseimage.jpg')
return img_rgb


then it is giving the following error trace:



File "/home/shubham/py-projects/DX/Web/app/base64toimage.py", line 10, in convert_to_image    
image_64_decode = base64.decodebytes(base64_code)
File "/usr/lib/python3.5/base64.py", line 552, in decodebytes
_input_type_check(s)
File "/usr/lib/python3.5/base64.py", line 521, in _input_type_check
raise TypeError(msg) from err
TypeError: expected bytes-like object, not str


above python function working fine if I am converting the image using this function



import base64

with open("t.png", "rb") as imageFile:
str = base64.b64encode(imageFile.read())
print str


please help me to solve this question? I am new to python.










share|improve this question
















I know this question has been asked much times, but please look once in my problem.



I am sending base64 image data from angular to python flask but when I am processing that base64 data on flask server(python3) then it is giving me the error




TypeError: expected bytes-like object, not str




my Javascript code is:



window['__CANVAS'].toDataURL("image/png");



Output of the above line is:



"data:image/png;base64,iVBORw0KGgoAAAANSUhEUg....."



I am receiving same data on flask server as string.



Code on python server that is using above base 64 data is:



def convert_to_image(base64_code):
image_64_decode = base64.decodebytes(base64_code)
image_result = open('baseimage.jpg', 'wb')
image_result.write(image_64_decode)
img_rgb = cv2.imread('baseimage.jpg')
return img_rgb


then it is giving the following error trace:



File "/home/shubham/py-projects/DX/Web/app/base64toimage.py", line 10, in convert_to_image    
image_64_decode = base64.decodebytes(base64_code)
File "/usr/lib/python3.5/base64.py", line 552, in decodebytes
_input_type_check(s)
File "/usr/lib/python3.5/base64.py", line 521, in _input_type_check
raise TypeError(msg) from err
TypeError: expected bytes-like object, not str


above python function working fine if I am converting the image using this function



import base64

with open("t.png", "rb") as imageFile:
str = base64.b64encode(imageFile.read())
print str


please help me to solve this question? I am new to python.







javascript python base64 angular6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 15:38









Idlehands

6,1631923




6,1631923










asked Nov 16 '18 at 15:17









Shubham BatraShubham Batra

1,31421337




1,31421337








  • 1





    b64encode returns bytes, that's why decodebytes works, because it is expecting bytes, not a string (as the error message clearly states). See: docs.python.org/3/library/base64.html To solve your problem, convert base64_code to bytes or pass a byte-like-object to the function instead of a string. Side note: you have successfully overriden the built-in str with str = base64.b64encode(... - don't do that!

    – Mike Scotty
    Nov 16 '18 at 15:21
















  • 1





    b64encode returns bytes, that's why decodebytes works, because it is expecting bytes, not a string (as the error message clearly states). See: docs.python.org/3/library/base64.html To solve your problem, convert base64_code to bytes or pass a byte-like-object to the function instead of a string. Side note: you have successfully overriden the built-in str with str = base64.b64encode(... - don't do that!

    – Mike Scotty
    Nov 16 '18 at 15:21










1




1





b64encode returns bytes, that's why decodebytes works, because it is expecting bytes, not a string (as the error message clearly states). See: docs.python.org/3/library/base64.html To solve your problem, convert base64_code to bytes or pass a byte-like-object to the function instead of a string. Side note: you have successfully overriden the built-in str with str = base64.b64encode(... - don't do that!

– Mike Scotty
Nov 16 '18 at 15:21







b64encode returns bytes, that's why decodebytes works, because it is expecting bytes, not a string (as the error message clearly states). See: docs.python.org/3/library/base64.html To solve your problem, convert base64_code to bytes or pass a byte-like-object to the function instead of a string. Side note: you have successfully overriden the built-in str with str = base64.b64encode(... - don't do that!

– Mike Scotty
Nov 16 '18 at 15:21














1 Answer
1






active

oldest

votes


















0














base64.decodebytes only accepts byte arrays, use base64.b64decode instead it accepts Strings as well






share|improve this answer
























  • by changing this I am getting new error hsv_img = cv2.cvtColor(image,cv2.COLOR_BGR2HSV) cv2.error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

    – Shubham Batra
    Nov 16 '18 at 19:01











  • stackoverflow.com/a/33522724/5048815

    – wiomoc
    Nov 16 '18 at 23:25












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%2f53340627%2ftypeerror-expected-bytes-like-object-not-str%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














base64.decodebytes only accepts byte arrays, use base64.b64decode instead it accepts Strings as well






share|improve this answer
























  • by changing this I am getting new error hsv_img = cv2.cvtColor(image,cv2.COLOR_BGR2HSV) cv2.error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

    – Shubham Batra
    Nov 16 '18 at 19:01











  • stackoverflow.com/a/33522724/5048815

    – wiomoc
    Nov 16 '18 at 23:25
















0














base64.decodebytes only accepts byte arrays, use base64.b64decode instead it accepts Strings as well






share|improve this answer
























  • by changing this I am getting new error hsv_img = cv2.cvtColor(image,cv2.COLOR_BGR2HSV) cv2.error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

    – Shubham Batra
    Nov 16 '18 at 19:01











  • stackoverflow.com/a/33522724/5048815

    – wiomoc
    Nov 16 '18 at 23:25














0












0








0







base64.decodebytes only accepts byte arrays, use base64.b64decode instead it accepts Strings as well






share|improve this answer













base64.decodebytes only accepts byte arrays, use base64.b64decode instead it accepts Strings as well







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 15:27









wiomocwiomoc

487512




487512













  • by changing this I am getting new error hsv_img = cv2.cvtColor(image,cv2.COLOR_BGR2HSV) cv2.error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

    – Shubham Batra
    Nov 16 '18 at 19:01











  • stackoverflow.com/a/33522724/5048815

    – wiomoc
    Nov 16 '18 at 23:25



















  • by changing this I am getting new error hsv_img = cv2.cvtColor(image,cv2.COLOR_BGR2HSV) cv2.error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

    – Shubham Batra
    Nov 16 '18 at 19:01











  • stackoverflow.com/a/33522724/5048815

    – wiomoc
    Nov 16 '18 at 23:25

















by changing this I am getting new error hsv_img = cv2.cvtColor(image,cv2.COLOR_BGR2HSV) cv2.error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

– Shubham Batra
Nov 16 '18 at 19:01





by changing this I am getting new error hsv_img = cv2.cvtColor(image,cv2.COLOR_BGR2HSV) cv2.error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

– Shubham Batra
Nov 16 '18 at 19:01













stackoverflow.com/a/33522724/5048815

– wiomoc
Nov 16 '18 at 23:25





stackoverflow.com/a/33522724/5048815

– wiomoc
Nov 16 '18 at 23:25




















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%2f53340627%2ftypeerror-expected-bytes-like-object-not-str%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

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues