Remove all thumbnails generated with easy-thumbnails Django App - django

I am using easy-thumbnails in my Django 1.5 project to generate thumbnail images.
I have been using several different sizes for thumbnails for testing, but now I would like to clear all thumbnails from my filesystem and from the easy-thumbnails database entries. Over time I created several different sizes of many images and I would like to remove those now.
My intention is to start with a clean slate and to remove all thumbnail images. I could not find out how to do that.

Just had the same problem.
Given:
class MyModel(Model):
image = ThumbnailerImageField()
You can delete all thumbnails with:
for m in MyModel.objects.all():
m.image.delete_thumbnails()
If you instead have:
class MyModel(Model):
image = ImageField()
Then you should use:
from easy_thumbnails.files import get_thumbnailer
for m in MyModel.objects.all():
thumbnailer = get_thumbnailer(m.image)
thumbnailer.delete_thumbnails()

I have created a Picture model in which I added a method, as follows
from easy_thumbnails.models import Source, Thumbnail
def clean_thumbnail(self):
if self.image:
sources = Source.objects.filter(name=self.image.name)
if sources.exists():
for thumb in Thumbnail.objects.filter(source=sources[0]):
try:
os.remove(os.path.join(settings.MEDIA_ROOT, thumb.name))
thumb.delete()
except Exception, e:
logger.warning(e)
And it works like a charm.

Related

Manually trigger image save method in Django model

I have a Django model with a customized image field. The image field creates some thumbnail sizes on upload. Code may look like this:
from django.db import models
from utils import CustomImageField
class Photo(models.Model):
image = CustomImageField()
Now I modify the original image, let's say I rotate it. And now I want to trigger the save method of my image field again, in order to overwrite the thumbnails and create rotated versions. So, I don't need to rotate the thumbnails elsewhere in my code (DRY).
Any thoughts? Something along those lines - but how exactly?
p = Photo.objects.get(pk=1)
p.image.save(...)
I have full control over the CustomImageField widget. The save() method is defined as:
def save(self, name, path, save=True):
Question is, what do I use for the methods parameters?
This question looks like a duplicate of Programmatically saving image to Django ImageField
The parameters of the ImageField.save() method are documented for FileField.save() (of which ImageField is a subclass):
https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.fields.files.FieldFile.save
Takes two required arguments: name which is the name of the file, and
content which is an object containing the file’s contents. The
optional save argument controls whether or not the model instance is
saved after the file associated with this field has been altered.
Defaults to True.
Here is what is working for us:
class CustomImage(models.Model):
image = models.ImageField(upload_to=get_file_path, max_length=500)
orig_name = models.TextField()
This is the method that adds an image file to the ImageField from an http resource:
from django.core.files.base import ContentFile
def download_photo(amazon_id, url):
img_data = requests.get(url)
img = CustomImage(orig_name=img_data.url)
img.image.save(slugify(img.orig_name), ContentFile(img_data.content), save=True)
It also works without ContentFile:
new_img = File(open(different_obj.image.path), 'r')
img.image.save(different_obj.image.url, new_img, save=True)
See also:
- https://docs.djangoproject.com/en/1.9/topics/files/
- https://djangosnippets.org/snippets/2587/
An option is dirty field checking, either manually (see this SO question) or using a pypi package
Alternatively, if you want to conserve memory, the resizing can be triggered from the field's property setter (assuming you inherit from FileField
class CustomImageField(FileField):
def _set_file(self, file):
has_file_changed = file != self._file
super(CustomImageField, self)._set_file(file)
if has_file_changed:
self.handle_resizing_etc()
# need to redeclare property so it points to the right _set_file
file = property(FileField._get_file, _set_file, FileField._del_file)
disclaimer: I haven't used this approach in production code and I haven't written a proof of concept before posting this answer, so it might not work as desired

Handling files in Django

