I have a model named "Post", which is going to refer to images and videos. I added ImageSpecField for thumbnail storage and created a function, that pulls the desired frame from uploaded video. Is there any way to use this function while generating thumbnail? Because right now ImageSpecField can only use FileField as an input.
I have tried creating a new class inheriting from ImageSpecField, but I quickly realized that this is not going to work because this class was instanced only on server start, thus putting this function in constructor of it would not work.
import cv2 as cv
from django.conf import settings
from django.db import models
from imagekit.processors import ResizeToFit
from imagekit.models import ImageSpecField
def video_to_image(source, frame):
vid_cap = cv.VideoCapture(settings.MEDIA_ROOT + source.__str__())
vid_cap.set(cv.CAP_PROP_POS_FRAMES, frame)
success, image = vid_cap.read()
vid_cap.release()
return image
class Post(models.Model):
IMAGE = 'I'
VIDEO = 'V'
FILE_TYPES = [
(IMAGE, 'Image'),
(VIDEO, 'Video')
]
file_type = models.CharField(max_length=1, choices=FILE_TYPES)
file = models.FileField(upload_to='post_images')
thumbnail_frame = models.IntegerField(default=0)
image_thumbnail = ImageSpecField(source='file',
processors=[ResizeToFit(width=200, height=200)],
format='JPEG',
options={'quality': 60})
I want imagekit to generate thumbnail from video, and be able to get it via ImageSpecField.