django-imagekit
django-imagekit copied to clipboard
How to create several thumbs, which are created while saving model?
Say, i have ImageFiled that stored URL in DB and file on hdd.
img = ProcessedImageField(storage=photo_fs,
processors=[Transpose(Transpose.AUTO),
ResizeToFit(1280, 960, False),
ResizeCanvas(1280, 960, anchor=Anchor.CENTER)
],
format='JPEG',
options={'quality': 75})
how can i create second (third, etc) thumb while saving model? something like a:
img_640_480 = ProcessedImageField(source="img",
processors=[ResizeToFit(640, 480, False),],
file_name=source_file_name_wo_ext+"_640-480.jpg"
)
thanks!
up. ups, i want img_640_480 not from img. because img already have canvas.. I want to create img_640_480 from uploaded source file.
img_640_480 = ProcessedImageField(source="img",
processors=[ResizeToFit(640, 480, False),],
cachefile_strategy='imagekit.cachefiles.strategies.Optimistic')
hmm, ProcessedImageField TypeError: init() got an unexpected keyword argument 'source'
Now, i just using several ProcessedImageField (without "source') and copy request.data.get('img') to each of them when saving. It is ugly, but it is work.
show me full your code please, with forms, models and view. Your solution with request.data.get('img') are ugly
first of all, i think ProcessedImageField has no "source" argument.
class ProcessedImageField(ImageField, SpecHost):
It is in ImageSpecField, not in SpecHostField.
How it works now:
class Photo(models.Model):
img = ProcessedImageField(storage=photo_fs,
processors=[Transpose(Transpose.AUTO),
ResizeToFit(1280, 960, False),
ResizeCanvas(1280, 960, anchor=Anchor.CENTER)
],
format='JPEG',
options={'quality': 75})
img_640_480 = ProcessedImageField(storage=photo_fs,
processors=[Transpose(Transpose.AUTO),
ResizeToFit(640, 480, False),
ResizeCanvas(640, 480, anchor=Anchor.CENTER)
],
format='JPEG',
options={'quality': 75})
I have no form and views, because i use Django Rest Framework with js frontend.
DRF class view
class PhotosList(generics.ListAPIView, generics.CreateAPIView):
def perform_create(self, serializer):
serializer.save(owner=self.request.user,
img=self.request.data.get('img'),
img_640_480=self.request.data.get('img'),
img_240_180=self.request.data.get('img'),
img_140_100=self.request.data.get('img'))
Sorry for my typo, you need ImageSpecField for thumbs, and ProcessedImageField for original images
class Photo(models.Model):
img = ProcessedImageField(storage=photo_fs,
processors=[Transpose(Transpose.AUTO),
ResizeToFit(1280, 960, False),
ResizeCanvas(1280, 960, anchor=Anchor.CENTER)
],
format='JPEG',
options={'quality': 75})
img_640_480 = ImageSpecField (source='img',
processors=[Transpose(Transpose.AUTO),
ResizeToFit(640, 480, False),
ResizeCanvas(640, 480, anchor=Anchor.CENTER)
],
format='JPEG',
options={'quality': 75})
see more docs here https://django-imagekit.readthedocs.org/en/latest/#defining-specs-in-models
i've read this docs. I have two question -
- how can i create ImageSpecField with storage attribute to store second image for example on other server (not to MEDIA dir)
- I've try to use ImageSpecField with source='img'. as i know, field is create Image from ProcessedImageField (which already has been procceced). I want to proccess second image with request.DATA. For example, say we upload img with 300x300. When we ResizeCanvas 1280, we have (1280-300)/2 = 490 white border (first image) Second img 640: if we use ResizeToFit from "source", then we have 490/2=245 white border but if we use ResizeCanvas from request.DATA, then we have (640-300)/2=170 white border (it is better) We can use django ImageField for source, but i don’t want to keep the original image.
- try to set up cachefile_storage and cachefile_backend attrs
- Save original image without resizing to ProcessedImageField and make 2 different ImageSpecs for thumbs