2

I have to resize an image and upload it to server using django, currently i am using fileSystem storage for uploading file, but and my code is

def create_new_event(request, steps=None):
    if request.method == 'POST':
        stepFirstForm = CreateEventStepFirstForm(request.POST, request.FILES)
        if stepFirstForm.is_valid():
            if request.FILES['artist_image']:
                randomArtistImage = ''.join(random.choice('0123456789ABCDEFabcdefghijklmn') for i in range(8));
                myfile = request.FILES['artist_image']
                fs = FileSystemStorage()
                fileName, fileExtension = os.path.splitext(myfile.name)
                artistImageName = randomArtistImage+'-'+str(request.user.id)+str(fileExtension)
                filename = fs.save('event_artists_images/'+artistImageName, myfile)
                uploaded_artist_image_url = fs.url(filename)

Using this code i can move image to event_artists_images folder, but i want to also resize this image upto 300*300 pixels. How can i do this. If i can use PIL Image class then on server how can i upload and resize file.

1 Answers1

1

This question has been answered several times, unless I am missing something. The answers here should help for example: Django resize image during upload or here https://blog.louwii.fr/2016/03/django-resize-an-image-on-upload/

Community
  • 1
  • 1
Yom86
  • 196
  • 1
  • 6
  • Yes this question has several answers but i want to resize it in django view not in model for modelForm – Pankaj Sharma Oct 25 '16 at 09:12
  • I don't understand why you need to make it in the view. You can rename and resize the image when you save it (in your save() function). Can you bemore specific about the need? The result will be the same (resized image is saved), no? – Yom86 Oct 25 '16 at 09:37
  • If that is what you really want, you can use "from PIL import Image" directly in the view too, so commands would be the same. Did you try it? https://wellfire.co/learn/python-image-enhancements/ – Yom86 Oct 25 '16 at 09:51