forked from nlintz/TensorFlow-Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_BytesIO.py
60 lines (48 loc) · 1.51 KB
/
ex_BytesIO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# http://stackoverflow.com/questions/26879981/writing-then-reading-in-memory-bytes-bytesio-gives-a-blank-result
"""
I tried writing to a zip file in memory, and then reading the bytes back out of that zip file.
So instead of passing in a file-object to gzip, I pass in a BytesIO object.
Here is the entire script:
"""
from io import BytesIO
import gzip
# write bytes to zip file in memory
myio = BytesIO()
g = gzip.GzipFile(fileobj=myio, mode='wb')
g.write(b"does it work")
g.close()
# read bytes from zip file in memory
myio.seek(0)
g = gzip.GzipFile(fileobj=myio, mode='rb')
result = g.read()
g.close()
print(result)
# But it is returning an empty bytes object for result
# need to seek back to the beginning of the file after writing the initial in memory file...
# myio.seek(0)
# np.ndarray.
# dumps() Returns the pickle of the array as a string.
# tobytes([order]) Construct Python bytes containing the raw data bytes in the array.
# tostring([order]) Construct Python bytes containing the raw data bytes in the array.
# Do the simplest possible python code to load your image, e.g.:
"""
from PIL import Image
im = Image.open('test.png')
# or
from PIL import Image
import io
with open('test.png') as f:
io = io.BytesIO(f.read())
im = Image.open(io)
"""
from PIL import Image
#from io import BytesIO
# First, load the image again
filename = "brain_left_right.jpg"
#=== non-tf test
ms = BytesIO()
bytes = Image.open(filename) #xxx.tobytes()
bytes.save(ms, format = "JPEG")
ms.flush()
ms.seek(0)
im=Image.open(ms) # <- Error here