2015-03-02 53 views
0

在我的情況下,有兩種獲取調整大小/裁剪圖像的方式。在1的情況下圖像 調整大小/作物圖像編碼爲base64圖像字符串

  1. 上傳正常的圖像文件
  2. 給人的base64字符串數據,調整大小和作物正常工作:

    f = Image.open(uploaded_image) 
    new_width, new_height = 1200, 630 
    wpercent = (new_width/float(f.size[0])) 
    hsize = int((float(f.size[1]) * float(wpercent))) 
    
    if f.mode != "RGB": 
        f = f.convert('RGB') 
    
    og_img = None 
    if f.size[0] < new_width: 
        #upscale 
        og_img = f.resize((new_width, hsize), Image.BICUBIC) 
    
    elif f.size[0] >= new_width: 
        #downscale 
        og_img = f.resize((new_width, hsize), Image.ANTIALIAS) 
    
    og_img = og_img.crop((0, 0, 1200, 630)) 
    

    調整/裁剪圖像: enter image description here

    在2.的情況下,代碼是s

    base64_image = str(request.POST.get('base64_image')).split(',')[1] 
    imgfile = open('/'.join([settings.MEDIA_ROOT, 'test.png' ]), 'w+b') 
    imgfile.write(decodestring(base64_image)) 
    imgfile.seek(0) 
    f = Image.open(imgfile) 
    
    #.. as above 
    

    但調整/裁剪圖像: enter image description here

    爲什麼會在2.case壞在質量和規模與上面的略有改變火焰? (黑底部分..)我做錯了什麼?我是否以錯誤的方式讀取base64字符串?

+1

你檢查的情況下,2(操作前)寫入到磁盤上的實際文件?這將告訴轉移或解碼是否失敗,將問題減半。 – spectras 2015-03-02 13:31:03

回答