How do you convert a PIL `Image` to a Django `File`? The way to do this without having to write back to the filesystem, and then bring the file back into memory via an open call, is to make use of String. IO and Django In. File = urllib2.urlopen(url) buf = StringIO(file.read()) b64png = 'http:// 이 장소를 Daum. 태그 - Aptana, django, Python, 서울특별시 구로구 가리봉동 |. Source code for django.core.files.base. class File (FileProxyMixin). django. django.core.files.base; Getting help FAQ Try the FAQ — it's got answers to many common questions. Index. Memory. Uploaded. File. Here is a quick sample on how you might do this. This assumes that you already have a thumbnailed image named 'thumb': import String. StringIO — Read and write strings as files¶ This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files). See the description of file objects for operations. File wrappers¶ A view return DownloadResponse which itself carries a file wrapper. Here are file wrappers distributed by Django and django-downloadview. Example: Using the StringIO module to read from a static file. Example: Using the StringIO module to write to a memory file. rendered by a django application. IO. from django. core. In. Memory. Uploaded. File. # Create a file- like object to write thumb data (thumb data previously created. PIL, and stored in variable 'thumb'). String. IO. String. IO(). thumb. save(thumb_io, format='JPEG'). Create a new Django file- like object to be used in models as Image. Field using. # In. Memory. Uploaded. File. If you look at the source in Django, a. Simple. Uploaded. File is essentially instantiated similarly to what is shown here. In. Memory. Uploaded. File(thumb_io, None, 'foo. None). # Once you have a Django file- like object, you may assign it to your Image. Field. Let me know if you need more clarification. I have this working in my project right now, uploading to S3 using django- storages. This took me the better part of a day to properly find the solution here.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |