0

I have created a multi image upload form in a Django where the user can upload personal images to his/her profile.

I want to find/create a python function where convert the original image from user to png and that new convert image update the png_image field from my model.any idea how to do that? Wow to do this automate for any image upload?

models.py

class MyModel(models.Model):
    name = models.TextField()
    slug_name = models.SlugField()
    user = models.ForeignKey(User, unique=False)
    original_image = models.ImageField(upload_to=directory_path)
    png_image = models.ImageField(upload_to=directory_path)

views.py

def dataset(request):
    uploadimages = UploadImagesForm(request.POST or None, request.FILES or None)
    if uploadimages.is_valid():
        if request.FILES.get('multipleimages', None) is not None:
            images = request.FILES.getlist('multipleimages')
            for image in images:
                ...........
matiit
  • 7,969
  • 5
  • 41
  • 65
Mar
  • 683
  • 2
  • 11
  • 26
  • 1
    Thats not much to do with Django, but rather `Pillow`. `Pillow` or `PIL` is a python library for image processing. Google it and you will find what you need. Hope this helps! You would have to make a preprocessor that converts the image to png first, and then submits the form to be saved to the model. – N. Ivanov Dec 15 '17 at 15:44
  • @N. Ivanov can you show me some example about this ? – Mar Dec 15 '17 at 15:56
  • 1
    Refer to [this](https://stackoverflow.com/a/10759132/7654934). Hope this helps! – N. Ivanov Dec 15 '17 at 15:57
  • @N. Ivanov from `PIL import Image im = Image.open('myimage.tif') im.save('myimage.png')` not work for my I have this error : `raise IOError("cannot write mode %s as PNG" % mode) IOError: cannot write mode F as PNG` – Mar Dec 15 '17 at 16:15

0 Answers0