1

Am currently accessing a s3 bucket from my school system.
To connect, I used the following:

import s3fs
from skimage import exposure
from PIL import Image, ImageStat

s3 = s3fs.S3FileSystem(client_kwargs={'endpoint_url': 'XXX'},
                       key='XXX',
                       secret='XXX')

I can retrieve an image from the s3 bucket as defined above and preprocess them using

infile = s3.open('test.jpg',"rb")
image = Image.open(infile)
img = np.asarray(image) #numpy.ndarray
img_eq = exposure.equalize_adapthist(img,clip_limit=0.03) #CLAHE
image_eq = Image.fromarray((img_eq * 255).astype(np.uint8)) #Convert back to image

To save the resulting image <image_eq> locally, would just be

image_eq.save("hello.jpg")

However, how do I save/write the resulting image into the s3fs filesystem instead?

Andrew Gaul
  • 2,296
  • 1
  • 12
  • 19
Andy
  • 191
  • 10

1 Answers1

0

save in Pillow accepts a file too. You could do:

 image_eq.save(fs.open('s3://bucket/file.png', 'wb'), 'PNG')

You have to write a binary file. I think it works best by enforcing the file type, e.g. in this case PNG.

Simone
  • 142
  • 1
  • 1
  • 11