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

  • SEARCH
  • Home
  • 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 8497531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:09:06+00:00 2026-06-11T00:09:06+00:00

So I have a form in Django that is used to upload an image

  • 0

So I have a form in Django that is used to upload an image to an ImageField. This works well. I then pass off the request.FILES[‘image’] off to a function called MakeThumbnail for further processing.

MakeThumbnail is not working too well. I have assembled this from a series of StackOverflow threads, but I think I am missing some crucial concepts. Here is my code.

    def MakeThumbnail(file):
        img = Image.open(file)
        thumbnail = img.thumbnail((128, 128), Image.ANTIALIAS)
        thumbnailString = StringIO.StringIO(thumbnail)
        newFile = ContentFile(thumbnailString)
        return newFile

The stacktrace says I am failing on this line: newFile = ContentFile(thumbnailString). It gives me the following error: expected read buffer, instance found

I think I might be failing back at thumbnail = img.thumbnail((128, 128), Image.ANTIALIAS), though, after looking at the local variables:

thumbnailString : <StringIO.StringIO instance at 0x0000000003B39748>
file : <InMemoryUploadedFile: Desert.jpg (image/jpeg)>
thumbnail : None
img : <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x96 at 0x3B25C08>

Notice that thumbnail is none. It should contain something. Does anyone have some hints for me?

thank you!

  • 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-11T00:09:07+00:00Added an answer on June 11, 2026 at 12:09 am

    Thank you so much for the help, Jan Spurny. I figured it out with your help. So, when img is saved to thumbnailString, a few things need to happen. The code above actually produces an error because we pass an instance to ContentFile instead of a buffer. No matter, we just need to call thumbnailString.read(). But thumbnailString.read() returns ”. Turns out we need to do thumbnailString.seek(0) beforehand. But this did not work either!

    Ultimately, I was able to get the image file back to a Django file using InMemoryUploadedFile.

    Here is what the code looks like, from beginning to end:

    from django.core.files.uploadedfile import InMemoryUploadedFile
    import StringIO
    def MakeThumbnail(file):
        img = Image.open(file)
        img.thumbnail((128, 128), Image.ANTIALIAS)
        thumbnailString = StringIO.StringIO()
        img.save(thumbnailString, 'JPEG')
        newFile = InMemoryUploadedFile(thumbnailString, None, 'temp.jpg', 'image/jpeg', thumbnailString.len, None)
        return newFile
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form in Django that looks like this class FooForm(forms.ModelForm): foo_field =
I have a Django form that uses a different number of fields based on
I have a Django form that needs to do some client-side validation before submitting
I have a Django form that uses an integer field to lookup a model
I have a form in Django with a TextField that receives comma separated input
I have a factory method that generates django form classes like so: def get_indicator_form(indicator,
I have a form in my django app where users can upload files. How
I have a web report that uses a Django form (new forms) for fields
I have a Django form that is part of page. Lets say I have
I have a working django form that submits and the django view handles the

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.