简体   繁体   English

调整大小/裁剪图像编码为base64图像字符串

[英]Resize/crop image encoded as base64 image string

in my case, there are 2 ways of getting image to resize/crop. 在我的情况下,有两种方法可以调整图像大小/裁剪。

  1. upload normal image file 上传普通图像文件
  2. giving base64 string data of image 给出图像的base64字符串数据

in 1. case, resize and crop is working well: 1.案例,调整大小和作物运作良好:

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))

resized/cropped image: 调整大小/裁剪图像: 在此输入图像描述

in 2. case, the code is the same as above with slight change in: 在2.情况下,代码与上面相同,略有变化:

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

but the resized/cropped image: 但调整大小/裁剪的图像: 在此输入图像描述

why is it in 2.case bad in quality and size? 为什么它的质量和尺寸都不好? (black bottom part..) what am I doing wrong? (黑底部分..)我做错了什么? am I reading the base64 string in wrong way? 我是以错误的方式读取base64字符串吗?

I found a website which has many interesting things in it.It has 2(there are many) tools which maybe can help you.The 1th tool converts image to base64 and the 2th tool minifies the size of image (up to 70% save). 我找到了一个网站里面有很多有趣的东西。它有2个(有很多)工具可能对你有帮助。第1个工具将图像转换为base64,第2个工具缩小图像尺寸(最多70%保存) 。

http://www.w3docs.com/tools/minimage/ http://www.w3docs.com/tools/minimage/

http://www.w3docs.com/tools/image-base64 http://www.w3docs.com/tools/image-base64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM