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;
}
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
add a comment |
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
1
b64encode
returnsbytes
, that's whydecodebytes
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, convertbase64_code
to bytes or pass a byte-like-object to the function instead of a string. Side note: you have successfully overriden the built-instr
withstr = base64.b64encode(...
- don't do that!
– Mike Scotty
Nov 16 '18 at 15:21
add a comment |
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
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
javascript python base64 angular6
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
returnsbytes
, that's whydecodebytes
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, convertbase64_code
to bytes or pass a byte-like-object to the function instead of a string. Side note: you have successfully overriden the built-instr
withstr = base64.b64encode(...
- don't do that!
– Mike Scotty
Nov 16 '18 at 15:21
add a comment |
1
b64encode
returnsbytes
, that's whydecodebytes
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, convertbase64_code
to bytes or pass a byte-like-object to the function instead of a string. Side note: you have successfully overriden the built-instr
withstr = 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
add a comment |
1 Answer
1
active
oldest
votes
base64.decodebytes
only accepts byte arrays, use base64.b64decode
instead it accepts Strings as well
by changing this I am getting new errorhsv_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
add a comment |
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
});
}
});
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%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
base64.decodebytes
only accepts byte arrays, use base64.b64decode
instead it accepts Strings as well
by changing this I am getting new errorhsv_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
add a comment |
base64.decodebytes
only accepts byte arrays, use base64.b64decode
instead it accepts Strings as well
by changing this I am getting new errorhsv_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
add a comment |
base64.decodebytes
only accepts byte arrays, use base64.b64decode
instead it accepts Strings as well
base64.decodebytes
only accepts byte arrays, use base64.b64decode
instead it accepts Strings as well
answered Nov 16 '18 at 15:27
wiomocwiomoc
487512
487512
by changing this I am getting new errorhsv_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
add a comment |
by changing this I am getting new errorhsv_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
add a comment |
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.
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%2f53340627%2ftypeerror-expected-bytes-like-object-not-str%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
1
b64encode
returnsbytes
, that's whydecodebytes
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, convertbase64_code
to bytes or pass a byte-like-object to the function instead of a string. Side note: you have successfully overriden the built-instr
withstr = base64.b64encode(...
- don't do that!– Mike Scotty
Nov 16 '18 at 15:21