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 :: page views count django 
Python :: get out of a help screen python 
Python :: get column means pandas 
Python :: python keyword search engine 
Python :: create a number of variables based on input in python 
Python :: Draw GFG Geeks For Geeks Logo using Python and Turtle 
Python :: is c++ easier than python 
Python :: How to convert string to uppercase, lowercase and how to swapcase in python 
Python :: python backtest 
Python :: pandas add mutliple columns 
Python :: how to print tic tac toe border on terminal in python 
Python :: While Loop Python Range Staying Constant Despite Shrinking List 
Python :: csv python 
Python :: json object type in python 
Python :: python run only when list is bigger 
Python :: python advanced programs time 
Python :: python Pyramid Patterns half 
Python :: django create view template 
Python :: problem 1 dot product python 
Python :: django multi column index 
Python :: is dictreader scoped in python 
Python :: data parsing app python 
Python :: scatter plot actual vs predicted python 
Python :: python codes and answers cheat code pdf 
Python :: python site-packages pyspark 
Python :: Syntax Closing a File in python 
Python :: py random sample 
Python :: how to convert ordereddict to dict in python 
Python :: how to make a timer in pyothn 
Python :: plotly scroll zoom 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =