I'm trying to implement crop and upload feature as per this blog post:
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.