1

I'm trying to implement crop and upload feature as per this blog post:

https://simpleisbetterthancomplex.com/tutorial/2017/03/02/how-to-crop-images-in-a-django-application.html

When attempting to save the cropped photo I get the error:

"NotImplementedError: This backend doesn't support absolute paths."

forms.py

class LetterPictureModelForm(forms.ModelForm):
    picture = forms.ImageField(
        label='Picture',
        widget=ClearableFileInput(attrs={
            'class': 'form-control',
            'placeholder': 'Picture'
        })
    )
    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 = LetterPicture
        fields = ['picture', 'x', 'y', 'width', 'height', ]
        widgets = {
                'letter': forms.HiddenInput(),
                'slot': forms.HiddenInput()
        }

    def save(self):
        letter_picture = super(LetterPictureModelForm, 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(letter_picture.picture)
        cropped_image = image.crop((x, y, w + x, h + y))
        resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
        resized_image.save(letter_picture.picture.path)

        return letter_picture

settings.py

STATICFILES_STORAGE = 'custom_storages.StaticStorage'

custom_storages.py

from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage

class StaticStorage(S3Boto3Storage):
    location = settings.STATICFILES_LOCATION


class MediaStorage(S3Boto3Storage):
    location = settings.MEDIAFILES_LOCATION

As you can see I'm using S3 to save media files, which has been working as expected so far.

Can anyone tell me what's causing this error? I don't understand what it means.

Update

Upon Zoro-Zen's suggestion I've adopted the approach that was used here: Resize thumbnails django Heroku, 'backend doesn't support absolute paths'

def save(self):
    letter_picture = super(LetterPictureModelForm, 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(letter_picture.picture)
    cropped_image = image.crop((x, y, w + x, h + y))
    resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
    fh = storage.open(letter_picture.picture.name, "w")
    format = 'JPEG'  # You need to set the correct image format here
    resized_image.save(fh, format)
    fh.close()

    return letter_picture

I have a feeling that I've made a trivial error as the images appear in S3 unchanged with no error thrown, I can't find my error yet though.

goose
  • 2,502
  • 6
  • 42
  • 69
  • [Maybe this helps](https://stackoverflow.com/questions/18215989/resize-thumbnails-django-heroku-backend-doesnt-support-absolute-paths)! – RonanFelipe Jul 09 '19 at 05:31
  • [Also here the same thing about path and name](https://stackoverflow.com/questions/48785235/django-user-uploaded-s3-files-in-the-view)! – RonanFelipe Jul 09 '19 at 05:32
  • @Zoro-Zen thanks for the links. I've tried adopting the approach on one of them. The error's gone, but it's not saving any updated images. I suspect I've made an error in my implementation as my understanding's not quite where it could be, but I can't spot whatever the problem is. – goose Jul 09 '19 at 06:56

0 Answers0