I'm using ImageMagick and the binding wand to generate thumbnails for uploaded images in Django. I can generate the thumbnail fine, but I'm uncertain about how to go about passing the image object from ImageMagick back into the Django model. So I have a simplified model as below:
from wand import Image
class Attachment(models.Model):
attachment = models.FileField(upload_to="some_path")
thumbnail = models.ImageField(upload_to="other_path")
def generate_thumb(self):
with Image(file=self.attachment) as wand:
thumb = wand.resize(width=50, height=50)
thumb.save(file=self.thumbnail)
This generates an error at the last line of ValueError: The 'thumbnail' attribute has no file associated with it. Is there a simple way to get a file object out of wand and into django without too much silliness?
Thanks.
Disclaimer: I am the creator of Wand you are trying to use.
First of all, ImageField requires PIL. It means you don’t need Wand, because you probably already installed an another image library. However I’ll answer to your question without any big changes.
It seems that self.thumbnail was not initialized yet at the moment, so you have to create a new File or ImageFile first:
import io
def generate_thumb(self):
buffer = io.BytesIO()
with Image(file=self.attachment) as wand:
wand.resize(width=50, height=50)
wand.save(file=buffer)
buffer.seek(0)
self.thumbnail = ImageFile(buffer)
Plus, from wand import Image will raise ImportError. It should be changed:
from wand.image import Image
If the goal is easy thumbnails in your django app try: https://github.com/sorl/sorl-thumbnail
Its pretty popular and active.

How to resize source with sorl-thumbnail?

I was searching the web for my question and couldn't find a clear answeror any example.
Basically, I want to use sorl and want to resize the source image during the Model save time to resize it down to 640x480 size, so that I don't end-up storing user's original 2.5 MB files on the disk. I will then use templatetags to create regular thumbnails out of my source as documented in sorl.
I came across couple of sources refer to use ThumbnailField model field that is supposed to be available in sorl.thumbnail.fields. See the link here. However, in my up-to-date sorl copy from the trunk I don't see any ThumbnailField or ImageWithThumbnailsField. My attempt to import it in the model fails accordingly. I see these references are old though and wondering whether I can achieve the same with up-to-date sorl.
On the other hand sorl documentation indicates only ImageField from sorl.thumbnail (see here) that does not have any size argument to control the source resizing.
BTW, I see this functionality is available with easy_thumbnail that takes an input parameter source_resize.
Any help will be appreciated!
SUMMARY
I accepted the below answer, however I feel natural sorl support for this use case can be very useful - i.e. adding resize_source param to sorl's ImageField to allow resizing the source image. Below are two factors why this can be useful in the field:
Not to store user's huge original images if your app has no need for it. Saving disk space.
Not to spend extra CPU for resizing thumbnails from that huge source images if you don't have specific extreme high quality reasons. To avoid this one may write nested tags in templates to thumbnail from smaller size images but it can become annoying very soon.
I found a flaw in the code above, got “str has no method chunck()”, if somebody want to use it. Here is my fix:
from sorl.thumbnail import get_thumbnail
from django.core.files.base import ContentFile
class Foo(models.Model):
image = models.ImageField(upload_to...)
def save(self, *args, **kwargs):
if not self.id:
super(Foo, self).save(*args, **kwargs)
resized = get_thumbnail(self.image, "100x100" ...)
self.image.save(resized.name, ContentFile(resized.read()), True)
super(Foo, self).save(*args, **kwargs)
Sorl's ImageField that you mention, is just a normal Django ImageField with the added benefit of managing the deletion of cached thumbnails. There's no resizing done on the initial upload - that's something you have to implement yourself manually via the view you are using to upload. The docs show how to go about this. You can use sorl in that view to do the actual resize operation itself, using the low level API examlpes
EDIT
A quicker alternative is to just resize the image when the model is being saved using sorl. You can do something like the following (completely untested though!)
from sorl.thumbnail import get_thumbnail
class Foo(models.Model):
image = models.ImageField(upload_to...)
def save(self, *args, **kwargs):
if not self.id:
# Have to save the image (and imagefield) first
super(Foo, self).save(*args, **kwargs)
# obj is being created for the first time - resize
resized = get_thumbnail(self.image, "100x100" ...)
# Manually reassign the resized image to the image field
self.image.save(resized.name, resized.read(), True)
super(Foo, self).save(*args, **kwargs)
this will mean that you will have 2 versions of the same image on disk - one where the django image field decides to save it (upload_to path) and one where sorl thumbnail has saved it's resized thumbnail. This, along with the fact the image is uploaded and saved twice, are the disadvantages of this approach. It's quicker to implement though
I was looking for solution for some time and eventually wrote app django-resized.
The following code uses the PIL Engine (part of sorl-thumbnail) to crop an image called picture.jpg (tested using Python 3.8 and sorl-thumbnail==12.6.3):
#
# Change this import to get the Engine of your underlying libraries.
# Options are: convert_engine, pgmagick_engine, pil_engine, vipsthumbnail_engine or wand_engine.
#
from sorl.thumbnail.engines.pil_engine import Engine
# This object has all we need
engine = Engine()
#
# When receiving data from a request
# you probably have a BytesIO instance ready to use like:
#
# im = engine.get_image(my_bytes_io)
#
with open("picture.jpg", "rb") as f:
im = engine.get_image(f)
im_crop = engine.crop(im, (535, 535), options={'crop': 'smart'})
im_crop.save("picture-thumb.jpg")
Instead of modifying the save method, I would have a helper function for reducing the image size (with the lines above) and call it from the Django view or form before updating the image field. Though it would work doing it on the save itself.
On the other hand, the Engine API has more useful features that could be useful! This API has been there since the first commit so, in my opinion, it's unlikely to change in the future: create, cropbox, orientation, flip_dimensions, colorspace, remove_border, calculate_scaling_factor, scale, crop, rounded, blur, padding, write, cleanup, get_image_ratio, get_image_info, get_image, get_image_size, is_valid_image.

