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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:03:07+00:00 2026-06-03T16:03:07+00:00

Having a weird issue with MemoryError in my Django project. Ubuntu 11 Apache2 Nginx

  • 0

Having a weird issue with MemoryError in my Django project.

  • Ubuntu 11
  • Apache2
  • Nginx
  • mod_wsgi
  • python2.7

I have a template tag that is resizing images using PIL and works great on my dev-machine, but in production it causes a 500 error with large images (~800px). The template tag seems to work without issue but only if the original image is already very small. Template tag is a modified version of http://djangosnippets.org/snippets/1944/

Just tried recompiling mod_wsgi for python2.7 – same error.

Is there a way to allocate more memory or maybe there something else that I am just not understanding?

Any help would be much appreciated!

Traceback emailed to me is:

    Traceback (most recent call last):

     File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response
       response = callback(request, *callback_args, **callback_kwargs)

     File "/var/www/rizzyhome.com/www/django/rizzyhome/products/views.py", line 31, in designs
       context_instance=RequestContext(request)

     File "/usr/local/lib/python2.7/dist-packages/django/shortcuts/__init__.py", line 20, in render_to_response
       return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

     File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 188, in render_to_string
       return t.render(context_instance)

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 123, in render
       return self._render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 117, in _render
       return self.nodelist.render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 744, in render
       bits.append(self.render_node(node, context))

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 757, in render_node
       return node.render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 127, in render
       return compiled_parent._render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 117, in _render
       return self.nodelist.render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 744, in render
       bits.append(self.render_node(node, context))

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 757, in render_node
       return node.render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 127, in render
       return compiled_parent._render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 117, in _render
       return self.nodelist.render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 744, in render
       bits.append(self.render_node(node, context))

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 757, in render_node
       return node.render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 64, in render
       result = block.nodelist.render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 744, in render
       bits.append(self.render_node(node, context))

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 757, in render_node
       return node.render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 64, in render
       result = block.nodelist.render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 744, in render
       bits.append(self.render_node(node, context))

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 757, in render_node
       return node.render(context)

     File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 227, in render
       nodelist.append(node.render(context))

     File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 885, in render
       return func(*func_args)

     File "/var/www/rizzyhome.com/www/django/rizzyhome/products/templatetags/yair.py", line 108, in thumbnail
       import Image, ImageOps

     File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 45, in <module>
       __import__("FixTk")

     File "/usr/lib/python2.7/lib-tk/FixTk.py", line 15, in <module>
       import ctypes

     File "/usr/lib/python2.7/ctypes/__init__.py", line 549, in <module>
       CFUNCTYPE(c_int)(lambda: None)

    MemoryError

Edit: Adding code

import os, errno

from django import template
from django.conf import settings

register = template.Library()

def _mkdir_p(path):
    try:
        os.makedirs(path)
    except OSError as exc: # Python >2.5
        if exc.errno == errno.EEXIST:
            pass
        else: raise

@register.simple_tag
def thumbnail(image_url, width, height, THIS_ROOT=settings.MEDIA_ROOT, THIS_URL=settings.MEDIA_URL, img_w=None, img_h=None, method="crop"):
    """
    Given the url to an image, resizes the image using the given width and 
    height on the first time it is requested, and returns the url to the new 
    resized image. If width or height are zero then the original ratio is 
    maintained.
    """

    if not img_w:
        img_w = width

    if not img_h:
        img_w = height

    image_url = unicode(image_url)

    # determine new paths
    resized_image_reduced = os.path.join('_resized_', image_url.replace(THIS_ROOT, ''))

    file_name, file_ext = os.path.basename(resized_image_reduced).split(os.extsep)
    resized_image_absolute_path = os.path.join(
        THIS_ROOT,
        os.path.dirname(resized_image_reduced),
        u'%s__%05d-%05d_%s%s%s' % (file_name, width, height, method, os.extsep, file_ext)
    )
    resized_image_relative_url = os.path.join(
        THIS_URL,
        os.path.dirname(resized_image_reduced),
        u'%s__%05d-%05d_%s%s%s' % (file_name, width, height, method, os.extsep, file_ext)
    )

    # make sure target directory exists
    _mkdir_p(os.path.realpath(os.path.dirname(resized_image_absolute_path)))

    # abort if thumbnail exists, original image doesn't exist, invalid width or 
    # height are given, or PIL not installed, or thumbnail is newer than source image
    if os.path.exists(resized_image_absolute_path):
        if os.path.exists(image_url):
            if os.path.getmtime(resized_image_absolute_path) > os.path.getmtime(image_url):
                return resized_image_relative_url
        else:
            return resized_image_relative_url
    if not image_url:
        return ""

    try:
        width = int(width)
        height = int(height)
    except ValueError:
        return resized_image_relative_url

    if not os.path.exists(image_url) or (width == 0 and height == 0):
        return image_url

    try:
        from PIL import Image, ImageOps
    except ImportError:
        return resized_image_relative_url

    # open image, determine ratio if required and resize/crop/save
    image = Image.open(image_url)
    if width == 0:
        width = image.size[0] * height / image.size[1]
    elif height == 0:
        height = image.size[1] * width / image.size[0]
    if image.mode not in ("L", "RGB"):
        image = image.convert("RGB")

    try:
        if method == 'scale':

            image.thumbnail((width, height), Image.ANTIALIAS)


            new_w, new_h = image.size

            x1 = (img_w-new_w) /2
            y1 = (img_h-new_h)

            new_image = Image.new(image.mode, (img_w, img_h), (255,255,255))
            new_image.paste(image, (x1,y1))

            new_image.save(
                resized_image_absolute_path,
                "JPEG",
                quality=100
            )

        elif method == 'crop':
            image = ImageOps.fit(image, (width, height), Image.ANTIALIAS)
            image.save(
                resized_image_absolute_path,
                "JPEG",
                quality=100
            )

    except Exception, e:
        raise
        return resized_image_relative_url

    return resized_image_relative_url
  • 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-03T16:03:10+00:00Added an answer on June 3, 2026 at 4:03 pm

    Thanks so much for commenting and offering assistance.

    Apparently there is a weird bug in the __init__.py for the CTYPE module. – or whatever, I just read it 🙂

    The fix, for me at least, was to comment out the last line in the __init__.py file. Seems that the last line is specifically for windows 64bit and is messing up my Ubuntu.

    http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=598727#38

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

Sidebar

Related Questions

I'm having a weird issue that I can't track down... For context, I have
Having a weird issue. I'm new to Macs and have a windows VM that
I am having a weird issue with my profiles. For my project, i have
Im having a very weird issue. I have a normal ajax call that upload
I'm having a weird issue with my Silverlight 4 project, although it's one I've
I am having some weird issue here. I have a database table which has
I having a weird issue with Django 1.3. When I try to add a
I am having a weird issue with CSS that will has to be simple
I am having a real weird issue. I have a dropdownlist which has a
I'm having a weird issue with a button that is being shown on screen,

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.