def save(self, *args, **kwargs): # Сначала - обычное сохранение super().save(*args, **kwargs) # Проверяем, указана ли картинка if self.img: self.img.name = utils.to_webp(self.img.path, self.img.name) filepath = self.img.path new_db_img = utils.get_rename_path(self.img.name, self.pk) new_file_name = utils.get_rename_path(filepath, self.pk) image = PIL.Image.open(filepath) width, height = image.size if (width > self.size or height > self.size): image.thumbnail((self.size, self.size), resample=PIL.Image.LANCZOS) image.save(os.path.normpath(new_file_name)) self.img.name = new_db_img super(Images, self).save(*args, **kwargs) image.close() utils.to_jpg(self.img.path) if filepath != new_file_name: os.remove(filepath)
def save(self, *args, **kwargs): # Сначала - обычное сохранение super(Partitions, self).save(*args, **kwargs) # Проверяем, указана ли картинка if self.cover: self.cover.name = utils.to_webp(self.cover.path, self.cover.name) filepath = self.cover.path new_db_img = utils.get_rename_path(self.cover.name, self.pk) new_file_name = utils.get_rename_path(filepath, self.pk) # Создадим обложку image = PIL.Image.open(filepath) image.thumbnail(settings.COVER_IMAGE_SIZE, resample=PIL.Image.LANCZOS) image.save(os.path.normpath(new_file_name)) self.cover.name = new_db_img super(Partitions, self).save(*args, **kwargs) image.close() utils.to_jpg(self.cover.path) if filepath != new_file_name: os.remove(filepath)
def save(self, *args, **kwargs): # Сначала - обычное сохранение super(Images, self).save(*args, **kwargs) # Проверяем, указана ли картинка if self.img: self.img.name = utils.to_webp(self.img.path, self.img.name) filepath = self.img.path new_db_img = utils.get_rename_path(self.img.name, self.pk) new_file_name = utils.get_rename_path(filepath, self.pk) thumb_file_name = utils.get_prefix_path(new_file_name) middle_file_name = utils.get_prefix_path(new_file_name, 'mid') # Создадим превьюшку image = PIL.Image.open(filepath) image = utils.get_box_thumb(image, settings.THUMB_IMAGE_SIZE) image.save(os.path.normpath(thumb_file_name)) utils.to_jpg(thumb_file_name) # Создадим обложку image = PIL.Image.open(filepath) image.thumbnail(settings.MIDDLE_SIZE_IMAGE, resample=PIL.Image.LANCZOS) image.save(os.path.normpath(middle_file_name)) utils.to_jpg(middle_file_name) # Просто уменьшим картинку до заданного MAX_SIZE_IMAGE image = PIL.Image.open(filepath) width, height = image.size if (width > settings.MAX_SIZE_IMAGE[0]) or ( height > settings.MAX_SIZE_IMAGE[1]): image.thumbnail(settings.MAX_SIZE_IMAGE, resample=PIL.Image.LANCZOS) # Наложение водяного знака image = utils.add_watermark(image) image.save(os.path.normpath(new_file_name)) self.img.name = new_db_img super(Images, self).save(*args, **kwargs) image.close() utils.to_jpg(self.img.path) if filepath != new_file_name: os.remove(filepath)