Issue
The following code is trying to convert the image
array to string
, and then regenerate the image
from string
.
import numpy as np
import matplotlib.pyplot as plt
image = np.ones((28,28))
print("image: {}".format(image.shape))
string = "{}".format(image.tostring())
print("len: {}".format(len(string)))
string_to_image = np.fromstring(string)
print("string_to_image: {}".format(string_to_image.shape))
Here is the output:
image: (28, 28)
len: 22739
/tmp/ipykernel_2794/3542140279.py:7: DeprecationWarning: tostring() is deprecated. Use tobytes() instead.
string = "{}".format(image.tostring())
/tmp/ipykernel_2794/3542140279.py:10: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
string_to_image = np.fromstring(string)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_2794/3542140279.py in <cell line: 10>()
8 print("len: {}".format(len(string)))
9
---> 10 string_to_image = np.fromstring(string)
11 print("string_to_image: {}".format(string_to_image.shape))
ValueError: string size must be a multiple of element size
How to fix the error?
Solution
Check Below code, need to use right datatype dtype=np.uint8
string_to_image = np.fromstring(string, dtype=np.uint8)
Answered By - Abhishek
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.