Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.core.files.storage import default_storage
- print default_storage.__class__
- from django.core.files.base import ContentFile
- import io
- import random
- def generate_photo_file():
- from PIL import Image
- file = io.BytesIO()
- red = random.randrange(0, 255, 10)
- green = random.randrange(0, 255, 10)
- blue = random.randrange(0, 255, 10)
- image = Image.new('RGBA', size=(100, 100), color=(red, green, blue))
- image.save(file, 'png')
- # when names collide(which certainly will)
- # a random string is by default(implicitly) appended to the name[test.png]
- file.name = 'test123.png'
- file.seek(0)
- return file
- file_name = 'testimage1234444.png'
- from app.models import *
- test_image = Image()
- file = generate_photo_file()
- test_image.image.save(file_name, ContentFile(file.getvalue()), save=True)
- file = default_storage.open(file_name, 'r')
- file.blob.make_public()
- image_url = test_image.image.url
Add Comment
Please, Sign In to add comment