Django - can't open image in model save() - django

def upload_path_handler(instance, filename):
return filename
class SpectacleGallery(models.Model):
image = models.ImageField(upload_to=upload_path_handler)
def save(self, *args, **kwargs):
Image.open(self.image)
super(SpectacleGallery, self).save(*args, **kwargs)
When I try to open it I get:
IOError at /admin/index/spectacle/1/
cannot identify image file
Why? File is a proper image.
Does that file in save methos is not a good format for PIL?
EDIT:
Here's my final working version of code:
def save(self, *args, **kwargs):
# set paths and additional variables
self_pk = self.pk
spectacle_id = self.spectacle_id
spectacle_id_str = str(spectacle_id)
create_gallery_spectacle_dir(spectacle_id)
new_filename = generate_image_name_hash()
new_filename_main = new_filename + '.jpg'
new_filename_thumb = new_filename + '_thumb.jpg'
new_file_thumb_path = settings.SPECTACLE_GALLERY_UPLOAD_DIR + '/' + spectacle_id_str + '/' + new_filename_thumb
new_file_thumb_root_path = settings.SPECTACLE_GALLERY_UPLOAD_PATH + spectacle_id_str + '/' + new_filename_thumb
new_file_root_path = settings.SPECTACLE_GALLERY_UPLOAD_PATH + spectacle_id_str + '/' + new_filename_main
if self.image:
#set new name and thum name
self.image.name = settings.SPECTACLE_GALLERY_UPLOAD_DIR + '/' + spectacle_id_str + '/' + new_filename_main
self.image_thumb = new_file_thumb_path
# image is in form and action is add call the "real" save() method.
if self.image and not self_pk:
super(SpectacleGallery, self).save(*args, **kwargs)
# image is in form and action is edit: get old image info, create variable with image field
if self.image and self_pk:
old_img = SpectacleGallery.objects.get(pk=self.pk)
old_img_instance = old_img.image
if self.image:
if self_pk:
image = old_img_instance
else:
image = self.image
super(SpectacleGallery, self).save(*args, **kwargs) #Call the "real" save() method.
#if image in form
if self.image:
# open file with PIL and convert to RGB
tmp_file = Image.open(self.image.path)
if tmp_file.mode != 'RGB':
tmp_file = tmp_file.convert('RGB')
#create and save thumbnail
tmp_file.thumbnail(settings.SPECTACLE_GALLERY_THUMB_SIZE, Image.ANTIALIAS) #make thumbnail
tmp_file.save(new_file_thumb_root_path, 'JPEG') #save thumbnail
# if edit delete old images
if self_pk:
delete_image_and_thumb(old_img.image, old_img.image_thumb)
#open and resize original image
image = Image.open(self.image.path)
if image.mode != 'RGB':
image = image.convert('RGB')
image.thumbnail(settings.SPECTACLE_GALLERY_IMAGE_SIZE, Image.ANTIALIAS) #make thumbnail
image.save(new_file_root_path,'JPEG', quality=100)

The Django imageField isn't an image you will need to do something like this.
Image.open(self.image.path)

Related

Django custom save model creating duplicate files

