Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

saving to PIL image to s3

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()
Comment

save PIL image to s3

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
        )
Comment

PREVIOUS NEXT
Code Example
Python :: python generate c array 
Python :: Plot Multiple ROC Curves in Python 
Python :: Location of INSTALLED_APP and MIDDLEWARE 
Python :: isclass function in python xml 
Python :: odoo get inherited models 
Python :: immutabledict working 
Python :: pandas get most occurring value for each id 
Python :: how to strip characters in python 
Python :: python error bars 
Python :: pandas assign multiple columns 
Python :: Get First From Table Django 
Python :: Problem With This? 
Python :: hashmap in python 
Python :: Python List insert() add element at designated place 
Python :: emi calculator python code 
Python :: python basic programs quadratic equation 
Python :: python created nested directory 
Python :: python round function 
Python :: python import only one function 
Python :: django rotatingfilehandler 
Python :: how to install pandas in python by git 
Python :: voting classifier grid search 
Python :: can 2020 get any worse 
Python :: Seaborn boxplots shifted incorrectly along x-axis 
Python :: student notebook (finish), INB (finish), Food and Fitness log (log necessary), debate speech (finish) 
Python :: numpy generate sequence from 0 to n 
Python :: py random.smple 
Python :: adding new character in string python 
Python :: paraphrase tool free online 
Python :: how to hide a specific file in python 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =