Home  >  Article  >  Backend Development  >  How to convert python byte stream into image

How to convert python byte stream into image

(*-*)浩
(*-*)浩Original
2019-07-05 09:36:046124browse

Because it is inconvenient during the transmission process of images, base64 is used to convert them for transmission.

How to convert python byte stream into image

First convert the picture into a byte stream for transmission. After receiving the data, convert the bytes into a picture for generation (Recommended learning: Python video tutorial)

Picture to byte stream

import base64

# 图片转字节
def tu_zi_jie():
    with open('jiu.png','rb') as fp:
        tu = base64.b64encode(fp.read())
    # 生成很长得一串字节流
    print(tu)

if __name__ == '__main__':
    tu_zi_jie()

Byte conversion to picture

import base64

# 字节转图片
def zi_tu():
	# 这个就是把上面打印的字节流那过来进行转换图片
	# 因为转换后字符串很长所以我截取了一部分
	b_tu = b'iVBORw0KGgoAAAANS....UhEU'
	tu_b = base64.b64decode(b_tu)
    with open('tu.png', 'wb') as fp:
        fp.write(tu_b)
if __name__ == '__main__':
    tu_zi_jie()

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to convert python byte stream into image. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn