I have a model for image url in django. I want to calculate the image_height and iamge_width of image which is stored in s3 bucket.Below is my model.py
from django.db import models
from .project_model import Project
class Image(models.Model):
image = models.TextField(blank=False)
image_width = models.IntegerField(default=0)
image_height = models.IntegerField(default=0)
project_id = models.ForeignKey(Project, on_delete=models.CASCADE)
is_thumpnail = models.BooleanField(blank=False)
upload_on = models.DateTimeField("created on", auto_now_add=True)
updated_on = models.DateTimeField("updated on", auto_now=True)
def __str__(self):
return self.file_name
After getting the value of height and width, I want to store the value in the model.py.
I am looking for below type of response example
{
"id": "cJ9cm",
"title": null,
"description": null,
"datetime": 1357856330,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 544702,
"views": 31829,
"bandwidth": 17337319958,
"link": "https://i.imgur.com/cJ9cm.jpg"
}
If you don't have the image I am not sure how you can do that if you had the image it's like this in Pillow:
from PIL import Image
im = Image.open('some_image.png')
width, height = im.size
also, I was wondering how you create this models instances? If you are using a lambda function that is triggered by s3 you can create these instances with image information quite easily, in that lambda function you can call your backend and pass the necessary fields.
You can use boto3 to get that image from s3 btw, that should be easier way. Also maybe using an image field might be better for your case: docs
you need to configure your storages as explained here
Once you have these dependencies installed you can now get that image from s3, boto3 has all the functions you need. Create a client to operate on s3
import boto3
s3_client = boto3.client(
's3',
aws_access_key_id="",
aws_secret_access_key="",
region_name="",
)