Djangothumbnails app issue

I an using this plugin to generate thumbnails. But somhow I couldn't make it work. The models work well, as images can be uploaded from the admin interface, even thumbnails get generated. I uploaded an image named "myphoto.jpg". The view I have is this.
def mainpage(request,slug):
page = get_object_or_404(MainPage, slug=slug)
image = get_object_or_404(Image)
return direct_to_template(request, 'flatpage.html',
extra_context={
'page':page,
'image':image,
})
I have defined the class in models as "Image". I had this in the template:
<img src="{% image.photo.myphoto_125x125 %}"> But the template shows error.
This is my model:
class Image(models.Model):
title = models.CharField(max_length=100)
photo = ImageWithThumbsField(upload_to='images/', sizes=((125,125),(200,200)))
# second_photo = ImageWithThumbsField(upload_to='site_media/images')
def __unicode__(self):
return self.title
The second field, "second_photo" I have commented out as it created duplicate copy of the main image. Even I am looking for a way to create entry for actual unsized image in the first field itself.
I havent tried this app, but I am using PIL to create thumbnail versions of uploaded images, and using a def in the model to retrieve the path to the thumbnail for display. This may be more of a manual approach than you are looking for but it works ok for me. You can override the save method on your model (which has your FileField) or send a post_save signal to a def to create the thumbnail.
But curious to know if this app you are using adds some useful functionality over this manual approach.
Somehow I got it working. Instead of creating a separate class, I used this line photo = ImageWithThumbsField(blank=True, upload_to='images/', sizes=((125,125),(200,200))) in my blog class. And I used the line {{% post.photo.url_125x124 %} in my index.html template.

django - save a resize copy of image on upload (using admin site)

I'm building a site for a client that needs to support image uploads (an artist) through the admin interface. Since most of the images are pretty high-res, I wanted to create thumb copies of the image to display on the gallery page after the upload. The upload works great with the forms.ImageFile element, but I was looking for some ideas on how to do the actual resizing and and linking between the thumb and the true size images. I had an idea to hold model class for both an image and an image thumb:
from django.db import models
class Image(models.Model):
"""a true size image"""
image = models.ImageFile(upload_to="images")
desc = models.CharField(max_length=256)
class Meta:
db_table = "images"
class ImageThumb(models.Model):
""""a thumbnail of an actual image"""
real_image = models.ForeignKey('Image')
image = models.ImageField(upload_to="images/thumbs")
class Meta:
db_table = "thumbs"
That part I'm stuck on is how to resize the real image after upload (pil? how?), and I could probably use some polishing on my models - any help will be great. Thanks.
There's a great plugin called sorl-thumbnail that deals with thumbnail generation - don't bother doing it yourself. sorl-thumbnail is very configurable, so the chances are it'll do anything you want it to do.
If that doesn't work for you, then photologue is also very good (photologue is more tailored towards managing photo albums, rather than just plain thumbnail generation).
See also easy-thumbnails and aino-convert. They might be a good bet since sorl-thumbnail might not be developed very actively from now on.