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 6931093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:31:57+00:00 2026-05-27T11:31:57+00:00

I just started using Django and Python and I’m trying to build a photo

  • 0

I just started using Django and Python and I’m trying to build a photo app. This script is generating thumbnails and I’d like to do that myself. Unfortunately I don’t understand what StringIO() is doing. The Python Docs aren’t very helpful to me in that case.

Can someone please explain to me what StringIO() does in this particular case?

From http://djangosnippets.org/snippets/1172/:

def save(self):
    from PIL import Image
    #Original photo
    imgFile = Image.open(self.image.path)

    #Convert to RGB
    if imgFile.mode not in ('L', 'RGB'):
        imgFile = imgFile.convert('RGB')

    #Save a thumbnail for each of the given dimensions
    #The IMAGE_SIZES looks like:
    #IMAGE_SIZES = { 'image_web'      : (300, 348),
    #                'image_large'    : (600, 450),
    #                'image_thumb'    : (200, 200) }
    #each of which corresponds to an ImageField of the same name
    for field_name, size in self.IMAGE_SIZES.iteritems():
        field = getattr(self, field_name)
        working = imgFile.copy()
        working.thumbnail(size, Image.ANTIALIAS)
        fp = StringIO()
        working.save(fp, "JPEG", quality=95)
        cf = ContentFile(fp.getvalue())
        field.save(name=self.image.name, content=cf, save=False);

    #Save instance of Photo
    super(Photo, self).save()
  • 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-05-27T11:31:57+00:00Added an answer on May 27, 2026 at 11:31 am

    StringIO is a class which can be used as a file-like object. You can use it exactly as you would a regular file, except instead of the data being written to disk, it will be written to a buffer ( a string buffer ) in memory.

    In this script it looks like the image is first saved to a StringIO memory buffer, and after that the value of the string is retrieved and passed to the constructor for ContentFile to create a new instance of ContentFile, which is then passed to the field save function.

    I would presume that the reason the script is using StringIO is that the constructor for ContentFile takes in a string, and writting to and then reading a StringIO file is the easiest way to get the image contents represented as a string.

    As a side note, I would like to suggest that you look at Django’s ImageFile field type, it has been more than enough for my image related needs, and is more clear than going through StringIO and ContentFiles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started Django and Python 50 hours ago ;-) so this might be
I have made an online gallery using Python and Django. I've just started to
I'm new to both Django and Python.. I just started using them a few
I just started using django for development. At the moment, I have the following
I just started using CCNet, and in the process of getting my build projects
I just started using Selenium 2 tonight so this will be very basic (I
I just recently started using Jinja2 for HTML templating on Google App Engine and
Just started using log4net and trying to get my head around the config and
I started using django framework just a few days ago and i desperately need
Just started using this technique and am having strange results on the PC side.

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.