Guest User

Untitled

a guest
Jan 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. rom django.db import models
  2. from PIL import Image
  3. from io import BytesIO
  4. from django.core.files.uploadedfile import InMemoryUploadedFile
  5. import sys
  6.  
  7. # Create your models here.
  8.  
  9. class Modify(models.Model):
  10. img = models.ImageField()
  11.  
  12. def save(self):
  13. #Opening the uploaded image
  14. im = Image.open(self.img)
  15.  
  16. output = BytesIO()
  17.  
  18. #Resize/modify the image
  19. m.resize( (100,100) )
  20.  
  21. #after modifications, save it to the output
  22. im.save(output, format='JPEG', quality=100)
  23. output.seek(0)
  24.  
  25. #change the imagefield value to be the newley modifed image value
  26. self.img = InMemoryUploadedFile(output,'ImageField', "%s.jpg" %self.img.name.split('.')[0], 'image/jpeg', sys.getsizeof(output), None)
  27.  
  28. super(Modify,self).save()
Add Comment
Please, Sign In to add comment