Saturday, October 25, 2014

Re: Problem with Pillow image resize and save

ah, ok :)
just as another idea, you can handle the image uploads with Nginx, this will free Python from dealing static files.

Le samedi 25 octobre 2014 20:31:35 UTC+1, John a écrit :
That's a good idea, Aliane. In my case, the server was lightly loaded, the conversion time is very short and the admin was only used by a very small number of users who were sufficiently patient.

John

On 25/10/14 20:23, Aliane Abdelouahab wrote:
You have to send the picture editing to another process, because you will block the operation untill the image will be converted! 

Le samedi 25 octobre 2014 19:23:33 UTC+1, John a écrit :
On 25/10/14 15:26, Daniel Grace wrote:
I need to include the following steps, but I don't know how:
1. copy the 'picture' file
2. resize it (the copy)
3. save it to the upload directory
4. store it in the 'thumb' field
... continue as before and save the 'profile'.
--

Daniel,

I've done something similar for a simple image manager. I have a WebImage object that holds references to an image and its thumbnail. The thumbnail is created using imagemagick's convert function when the image is updated. Extracts from models.py and admin.py shown below should give you all the clues you need.

John

#################
# models.py

from django.conf import settings
from django.db import models
from django.db.models import F, Q
import os
import uuid
import subprocess

_CONVERT_CMD = '/usr/bin/convert'
_CONVERT_THUMBNAIL_OPTS = ['-thumbnail','150x50']

# ...

class WebImage(models.Model):
  "Images that are used on the web pages. Limited to certain sizes. Can be produced by an ImageGenerator."
  def get_image_file_path(inst, filename):
    fn, ext = os.path.splitext(filename)
    filename = "%s%s" % (uuid.uuid4(), ext)
    return os.path.join(settings.MEDIA_ROOT+'wi/', filename)
 
  name = models.CharField(max_length=80,help_text="Full name of the web image")
  slug = models.SlugField(max_length=50,help_text="Slug for the web image name")
  description = models.TextField(help_text="Image description")
  img = models.ImageField(null=True,upload_to=get_image_file_path,height_field="height",width_field="width")
  img_gen = models.ForeignKey('ImageGenerator', blank=True, null=True, on_delete=models.SET_NULL)
  img_size = models.ForeignKey('ImageSize', blank=True, null=True, default=None, on_delete=models.SET_NULL)
  width = models.IntegerField(editable=False,blank=True,help_text="Width of the picture. Not editable")
  height = models.IntegerField(editable=False,blank=True,help_text="Height of the picture. Not editable")
  licensing = models.TextField(blank=True,help_text="Details of image ownership, copyright and licensing. Blank for 'same as original image'")
  created_by = models.CharField(editable=False,blank=True,max_length=80, help_text="Uploader. Not editable")
  creation_date = models.DateTimeField(editable=False,blank=True,auto_now_add=True, help_text="Date/time uploaded. Not editable")
  uploaded_by = models.CharField(editable=False,blank=True,max_length=80, help_text="Uploader. Not editable")
  upload_date = models.DateTimeField(editable=False,blank=True,auto_now_add=True, help_text="Date/time uploaded. Not editable")
  tags = TaggableManager()

  def thumb_path(self):
    return os.path.join(settings.MEDIA_ROOT+'thumb/',os.path.basename(self.img.name))

  def thumb_url(self):
    return "%sthumb/%s" % (settings.MEDIA_URL,os.path.basename(self.img.name))

  def create_thumb(self):
    args = [_CONVERT_CMD, self.img.name] + _CONVERT_THUMBNAIL_OPTS + [self.thumb_path()]
    try:
      output = subprocess.call(args)
    except subprocess.CalledProcessError, e:
      self.description = "%s\n%s\n%d" % (self.description, e.output, e.returncode)
      pass # This needs to be a logger.
    self.save()

  def thumb_field(self):
    return ('<img src="%s" />' % self.thumb_url())
  thumb_field.short_description = "Thumbnail"
  thumb_field.allow_tags = True

  def get_absolute_url(self):
    return "%swi/%s" % (settings.MEDIA_URL,os.path.split(self.img.name)[1])

  def __unicode__(self):
    return "%s (%dx%d)" % (self.name,self.width,self.height)

########################
# admin.py

class WebImageAdmin(ModelAdmin):
  def save_model(self, req, obj, form, change):
    # Add the uploader info to the object
    if not change or obj.uploaded_by == '':
      obj.uploaded_by = "%s (%s %s)" % (req.user.username, req.user.first_name, req.user.last_name)
    # And the creator info in the same manner
    if not change or obj.created_by == '':
      obj.created_by = "%s (%s %s)" % (req.user.username, req.user.first_name, req.user.last_name)
    # And save
    obj.save()
    # Create a thumbnail
    obj.create_thumb()

  list_display = ("__unicode__","img_size","thumb_field",)
  list_filter = ('img_size','width','height')
  ordering = ('name','width','height')
  prepopulated_fields = {"slug": ("name",)}
  save_on_top = True
 
  form = WebImageForm

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/971addda-168d-43cc-aee5-c332cea9a3d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/733922f6-f899-4a21-bdee-84e65964bc12%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment