Guest User

Untitled

a guest
Feb 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. from django.core.files.storage import default_storage
  2. print default_storage.__class__
  3. from django.core.files.base import ContentFile
  4.  
  5. import io
  6. import random
  7.  
  8.  
  9. def generate_photo_file():
  10. from PIL import Image
  11. file = io.BytesIO()
  12. red = random.randrange(0, 255, 10)
  13. green = random.randrange(0, 255, 10)
  14. blue = random.randrange(0, 255, 10)
  15. image = Image.new('RGBA', size=(100, 100), color=(red, green, blue))
  16. image.save(file, 'png')
  17. # when names collide(which certainly will)
  18. # a random string is by default(implicitly) appended to the name[test.png]
  19. file.name = 'test123.png'
  20. file.seek(0)
  21. return file
  22.  
  23. file_name = 'testimage1234444.png'
  24. from app.models import *
  25. test_image = Image()
  26. file = generate_photo_file()
  27. test_image.image.save(file_name, ContentFile(file.getvalue()), save=True)
  28. file = default_storage.open(file_name, 'r')
  29. file.blob.make_public()
  30. image_url = test_image.image.url
Add Comment
Please, Sign In to add comment