def split_image_bs4(file, max_len=720): """ 切割图片 :param file: 二进制文件 :param max_len: 最大长度 :return: bs64 """ # f = base64.b64decode(b64) # with io.BytesIO(f) as im: im = io.BytesIO(file) img = Image.open(im) w, h = img.size # print(w, h) v = max(w, h) if v > max_len: if v == w: final_w = max_len final_h = h * (max_len/w) else: final_w = w * (max_len / h) final_h = max_len # print(final_w) final_img = img.resize((int(final_w), int(final_h)), Image.ANTIALIAS) # print(type(final_img)) img_byte = io.BytesIO() final_img.save(img_byte, 'png') b64 = base64.b64encode(img_byte.getvalue()) return b64 else: return base64.b64encode(file)