Thanks Kelly, this looks like what I was looking for. Will give it a try. So far my self implementation looks like this:

  def medium_image(self):
        return self.get_cache_image_path((400, 400))

    def small_image(self):
        return self.get_cache_image_path((128, 128))

 def get_cache_image_path(self, size):
        """Create cached image file and return the path to this file"""
        #split file path to list to separate folder and file
        folder_file = self.image.__str__().split('/')
        #get base filename with extension
        file_name = folder_file.pop()

        #get full folder name
        folder_name = "/".join(folder_file)

        #extract file extension from file name
        extension = file_name[-3:]
        #extract clean file name for cached file
        cached_file_name = file_name[:-4]
        #create unique cached file name from size
        cached_file_name += "_%sx%s" % size
        cached_file_name += ".%s" % extension

        self.create_cached_image_file(cached_file_name, folder_name, size)

        #return path to cached file
return settings.MEDIA_URL + folder_name + '/cache/' + cached_file_name

 def create_cached_image_file(self, cached_file, folder, cache_file_size):
"""Create cache folder and cache image file but only if they don't exist already"""
        cached_folder = settings.MEDIA_ROOT + folder + "/cache/"
        #if cached folder does not exist create it
        if not os.path.isdir(cached_folder):
            os.mkdir(cached_folder)

        #if file does not exist already then create it
        if not os.path.isfile(cached_folder + cached_file):
            im = Image.open(self.image.path)
            im.thumbnail(cache_file_size, Image.ANTIALIAS)
            im.save(cached_folder + cached_file)

This is within my model and in my template I can call {{model.medium_image}} or {{model.small_image}}. Works so far but is not "DRY" and also still I create a copy of each uploaded image. Will now check sorl-thumbnail.



On 02/03/2013 05:49 PM, Kelly Nicholes wrote:
Check out sorl-thumbnail. https://github.com/sorl/sorl-thumbnail . This lets you specify the desired size in a template.and can deal with cropping. It caches the results so it doesn't have to regenerate it each time.

On Saturday, February 2, 2013 1:33:36 PM UTC-7, nYmo wrote:

    Hi all,
    I'm new to django and also python but have already some
    programming experience. I'm currently creating my first
    application in django and get stucked because I'm looking for the
    best way to resize uploaded images.
    I'm already so far that I can upload/delete/update my images and
    show them in my view. Now I want to resize the images for my view
    and thought about the best way?

    First question: Is it possible to resize the images on the fly?
    For example I uploaded an image in 1920x1080px and now want to
    transform it to 400x200 or something similar when the view is
    being loaded? Is this a convenient way in django or not?

    The only other way in my opinion could be to resize the image
    during the file is being uploaded. What I don't like about this is
    that I have more than one copy of one image only because of
    different image sizes.

    Any other thoughs?

    I know that there are some nice packages out there where such
    problems are already solved. But since I'm new to django I want to
    learn by myself how to accomplish the basic stufff :)

    Thanks in advance

    Regards nymo

--
You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to