I'm trying to get image uploads to also save as thumbnails, which works. The problem was when I did an update the custom method saved the thumbnail again in a different directory. so I modified my save function to look like this.
models.py
class Photo(models.Model):
title = models.CharField(max_length=64)
description = models.CharField(max_length=255)
created = models.DateTimeField(auto_now_add=True)
image = models.ImageField(upload_to='photos/%Y%m')
thumbnail = models.ImageField(blank=True, upload_to='thumbnails/%Y%m')
submitter = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
year = models.ForeignKey(Year, blank=True, on_delete=models.CASCADE)
people = TaggableManager(through=TaggedPeople, verbose_name='People')
tags = TaggableManager(through=TaggedGeneric, verbose_name='Tags')
def save(self, *args, **kwargs):
try:
this = Photo.objects.get(id=self.id)
if this.thumbnail != self.thumbnail:
this.thumbnail.delete(save=False)
except:
if self.thumbnail:
img = Image.open(BytesIO(self.thumbnail.read()))
if hasattr(img, '_getexif'):
exif = img._getexif()
if exif:
for tag, label in ExifTags.TAGS.items():
if label == 'Orientation':
orientation = tag
break
if orientation in exif:
if exif[orientation] == 3:
img = img.rotate(180, expand=True)
elif exif[orientation] == 6:
img = img.rotate(270, expand=True)
elif exif[orientation] == 8:
img = img.rotate(90, expand=True)
img.thumbnail((360,360), Image.ANTIALIAS)
output = BytesIO()
img.save(output, format='JPEG', quality=95)
output.seek(0)
self.thumbnail = File(output, self.thumbnail.name)
return super().save(*args, **kwargs)
def __str__(self):
return self.title
and my views
class PhotoCreateView(LoginRequiredMixin, CreateView):
model = Photo
fields = ['image', 'title', 'description', 'year', 'people', 'tags']
template_name = 'photoapp/create.html'
success_url = '/photo/?page=1'
extra_context = {'tags':GenericTag.objects.all().order_by('name'),'people':PeopleTag.objects.all().order_by('name'),}
def form_valid(self, form):
form.instance.thumbnail = self.request.FILES['image']
form.instance.submitter = self.request.user
return super().form_valid(form)
class PhotoUpdateView(LoginRequiredMixin, UpdateView):
template_name = 'photoapp/update.html'
model = Photo
fields = ['title', 'description', 'year', 'people', 'tags']
success_url = '/photo/?page=1'
So the CreateView now works fine, and I have stopped the duplicate thumbnail files, but the UpdateView does not work. How can I fix this?
I figured it out. I just had to add a save to the try section.
def save(self, *args, **kwargs):
try:
this = Photo.objects.get(id=self.id)
if this.thumbnail != self.thumbnail:
this.thumbnail.delete(save=False)
return super().save(*args, **kwargs)
except:
if self.thumbnail:
img = Image.open(BytesIO(self.thumbnail.read()))
if hasattr(img, '_getexif'):
exif = img._getexif()
if exif:
for tag, label in ExifTags.TAGS.items():
if label == 'Orientation':
orientation = tag
break
if orientation in exif:
if exif[orientation] == 3:
img = img.rotate(180, expand=True)
elif exif[orientation] == 6:
img = img.rotate(270, expand=True)
elif exif[orientation] == 8:
img = img.rotate(90, expand=True)
img.thumbnail((360,360), Image.ANTIALIAS)
output = BytesIO()
img.save(output, format='JPEG', quality=95)
output.seek(0)
self.thumbnail = File(output, self.thumbnail.name)
return super().save(*args, **kwargs)
I had a similar complaint in Django-Wagtail, where saving image.save() programmatically duplicated on the file system the imported image. Do get around this I used Python Glob to get the path, and OS to remove the original image after the save function had been loaded. It worked well enough for Wagtail, I wanted to capture the issue for others - as there isn't much on the web about this. It's frustrating, doubles your disk space usage due to this functionality if you aren't careful! I don't like that it renames the files/moves them, but it is what it is.
from django.core.management.base import BaseCommand, CommandError
from wagtail.images.models import Image
from django.core.files.images import ImageFile
import os
from os import path
import glob
#import sqlite3
class Command(BaseCommand):
help = "just a thing to "
def handle(self, *args, **options):
target_path = "/home/inmyth/inmyth/media/images/"
my_images = []
if os.path.exists(target_path):
my_images = glob.glob(target_path + "*.png")
for mj_imgs in my_images:
print(mj_imgs)
image_file = ImageFile(open(mj_imgs, 'rb'), name=mj_imgs[:-4])
img_label = mj_imgs.rfind("/") + 1
image = Image(title=mj_imgs[img_label:-4], file=image_file)
image.save()
os.remove(mj_imgs)
pass

How to associate newly created file model

