0

I'm completely lost on how I should modify an image after it's saved. I have a model:

class Pic(models.Model):
    imgfile = FaceChopField(upload_to='img/%m/%d')

The image is uploaded just fine. I've looked up this question a bunch, and I've found a number of snippets and similar questions, but I'm still terribly confused. Yes, I have gone through a bunch of searches regarding this exact sort of confusion/issue.

Is there any way I can just:

  1. Access the saved image directory.
  2. Locate the image uploaded by name/directory.
  3. Run my modify_image(filename) on the image.
  4. Save the modified image in some other directory.

I've read through documentation on the Django site on managing files, and I've pored over StackOverflow for a while, trying different solutions. All I'm asking for is, perhaps, a straightforward approach to the aforementioned. You don't even need to show me any code if it's too much of a bother. I'm just at a loss and I don't know what I'm doing at this point, so some algorithmic layout of a solution would be great. Thank you.


Here's my current attempt:

class FaceChopFieldFile(ImageFieldFile):
    def __init__(self, *args, **kwargs):
        super(FaceChopFieldFile, self).__init__(*args, **kwargs)

    def save(self):
        super(FaceChopFieldFile, self).save()
        image_content = facechop(self.path) #my image modification function

        self.storage.save(self.path, image_content)


class FaceChopField(ImageField):
    attr_class = FaceChopFieldFile    


class Pic(models.Model): 
    imgfile =  FaceChopField(upload_to='img/%m/%d')

Anything wrong?

Mr_Spock
  • 3,815
  • 6
  • 25
  • 33

2 Answers2

0

What you really need is a hook that is called when an object is just about to be saved to DB. What I'd do is have all the image processing logic within function that overrides save method of your model. However this would slowdown the user experience of you site. In that situation what I'd recommend is writing celery task. Even better you can run a periodic task

Community
  • 1
  • 1
Sidharth Shah
  • 1,569
  • 1
  • 11
  • 14
0

I've fixed my own problem. Turned out I was overriding save() incorrectly:

class FaceChopFieldFile(ImageFieldFile):
    def __init__(self, *args, **kwargs):
        super(FaceChopFieldFile, self).__init__(*args, **kwargs)

    def save(self, name, content, save=True):
        super(FaceChopFieldFile, self).save(name, content, save)
        image_content = facechop(self.path) #my image modification function

        self.storage.save(self.path, image_content)


class FaceChopField(ImageField):
    attr_class = FaceChopFieldFile



class Pic(models.Model): 
    imgfile =  FaceChopField(upload_to='img/%m/%d')

The problem was:

def save(self):
    super(FaceChopFieldFile, self).save()

So I changed it to:

def save(self, name, content, save=True):
            super(FaceChopFieldFile, self).save(name, content, save)

It does exactly what I want it to do now.

Mr_Spock
  • 3,815
  • 6
  • 25
  • 33