I am trying to convert a ndarray
of shape (h,w,c)
to a file-like object with read operation.
The idea is to have the file in memory and send it as a parameter to a AWS S3 bucket.
Here's an example of the code:
def arr_to_file(ndarray, key, bucket):
img = Image.fromarray(ndarray)
s3.upload_fileobj(
img, # The problem is here. Image does not implement a read method
bucket,
key
)
ndarray sample:
[[[135 114 177]
[123 131 174]
[111 138 179]
...
[130 111 170]
[139 127 155]
[144 124 143]]
[[125 133 182]
[119 133 182]
[104 148 182]
...
[129 118 165]
[142 116 160]
[145 112 155]]
[[125 151 186]
[115 145 187]
[ 96 154 185]
...
[105 125 160]
[123 109 163]
[117 127 161]]
...
[[ 97 124 127]
[113 119 129]
[124 111 141]
...
[ 74 110 85]
[ 63 94 96]
[ 65 85 105]]
[[116 102 124]
[116 128 117]
[119 115 154]
...
[ 82 80 95]
[ 85 89 95]
[ 85 89 97]]
[[113 131 116]
[107 155 108]
[114 130 151]
...
[ 88 39 102]
[105 75 98]
[100 91 97]]]
Any suggestions?
Edit (got this to work with ndarrays by changing save/load functions):
Numpy has a built in function to save and load to save .npy
files. The following worked for me:
>>> import numpy as np
>>> a = np.random.rand(2, 2, 2)
>>> np.save("test", a)
>>> g = np.load("test.npy")
Edit again:
>>> from tempfile import TemporaryFile
>>> outfile = TemporaryFile()
>>> x = np.arange(10)
>>> np.save(outfile, x)
>> #send outfile to S3 somehow...
>>> _ = outfile.seek(0) # Only needed here to simulate closing & reopening file
>>> f = np.load(outfile)
Figured it out, here's what I did:
img = Image.fromarray(ndarray)
img_obj = io.BytesIO()
img.save(img_obj, format="jpeg")
img_obj.seek(0)
This creates the img_obj
in memory which can be passed to the S3 Bucket.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.