Thumbnails are created on each model save()

toothie
class UserProfile(models.Model):
 user = models.OneToOneField(User, related_name="person")
 image = models.ImageField(upload_to='site-media/media/userimages/', default = 'site-media/user_default.jpeg')
 thumbnail = models.ImageField(upload_to="site-media/media/userimages/userthumbs/", blank=True, null=True)
 thumbnail2 = models.ImageField(upload_to="site-media/media/userimages/userthumbs2/", blank=True, null=True)
 pub_date = models.DateTimeField(auto_now_add=True)
 bio = models.CharField(max_length=300, blank = True)

def create_thumbnail(self):
    if not self.image:
        return

    IMAGE_SIZE = (150,150)

    user_name = self.user.username
    image = Image.open(StringIO(self.image.read()))
    imagef = ImageOps.fit(image, IMAGE_SIZE, Image.ANTIALIAS)

    temp_handle = StringIO()
    imagef.save(temp_handle, 'jpeg')
    temp_handle.seek(0)

    suf = SimpleUploadedFile(os.path.split(self.image.name)[-1],
            temp_handle.read(), content_type='image/jpeg')
    self.image.save('%s.%s'%(user_name,'jpeg'), suf, save=False)

    THUMBNAIL_SIZE = (100,100)

    image = Image.open(StringIO(self.image.read()))
    thumb = ImageOps.fit(image, THUMBNAIL_SIZE, Image.ANTIALIAS)

    temp_handle = StringIO()
    thumb.save(temp_handle, 'jpeg')
    temp_handle.seek(0)

    suf = SimpleUploadedFile(os.path.split(self.image.name)[-1],
            temp_handle.read(), content_type='image/jpeg')
    self.thumbnail.save('%s_thumb.%s'%(user_name,'jpeg'), suf, save=False)

    THUMBNAIL2_SIZE = (32,32)

    thumb2 = ImageOps.fit(image, THUMBNAIL2_SIZE, Image.ANTIALIAS)

    temp_handle = StringIO()
    thumb2.save(temp_handle, 'jpeg')
    temp_handle.seek(0)

    suf = SimpleUploadedFile(os.path.split(self.image.name)[-1],
            temp_handle.read(), content_type='image/jpeg')
    self.thumbnail2.save('%s_thumb2.%s'%(user_name,'jpeg'), suf, save=False)

def save(self, *args, **kwargs):
    """Save image dimensions."""
    self.create_thumbnail()

    super(UserProfile, self).save(*args, **kwargs)

This is a model for a user's profile. It woks great. The problem with this code is, whenever I update the textfields, the thumbnails and image gets created again without the previous ones being deleted. This is a certailnly a bad thing. So, is there a way to avoid copies of image and thumbnails with each update of the textfields? I thought of creating the thumbnails in the view, but, I couldn't write a working code.

Krzysztof Szularz

Use solr-thumbnail for the job.

Simply use the ImageField provided by the app and all your troubles will go away. :)

In your case you'll need only image field in the model. With solr-thumbnail all you need is one file to create many thumbnails in various sizes out of it. The app also deletes thumbnails when the original file is no more.

Installation process is a breeze as well.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Thumbnails are not created?

From Dev

Unable to save data add to the model created with Devise (name model : user )

From Dev

How to save changed state of dynamic created UISwitch in CoreData model in Swift?

From Dev

How to save changed state of dynamic created UISwitch in CoreData model in Swift?

From Dev

IN MATLAB: How to save a graph created inside of a loop for each repetition?

From Dev

Csv file - User permission needs to be created after each save

From Dev

How can I save thumbnails in this script?

From Dev

How can I save thumbnails in this script?

From Dev

Is there a way in flask to get each object in the request.json and save each attribute on the model?

From Dev

Sklearn How to Save a Model Created From a Pipeline and GridSearchCV Using Joblib or Pickle?

From Dev

Reading a file created by the media capture plugin in Cordova - Creating Thumbnails

From Dev

Reading a file created by the media capture plugin in Cordova - Creating Thumbnails

From Dev

How to save radio button selections (each with a string value) to an array of strings within a specific mongoose model?

From Dev

Laravel get each line from textarea and save the values in array to send to model

From Dev

Django, retrieve image using url and save it ThumbnailerImageField of easy_thumbnails

From Dev

Django model save related model

From Dev

My thumbnails in a row are not side by side (Instead stacking on top of each other)

From Dev

Firebase: $save to Not Yet Created Object

From Dev

Save the value of Dynamically created textbox

From Dev

blob created by createObjectURL save to database

From Dev

Save current user with created object

From Dev

Save tensorflow model to file

From Dev

Laravel 'save()' for 'hasmany' Model

From Dev

OpenNLP save a trained model

From Dev

save a tuple to a django model

From Dev

Is it ideal to perform save in model?

From Dev

Save MinMaxScaler model in sklearn

From Dev

to save a complicated model in backbone

From Dev

Cannot save a rails model

Related Related

  1. 1

    Thumbnails are not created?

  2. 2

    Unable to save data add to the model created with Devise (name model : user )

  3. 3

    How to save changed state of dynamic created UISwitch in CoreData model in Swift?

  4. 4

    How to save changed state of dynamic created UISwitch in CoreData model in Swift?

  5. 5

    IN MATLAB: How to save a graph created inside of a loop for each repetition?

  6. 6

    Csv file - User permission needs to be created after each save

  7. 7

    How can I save thumbnails in this script?

  8. 8

    How can I save thumbnails in this script?

  9. 9

    Is there a way in flask to get each object in the request.json and save each attribute on the model?

  10. 10

    Sklearn How to Save a Model Created From a Pipeline and GridSearchCV Using Joblib or Pickle?

  11. 11

    Reading a file created by the media capture plugin in Cordova - Creating Thumbnails

  12. 12

    Reading a file created by the media capture plugin in Cordova - Creating Thumbnails

  13. 13

    How to save radio button selections (each with a string value) to an array of strings within a specific mongoose model?

  14. 14

    Laravel get each line from textarea and save the values in array to send to model

  15. 15

    Django, retrieve image using url and save it ThumbnailerImageField of easy_thumbnails

  16. 16

    Django model save related model

  17. 17

    My thumbnails in a row are not side by side (Instead stacking on top of each other)

  18. 18

    Firebase: $save to Not Yet Created Object

  19. 19

    Save the value of Dynamically created textbox

  20. 20

    blob created by createObjectURL save to database

  21. 21

    Save current user with created object

  22. 22

    Save tensorflow model to file

  23. 23

    Laravel 'save()' for 'hasmany' Model

  24. 24

    OpenNLP save a trained model

  25. 25

    save a tuple to a django model

  26. 26

    Is it ideal to perform save in model?

  27. 27

    Save MinMaxScaler model in sklearn

  28. 28

    to save a complicated model in backbone

  29. 29

    Cannot save a rails model

HotTag

Archive