File 和 ImageFile 接受 Python 的 file 或 StringIO 物件
而 ContentFile 接受 string
ref:
https://docs.djangoproject.com/en/dev/ref/files/file/#the-file-object
Django Form
image_file = request.FILES['file']
# 方法一
profile.mugshot.save(image_file.name, image_file)
# 方法二
profile.mugshot = image_file
profile.save()
open('/path/to/file.png')
from django.core.files import File
with open('/home/vinta/image.png', 'rb') as f:
profile.mugshot = File(f)
profile.save()
Django ContentFile
import os
import uuid
from django.core.files.base import ContentFile
import requests
url = 'http://vinta.ws/static/photo.jpg'
r = requests.get(url)
file_url, file_ext = os.path.splitext(r.url)
file_name = '%s%s' % (str(uuid.uuid4()).replace('-', ''), file_ext)
profile.mugshot.save('123.png', ContentFile(r.content), save=False)
# 如果 profile.mugshot 是 ImageField 欄位的話
# 可以用以下的方式來判斷它是不是合法的圖檔
try:
profile.mugshot.width
except TypeError:
raise RuntimeError('圖檔格式不正確')
profile.save()
Data URI, Base64
from binascii import a2b_base64
from django.core.files.base import ContentFile
data_uri = 'data:image/jpeg;base64,/9j/4AAQSkZJRg....'
head, data = data_uri.split(',')
binary_data = a2b_base64(data)
# 方法一
profile.mugshot.save('whatever.jpg', ContentFile(binary_data), save=False)
profile.save()
# 不能用這種方式,因為少了 file name
profile.mugshot = ContentFile(binary_data)
profile.save()
# 方法二
f = open('image.png', 'wb')
f.write(binary_data)
f.close()
# 方法三
from StringIO import StringIO
from PIL import Image
img = Image.open(StringIO(binary_data))
print img.size
ref:
https://stackoverflow.com/questions/19395649/python-pil-create-and-save-image-from-data-uri
StringIO, PIL image
你就把 StringIO 想成是 open('/home/vinta/some_file.txt', 'rb')
的 file 物件
from StringIO import StringIO
from PIL import Image
import requests
r = requests.get('http://vinta.ws/static/photo.jpg')
img = Image.open(StringIO(r.content))
print pil_image.size
StringIO, PIL image, Django
from StringIO import StringIO
from django.core.files.base import ContentFile
from PIL import Image
img = Image.open(instance.file)
# or
raw_img_io = StringIO(binary_data)
img = Image.open(raw_img_io)
img = img.resize((524, 328), Image.ANTIALIAS)
img_io = StringIO()
img.save(img_io, 'PNG', quality=100)
profile.image.save('whatever.png', ContentFile(img_io.getvalue()), save=False)
profile.save()
ref:
https://stackoverflow.com/questions/3723220/how-do-you-convert-a-pil-image-to-a-django-file
Download file from URL, tempfile
import os
import tempfile
import requests
import xlrd
try:
file_path = report.file.path
temp = None
except NotImplementedError:
url = report.file.url
r = requests.get(url, stream=True)
file_url, file_ext = os.path.splitext(r.url)
# delete=True 會在 temp.close() 之後自己刪掉
temp = tempfile.NamedTemporaryFile(prefix='report_file_', suffix=file_ext, dir='/tmp', delete=False)
file_path = temp.name
with open(file_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
wb = xlrd.open_workbook(file_path)
# 因為是 tempfile.NamedTemporaryFile(delete=False)
# 所以你要自己刪掉
try:
os.remove(temp.name)
except AttributeError:
pass
ref:
https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests