2017-07-17 10 views
0

サイズ変更後にユーザーアバターをS3にアップロードしています。私のModelFormは以下の通りです:Django、Pillow、NotImplementedError - 絶対パス

class UserAvatarForm(forms.ModelForm): 
    x = forms.FloatField(widget=forms.HiddenInput()) 
    y = forms.FloatField(widget=forms.HiddenInput()) 
    width = forms.FloatField(widget=forms.HiddenInput()) 
    height = forms.FloatField(widget=forms.HiddenInput()) 

    class Meta: 
     model = UserProfile 
     fields = ('id', 'img', 'x', 'y', 'width', 'height') 

    def save(self, *args, **kwargs): 
     photo = super(UserAvatarForm, self).save() 

     x = self.cleaned_data.get('x') 
     y = self.cleaned_data.get('y') 
     w = self.cleaned_data.get('width') 
     h = self.cleaned_data.get('height') 

     image = Image.open(photo.img) 
     cropped_image = image.crop((x, y, w+x, h+y)) 
     resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS) 
     resized_image.save(photo.img.path) 

     return photo 

NotImplementedError /プロフィール/アバターで/このバックエンドは 絶対パスをサポートしていません。

保存方法を編集してFile Storage APIを使用することができますが、実装方法はわかりません。どんな助け?おかげで

答えて

0

これはそれを働かせました。今後の参考のためにここに残しておきます。

def save(self, *args, **kwargs): 
    photo = super(UserAvatarForm, self).save() 

    x = self.cleaned_data.get('x') 
    y = self.cleaned_data.get('y') 
    w = self.cleaned_data.get('width') 
    h = self.cleaned_data.get('height') 

    image = Image.open(photo.img) 
    cropped_image = image.crop((x, y, w+x, h+y)) 
    resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS) 
    storage_path = storage.open(photo.img.name, "wb") 
    resized_image.save(storage_path, 'png') 
    storage_path.close() 

    return photo