Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8806621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:10:18+00:00 2026-06-14T02:10:18+00:00

I would like to scale an ImageField before the model gets saved to the

  • 0

I would like to scale an ImageField before the model gets saved to the disk, but somehow get an unreadable image out. The goal is to scale it without ever saving it to the disk.

This is my attempt so far:

IMAGE_MAX_SIZE = 800, 800
class Picture(models.Model):
    ...
    image = models.ImageField(upload_to='images/%Y/%m/%d/')

    # img is a InMemoryUploadedFile, received from a post upload
    # removing the scale function results in a readable image
    def set_image(self, img):
        self.image = img
        self.__scale_image()

    def __scale_image(self):
        img = Image.open(StringIO(self.image.read()))
        img.thumbnail(IMAGE_MAX_SIZE, Image.ANTIALIAS)
        imageString = StringIO()
        img.save(imageString, img.format)
        self.image.file = InMemoryUploadedFile(imageString, None, self.image.name, self.image.file.content_type, imageString.len, None)

I’m not getting an error, but the resulting image can not be displayed correctly. Any ideas how to correct this?

Thanks
Simon

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:10:19+00:00Added an answer on June 14, 2026 at 2:10 am

    I was close, but not quite there. This function works now fine and the image does not get saved to the disc at any point during the scaling.

    IMAGE_MAX_SIZE = 800, 800
    class Picture(models.Model):
        ...
        image = models.ImageField(upload_to='images/%Y/%m/%d/')
    
        # img is a InMemoryUploadedFile, received from a post upload
        def set_image(self, img):
            self.image = img
            self.__scale_image(self.image, IMAGE_MAX_SIZE)
    
        def __scale_image(self, image, size):
            image.file.seek(0) # just in case
            img = Image.open(StringIO(image.file.read()))
            img.thumbnail(size, Image.ANTIALIAS)
            imageString = StringIO()
            img.save(imageString, img.format)
    
            # for some reason content_type is e.g. 'images/jpeg' instead of 'image/jpeg'
            c_type = image.file.content_type.replace('images', 'image')
            imf = InMemoryUploadedFile(imageString, None, image.name, c_type, imageString.len, None)
            imf.seek(0)
            image.save(
                    image.name,
                    imf,
                    save=False
                )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to build some smaller scale but hightly customized documentation sites for
I have an image that I would like to scale to full screen. How
I would like to scale up and down a background image in my page.
I would like to Scale an Image from 640x480 to 64x48. There are different
I currently have a web application that I would like to scale out using
I would like to know, in a practical sense, on a large scale (Java
I would like to know if the following is possible in Scala (but I
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
I would like to scale a lable/text when printing. Below is the sample code
I would like to ask a question about a new and large scale web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.