from PIL import Image
import io
def modify_image(image, format):
pil_image = Image.open(image)
# Prints out (1280, 960)
print(pil_image.size)
in_mem_file = io.BytesIO()
# format here would be something like "JPEG". See below link for more info.
pil_image.save(in_mem_file, format=format)
return in_mem_file.getvalue()
if self.image:
m = storage.open(self.image.name)
obj_location = 'media/%s' % self.image.name
# img = Image.open(self.image)
session = boto3.Session()
s3_session = session.client(
"s3", aws_access_key_id=settings.AWS_S3_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_S3_SECRET_ACCESS_KEY
)
img = Image.open(m)
if img.height > 200 or img.width > 200:
output_size = (200, 200)
img.resize(output_size)
img.thumbnail(output_size)
img.convert('RGB')
sfile = io.BytesIO()
img.save(sfile, format='JPEG')
# im.seek(0)
value = sfile.getvalue()
# buffer = sfile.getbuffer()
md = hashlib.md5(value).digest()
img_md5 = base64.b64encode(md).decode('utf-8')
s3_session.put_object(
ContentMD5=img_md5,
Body=sfile,
Bucket=settings.AWS_STORAGE_BUCKET_NAME,
Key=self.image.name
)