1

我正在上傳圖片,圖片上傳並將其保存在django模型中工作得很好。創建縮略圖並將其保存爲temp。位置也起作用。不工作的部分是保存縮略圖,文件被創建並保存,但是它是一個空文件。 :/ 如何解決缺少數據的問題。python pil image to django imagefield轉換

如果有人知道如何將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 

回答

1
from django.core.files.base import ContentFile 
from PIL import ImageOps, Image as pil_image 
import os.path 
import json 


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) 
     newfile.thumbnail.save(file_name, file_content) 

     #opening and resizing the thumbnail 
     path = os.path.join(settings.MEDIA_ROOT, newfile.thumbnail.url) 
     thumb_file = pil_image.open(path) 

     if thumb_file.mode not in ("L", "RGB"): 
      thumb_file = thumb_file.convert("RGB") 

     thumb_image_fit = ImageOps.fit(thumb_file, (100, 100), pil_image.ANTIALIAS) 
     thumb_image_fit.save(path) 

     #=============================================================== 

     results = { 
        'image': 
         { 
          'url': newfile.image.path, 
          'width': newfile.image.width, 
          'height': newfile.image.height 
         }, 
        'thumbnal': 
         { 
          'url': newfile.thumbnail.path, 
          'width': newfile.thumbnail.width, 
          'height': newfile.thumbnail.height 
         } 
        } 
     return HttpResponse(json.dumps(results)) 
    raise Http404 
1

您可以保存文件並將其路徑(相對於MEDIA_ROOT)手動分配到目標字段。

thumb_path = 'uploads/1_small.jpg' 
thumb_image_fit.save(os.path.join(MEDIA_ROOT, thumb_path), 'JPEG', quality=75) 
newfile.thumbnail = thumb_path 

當然,你需要手動完成所有Django的東西 - 檢查文件是否存在,修改名稱,如果是的話,等等。

+0

我現在不能測試,但newfile.thumbnail是ImageField的,我不認爲「newfile.thumbnail = thumb_path」都會好的。如果我得到它的權利imagefield是像這樣保存:newfile.thumbnail.save(file_name,thumb_file)(file_name是一個字符串和thumb_file必須是文件或內容文件) – Aleksandrenko 2012-04-18 06:14:48

+0

是,'obj.field.save(name,content)是更正確和原始記錄的方式。但'FileField'只存儲數據庫中的路徑,並允許直接分配該路徑。這是無證的功能,但它的工作原理。 – ilvar 2012-04-19 04:22:03

+0

謝謝你,我會記住未來。 – Aleksandrenko 2012-04-19 05:15:42