2015-03-02 94 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

回答