我有一个TIFF图像类型的base64编码的字符串,我想使用python将其转换为PNG图像。我看到了一个可以操纵tiff文件的包装枕头,但是如何进行给定的“ TIFF的base64”?
答案 0 :(得分:2)
base64
至tiff
:
import base64
text_base64 = 'SUkqAAgAAAASAP4ABAABAAAAAAAAAAABAwABAAAACgAAAAEBAwABAAAACgAAAAIBAwADAAAA5gAAAAMBAwABAAAAsoAAAAYBAwABAAAAAgAAABEBBAABAAAAgAMAABIBCQABAAAAAQAAABUBAwABAAAAAwAAABYBAwABAAAAKAAAABcBBAABAAAAPwAAABoBBQABAAAA7AAAABsBBQABAAAA9AAAABwBAwABAAAAAQAAACgBAwABAAAAAgAAADIBAgAUAAAA/AAAAD0BAwABAAAAAgAAAGmHBAABAAAAEAEAAF4BAAAIAAgACABIAAAAAQAAAEgAAAABAAAAMjAxOTowNzoyMyAyMTozNDoxOQAGAACQBwAEAAAAMDIyMQGRBwAEAAAAAQIDAACgBwAEAAAAMDEwMAGgCQABAAAAAQAAAAKgCQABAAAACgAAAAOgCQABAAAACgAAAAAAAAAGAAMBAwABAAAABgAAABoBCQABAAAASAAAABsBCQABAAAASAAAACgBCQABAAAAAgAAAAECBAABAAAArAEAAAICBAABAAAA1AEAAAAAAAD/2P/gABBKRklGAAEBAAABAAEAAP/bAEMABQMEBAQDBQQEBAUFBQYHDAgHBwcHDwsLCQwRDxISEQ8RERMWHBcTFBoVEREYIRgaHR0fHx8TFyIkIh4kHB4fHv/bAEMBBQUFBwYHDggIDh4UERQeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHv/AABEIAAoACgMBIgACEQEDEQH/xAAXAAADAQAAAAAAAAAAAAAAAAAABwgJ/8QAIBAAAgICAgMBAQAAAAAAAAAAAgMBBAUGBxEACCETEv/EABQBAQAAAAAAAAAAAAAAAAAAAAD/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwBi60fFuU1XB5beI0a3tWVxGMv2RzeECzk77nALXymDn9LIsE4UkEjIrMJWMH/H4hOfJXsNzHq3Iuy6xhd0cnF4jL2qFJbqVawwEpcS1wTWrJjCgRjszIiKfszMzM+Htlu+6at7CbVhdY2/YMHi1NQ5dLHZJ1ZAG2sprThYFAwRsMzKeuyIyKe5mZ80MoVKtCjXo0ayatSsoUoQlcAtQDHQgIx8EYiIiIj5ER4H/9l4nPv//z8DAwMjIyMDKvgPFgeSQCkIGxnABTGl4ArgsnDFyAwi9eKRRXMYhItmL5p34Gpw+QhTI7J2AOTAPvUA'
bytes_base64 = text_base64.encode()
data = base64.b64decode(bytes_base64)
open('image.tiff', 'wb').write(data)
tiff
至base64
和html
:
import base64
data = open('image.tiff', 'rb').read()
bytes_base64 = base64.b64encode(data)
text_base64 = bytes_base64.decode()
print(text_base64)
html = '<img src="data:image/tiff;base64,' + text_base64 + '">'
open('output-tiff.html', 'w').write(html)
可能大多数浏览器无法显示tiff,因此文件.html可能显示损坏的图像。您必须将其从页面保存到文件,然后才能看到tiff。
这是示例中使用的图像,但此处为png
格式:
编辑:具有相同图像但格式为png
的示例。该html将在每个浏览器中显示图像。
# base64 to png
import base64
text_base64 = 'iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAAAAACoWZBhAAAAMklEQVR4nEXNwQkAMBACwfFI/y2bxwXia8FFUwF1pCCNJTIIwaD04ctsKRyaldI/9i8u1iwOE6FA880AAAAASUVORK5CYII='
bytes_base64 = text_base64.encode()
data = base64.b64decode(bytes_base64)
open('image.png', 'wb').write(data)
# png to base64 and html
import base64
data = open('image.png', 'rb').read()
bytes_base64 = base64.b64encode(data)
text_base64 = bytes_base64.decode()
print(text_base64)
html = '<img src="data:image/png;base64,' + text_base64 + '">'
open('output-png.html', 'w').write(html)
答案 1 :(得分:0)
这是基于Imagemagick的Python Wand中PNG的实现方法。
#!/bin/python3.7
from wand.image import Image
with Image(filename='inline:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAFVBMVEUAAAAAAAD13rO+vr7///8ekP/////XKQFBAAAAAXRSTlMAQObYZgAAAAFiS0dEBmFmuH0AAAAHdElNRQfjBxgCMxZ4smTjAAAAhElEQVQoz6WR0Q2AIAxEZQNaXaDdgLgCDuD+y1gQ4hViYmL/eLn2jnZZvhVRdO+gKg6wqsYRyADEjTGw7ikhMAGRA/k4ZQRgZDO2zGBUXTBLyaGYhaWhLuEu6SDo0DMB/gHiGwh3cvgMUSGCOxP1pzDgLzOD0gFLNRubyQSC6vq0USsDFzgRGqPIUzNPAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDE5LTA3LTI0VDAyOjQ4OjI3KzAwOjAw2vH1ygAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxOS0wNy0yNFQwMjo0ODoyNyswMDowMKusTXYAAAAASUVORK5CYII=') as img:
img.save(filename='noseguy_b64.png')