2015-04-07 95 views
2

我已經成功安裝(經過很多努力)django-imagekit,我現在可以使用django-imagekit來壓縮上傳圖像的文件大小。如果圖像大小爲GT,django-imagekit壓縮圖像300KB

我可以上傳6MB的圖片,當我使用10的質量(見下文)時,django-imagekit會將圖像壓縮爲230KB。

當上傳的圖像大小爲300Kb,1MB,2MB,3MB或更大時,是否有一種方法可以使用不同的文件壓縮(django-imagekit將此稱爲質量)(我正在考慮if/elseif/else聲明將確認圖像的大小,並且應用較低質量的圖像的尺寸(KB)越大?對於較大尺寸的圖像,10的文件壓縮效果很好,但從根本上降低了圖像的質量,因此尺寸較小圖像例如25KB。

我甚至不知道我怎麼會寫代碼,在那裏我會放置代碼,將做到這一點。因此,任何幫助,將不勝感激。

這裏是我的相關models.py文件代碼:

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) 

編輯:

我試圖實現ProcessedImageField類的表單字段的版本,但這並不上傳圖片。

這裏是形式的代碼,而改變models.py代碼回像場(也就是上面註釋掉)我曾嘗試:

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 

回答

3

的解決方案我要報價是沒有經過測試。它基於imagekit庫的源代碼。

optionskwargImageSpec類用於傳遞給PIL的Image.save()。因此,對於一個動態options

,你可以創建自己的Spec類中定義optionsproperty,並使用getter返回一個對即時options。喜歡的東西:

from imagekit import ImageSpec 
from imagekit.processors import Adjust, ResizeToFill 

class ThumbnailSpec(ImageSpec): 
    format = 'JPEG' 
    options={'quality': 50} 
    processors=[Adjust(sharpness=1.1),] 

    @property 
    def options(self): 
     options = self._options 
     #You can create some checks here and choose to change the options 
     #you can access the file with self.source 
     if self.source.size > 2*100*100: 
      options['quality'] -= 25 
     return options 
    @options.setter 
    def options(self, value): 
     self._options = value 

ThumbnailSpec通過它傳遞給ProcessedImageField

name_details_photograph = ProcessedImageField(
    upload_to=_get_name_details_photograph_upload_location, 
    null=True, 
    blank=True, 
    spec=ThumbnailSpec 
) 
3

最後使用您可以創建自定義processordjangoimagekit,然後在模型中使用它。 processor將檢查圖像的大小,然後返回編輯的圖像。像這樣的東西 -

class ConditionalResizer(object): 
    min_size = None # minimum size to reduce in KB 
    def __init__(self, *args, min_size=1000, **kwargs): 
     super().__init__(self, *args, **kwargs) # code is for python > 3.0, modify for python < 3.0 as necessary 
     self.min_size = min_size 

    def process(self, image): 
     size = # code to get size of image 
     if size > self.min_size: 
      # process the image 
      image = # processed image 
     return image 

然後在你的Form,添加處理器 -

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), ConditionalResize(min_size=1000)]) 

class Meta: 
    model = NameDetails 

我沒有測試此代碼還,但應該能夠解決您的問題。讓我知道如果它不。

你可以在這裏找到更多關於處理器的信息 - https://django-imagekit.readthedocs.org/en/latest/#processors