画像をアップロードしています。画像をアップロードしてdjangoモデルに保存すると、問題なく動作します。サムネイルを作成し、一時的に保存します。場所も機能します。動作しない部分はサムネイルの保存で、ファイルを作成して保存するが空のファイルです。:/ 不足しているデータの問題を解決するにはどうすればよいですか。
誰かが pil イメージを django モデル - imagefield に tmp 保存せずに変換する方法を知っている場合は、教えてください。
def ajax_upload(request):
if request.method == 'POST':
newfile = Image()
newfile.user = request.user
file_content = ContentFile(request.raw_post_data)
file_name = request.GET.get('file')
newfile.image.save(file_name, file_content)
# thumbnail creation ==========================================
path = os.path.join(settings.MEDIA_ROOT, newfile.image.url)
thumb_image = pil_image.open(path)
# ImageOps compatible mode
if thumb_image.mode not in ("L", "RGB"):
thumb_image = thumb_image.convert("RGB")
thumb_image_fit = ImageOps.fit(thumb_image, (32, 32), pil_image.ANTIALIAS)
#saving temp file
tmp_file_path = os.path.join(settings.MEDIA_ROOT, 'tmp_thumbnail.jpg')
thumb_image_fit.save(tmp_file_path, 'JPEG', quality=75)
#opening the tmp file and save it to django model
thumb_file_data = open(tmp_file_path)
thumb_file = File(thumb_file_data)
newfile.thumbnail.save(file_name, thumb_file)
#===============================================================
results = {'url': newfile.image.url, 'id': newfile.id, 'width': newfile.image.width, 'height': newfile.image.height}
return HttpResponse(json.dumps(results))
raise Http404