I have managed to install (after a lot of effort) django-imagekit and I am now able to use django-imagekit to compress the file size of uploaded images.
I can upload an image of 6MB and django-imagekit will compress the image to 230KB when I use a quality of 10 (see below).
Is there a way to use a different file compression (django-imagekit refers to this as quality) when the uploaded image is a size of 300Kb 1MB , 2MB, 3MB or larger (I am thinking an if/elseif/else statement that would confirm the size of the image and apply a lower quality the larger the size (KB) of the image? The file compression of 10 works well for larger sized images but is terrible for smaller sized images.
I am not even sure how I would write the code and where I would place the code that would achieve this. So any help would be appreciated.
Here is my relevant models.py file code:
from imagekit.processors import Adjust, ResizeToFill
from imagekit.models import ProcessedImageField
class NameDetails(models.Model, FillableModelWithLanguageVersion):
user = models.ForeignKey(User)
....
#name_details_photograph = models.ImageField(upload_to=_get_name_details_photograph_upload_location, null=True, blank=True)
name_details_photograph = ProcessedImageField(upload_to=_get_name_details_photograph_upload_location, null=True, blank=True, options={'quality': 25}, processors=[Adjust(sharpness=1.1),])
....
def __unicode__(self):
return unicode(self.user)
EDIT:
I have tried to implement the form field version of the ProcessedImageField class, but this does not upload the image.
Here is the forms code I have tried while changing the models.py code back to a image field (that is commented out above):
from imagekit.forms import ProcessedImageField
from imagekit.processors import Adjust, ResizeToFill
class NameDetailsForm(forms.ModelForm):
def __init__(self, available_languages, language_preference, file_required, *args, **kwargs):
"""
available_languages should be a valid choices list
"""
super(NameDetailsForm, self).__init__(*args, **kwargs)
self.fields['language_code'] = forms.ChoiceField(choices=available_languages, initial=language_preference, label=_('Language'),)
#self.fields['name_details_photograph'] = forms.FileField(label=_('Photograph'), required=file_required)
self.fields['name_details_photograph'] = ProcessedImageField(label=_('Photograph'), required=file_required, spec_id='myapp:profile:name_details_photograph', options={'quality': 25}, processors=[Adjust(sharpness=1.1),])
class Meta:
model = NameDetails
Aucun commentaire:
Enregistrer un commentaire