Last active
March 14, 2020 19:52
-
-
Save alukach/7266c77e9990307e492516a6b8990c63 to your computer and use it in GitHub Desktop.
An example of a PIL-friendly object to represent an S3 file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import IO | |
from dataclasses import dataclass, field | |
from io import BytesIO | |
import boto3 | |
from PIL import Image | |
s3 = boto3.resource("s3") | |
@dataclass | |
class S3File: | |
bucket: str | |
key: str | |
io: IO = field(init=False) | |
def __post_init__(self): | |
self.io = BytesIO() | |
@property | |
def s3_object(self): | |
return s3.Object(self.bucket, self.key) | |
@property | |
def write(self): | |
return self.io.write | |
def read(self): | |
return self.s3_object.get()["Body"].read() | |
def flush(self): | |
""" After writing to a file, PIL will run 'flush' if available """ | |
self.io.seek(0) | |
return self.s3_object.put(Body=self.io) | |
if __name__ == "__main__": | |
bucket = "my-bucket" | |
src_file = S3File(bucket, "planet/PSScene4Band-20180112_105605_102c/thumb.png") | |
dst_file = S3File(bucket, "planet/PSScene4Band-20180112_105605_102c/thumb-cropped.png") | |
# Do some PIL things... | |
src = Image.open(src_file) | |
src.crop(src.getbbox()).save(dst_file, format=src.format) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment