Using flutter flutter_image_compress to upload base64 compressed file but cant uncompress in python

huangapple go评论69阅读模式
英文:

Using flutter flutter_image_compress to upload base64 compressed file but cant uncompress in python

问题

以下是翻译好的部分:

I can successfully upload a file to s3 by

  1. using flutter_image_compress to compress and
  2. converting the file to base64.

现在我可以解码文件:

base64_string = open("img1b95b3ed9e494595b01610f06d9f074b.txt", "r").readlines()[0]

Now after that all fails:

msg = base64.b64decode(base64_string)
inflated = zlib.decompress(msg)

inflated = zlib.decompress(msg)
zlib.error: Error -3 while decompressing data: incorrect header check

So what is the proper method to decode in python from using the flutter flutter_image_compress package?

Thanks

PS this is now I compress in flutter:

以下是未翻译的部分,因为它们是代码或者与翻译无关的内容:

Future<Uint8List?> testCompressFile(File file) async {
var result = await FlutterImageCompress.compressWithFile(file.absolute.path,
quality: 100,
keepExif: true
);
return result;
}

英文:

I can successfully upload a file to s3 by

  1. using flutter_image_compress to compress and
  2. converting the file to base64.

Now I can decode the file:

 base64_string = open(&quot;img1b95b3ed9e494595b01610f06d9f074b.txt&quot;, &quot;r&quot;).readlines()[0]

Now after that all fails:

 msg = base64.b64decode(base64_string)
 inflated = zlib.decompress(msg)

inflated = zlib.decompress(msg)
zlib.error: Error -3 while decompressing data: incorrect header check

So what is the proper method to decode in python from using the flutter flutter_image_compress package?

Thanks

PS this is now I compress in flutter:

Future&lt;Uint8List?&gt; testCompressFile(File file) async {
  var result = await FlutterImageCompress.compressWithFile(file.absolute.path,
      quality: 100,
      keepExif: true
      );
  return result;
}

答案1

得分: 1

解码Base64将给你一个uint8List,在Python中,这相当于byteArray。请尝试将该byteArray转换为bytes以获得二进制数据。你不需要使用zlib

listOfInt = [1, 2, 3, 4, 5, 6]
byteArray = bytearray(listOfInt)
print(byteArray) # bytearray(b'\x01\x02\x03\x04\x05\x06')
byteObj = bytes(byteArray)
print(byteObj) # b'\x01\x02\x03\x04\x05\x06'
英文:

decoding base64 would give you uint8List which in python's terms would be a byteArray. Please try converting that byteArray into bytes to gain the binary data. You don't need the zlib:

listOfInt = [1, 2, 3, 4, 5, 6]
byteArray = bytearray(listOfInt)
print(byteArray) # bytearray(b&#39;\x01\x02\x03\x04\x05\x06&#39;)
byteObj = bytes(byteArray)
print(byteObj) # b&#39;\x01\x02\x03\x04\x05\x06&#39;

huangapple
  • 本文由 发表于 2023年2月27日 00:33:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定