I have this code,
#receiver(post_save, sender=FileAnswer)
def save_category_signals(sender, instance, **kwargs):
file_types = ["image", "image-multiple"]
file_type = instance.question.form_input_type
if file_type in file_types:
image_path = instance.body.path
image = Image.open(image_path)
img = image.convert('RGB')
new_path = image_path.rsplit('.', 1)
pdf = img.save(f'{new_path[0]}_{instance.id}.pdf', format="PDF")
# os.remove(image_path)
instance.body = pdf
instance.save()
# os.remove(image_path) # remove old image
This code does not associate the file to the model instance. I have looked at django ContentFile and File. still can't really wrap my head around it as the examples aren't that helpful.
try something like this you should use InMemoryUploadedFile:
#receiver(post_save, sender=FileAnswer)
def save_category_signals(sender, instance, **kwargs):
file_types = ["image", "image-multiple"]
file_type = instance.question.form_input_type
if file_type in file_types:
image_path = instance.body.path
new_path = image_path.rsplit('.', 1)
image = Image.open(image_path)
image = image.convert('RGB')
output = io.BytesIO()
image.save(output, format='PDF')
output.seek(0)
pdf = InMemoryUploadedFile(output, 'FileField',
f'{new_path[0]}_{instance.id}.pdf',
'application/pdf',
sys.getsizeof(output), None)
# os.remove(image_path)
instance.body = pdf
instance.save()
# os.remove(image_path) # remove old image

name 'InMemoryUploadedFile' is not defined in django model?

So basically I have a django model which has a filefield. I'd like to resize images that get submitted and I have this code here to do that
def save(self, *args, **kwargs):
img = Image.open(self.media)
output = BytesIO()
original_width, original_height = img.size
aspect_ratio = round(original_width / original_height)
desired_height = 100
desired_width = desired_height * aspect_ratio
img = img.resize((desired_width, desired_height))
img.save(output, format='JPEG', quality=90)
output.seek(0)
self.image = InMemoryUploadedFile(output, 'ImageField', "%s.jpg" % self.image.name.split('.')[0], 'image/jpeg', sys.getsizeof(output), None)
super(Post, self).save(*args, **kwargs)
The error im getting is "name 'InMemoryUploadedFile' is not defined". How do I fix this? Also a side question. The model also takes in videos. How would I resize a video? This is just a side question the InMemoryUploaded is the main question. Thank you!
did you import InMemoryUploadedFile?
please import it first
from django.core.files.uploadedfile import InMemoryUploadedFile
You don't need to use InMemoryUploadedFile to save a file. Also you get the error because you never import it. To import it from django.core.files import InMemoryUploadedFile. Instead of it you should better use ContentFile:
from django.core.files.base import ContentFile
...
def save(self, *args, **kwargs):
img = Image.open(self.media)
output = BytesIO()
original_width, original_height = img.size
aspect_ratio = round(original_width / original_height)
desired_height = 100
desired_width = desired_height * aspect_ratio
img = img.resize((desired_width, desired_height))
img.save(output, format='JPEG', quality=90)
output.seek(0)
self.image.save("%s.jpg" % self.image.name.split('.')[0], ContentFile(output), save=False)
super(Post, self).save(*args, **kwargs)

How to compress images before uploading them to Amazon s3 using django storages and django rest framework?

I'm trying to compress the images before uploading them to the amazon s3 server, but I couldn't do it, I used 'PIL', to do it, but it didn't work
This is the code I used with the library 'PIL':
from io import BytesIO
from PIL import Image
from django.core.files import File
def compress(image):
im = Image.open(image)
im_io = BytesIO()
im.save(im_io,'PNG', quality=70)
new_image = File(im_io, name=image.name)
return new_image
class MyModel(models.Model):
file = models.FileField(blank=True, null=True, validators=[validateFileSize])
def save(self, *args, **kwargs):
new_image = compress(self.file)
self.file = new_image
super().save(*args, **kwargs)
I solved it using the following code
def compress(image):
im = Image.open(image)
# create a BytesIO object
im_io = BytesIO()
# save image to BytesIO object
#im = im.resize([500,500])
im = im.convert("RGB")
im = im.save(im_io,'JPEG', quality=70)
# create a django-friendly Files object
new_image = File(im_io, name=image.name)
return new_image
class Media(models.Model):
file = models.FileField(blank=True, null=True, validators=[validateFileSize])
def __str__(self):
return str(self.id)
def save(self, *args, **kwargs):
if self.file:
if self.file.size > (300 * 1024):
# call the compress function
new_image = compress(self.file)
# set self.image to new_image
self.file = new_image
# save
super().save(*args, **kwargs)

