Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

How to get a mock image in django?

from io import StringIO
from PIL import Image
from django.core.files.base import File

# Just invoke this function and you'll get a png file
def get_mock_img(name='test.png', ext='png', size=(50, 50), color=(256, 0, 0)):
    file_obj = StringIO()
    image = Image.new("RGB", size=size, color=color)
    image.save(file_obj, ext)
    file_obj.seek(0)
    return File(file_obj, name=name)
 
PREVIOUS NEXT
Tagged: #How #mock #image
ADD COMMENT
Topic
Name
7+8 =