Django - How to update a field inside a model save() method?

It is possible to update a model field inside the save() method?
models.py:
class BicycleAdItemKind(MPTTModel):
image_file_temp_fullpath = ""
image_file_temp_filename = ""
def url(self, filename):
#pdb.set_trace()
#url = "MultimediaData/HelpAdImages/ItemKind/%s/%s" % (self.id, filename)
url = "MultimediaData/HelpAdImages/ItemKind/Temp/%s" % (filename)
self.image_file_temp_fullpath = url
self.image_file_temp_filename = filename
return url
def item_kind_image(self):
return '<img align="middle" src="/media/%s" height="60px" />' % self.image
item_kind_image.allow_tags = True
n_item_kind = models.CharField(max_length=50) # Bicicleta completa, Componentes para bicicleta, Acessorios para ciclista
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
description = models.TextField(null=True, blank=True)
image = models.ImageField(upload_to=url, null=True, blank=True)
date_inserted = models.DateTimeField(auto_now_add=True)
date_last_update = models.DateTimeField(auto_now=True)
def save(self, *args, **kwargs):
if not self.id:
BicycleAdItemKind.tree.insert_node(self, self.parent)
super(BicycleAdItemKind, self).save(*args, **kwargs)
pdb.set_trace()
# I will move the file from "Temp" folder to the folder with the "Id" number
from django.core.files.move import file_move_safe
src = settings.MEDIA_ROOT + "/" + self.image_file_temp_fullpath
dst = settings.MEDIA_ROOT + "/" + "MultimediaData/HelpAdImages/ItemKind/%s/%s" % (self.id, self.image_file_temp_filename)
new_directory = settings.MEDIA_ROOT + "/MultimediaData/HelpAdImages/ItemKind/%s" % (self.id)
if not os.path.exists(new_directory):
os.makedirs(new_directory)
if file_move_safe(src, dst):
# I will update the field image
BicycleAdItemKind.objects.filter(pk=self.id).update(image=dst)
# Delete the Temp file
def __unicode__(self):
return self.n_item_kind
class MPTTMeta:
order_insertion_by = ['n_item_kind']
The line of code bellow does not do any action,
BicycleAdItemKind.objects.filter(pk=self.id).update(image=dst)
It is possible to do an update inside a save method?
Give me some clues.
Best Regards,
Move the super save to the end of save() so it updates the model after you make your changes.
def save(self, *args, **kwargs):
if not self.id:
BicycleAdItemKind.tree.insert_node(self, self.parent)
pdb.set_trace()
# I will move the file from "Temp" folder to the folder with the "Id" number
from django.core.files.move import file_move_safe
src = settings.MEDIA_ROOT + "/" + self.image_file_temp_fullpath
dst = settings.MEDIA_ROOT + "/" + "MultimediaData/HelpAdImages/ItemKind/%s/%s" % (self.id, self.image_file_temp_filename)
new_directory = settings.MEDIA_ROOT + "/MultimediaData/HelpAdImages/ItemKind/%s" % (self.id)
if not os.path.exists(new_directory):
os.makedirs(new_directory)
if file_move_safe(src, dst):
# I will update the field image
BicycleAdItemKind.objects.filter(pk=self.id).update(image=dst)
# Delete the Temp file
super(BicycleAdItemKind, self).save(*args, **kwargs)
Have you tried checking to see what dst is when you run the update? Why not do it like this?
self.image=dst