图片转换为base64编码后,实现无图片展示图片

第一部分,将图片转换为base64编码输出为txt文本

第二部分,将转换出来的base64编码赋值给b64_code,通过base64编码转换为图片,打开,然后删除

base64编码过长,同时输入过多编码后,编码不识别(暂时无法解决)

import os
import base64
import io
from PIL import Image
import cv2
import pyglet
'''
def image_to_base64(path):
    with open(path, 'rb') as img:
        # 使用base64进行编码
        b64encode = base64.b64encode(img.read())
        s = b64encode.decode()
        b64_encode = 'data:image/jpeg;base64,%s' % s
        # 返回base64编码字符串
        return b64_encode
#打开图片
def open_image(img_b64decode):
    image = io.BytesIO(img_b64decode)
    print(image)
    img = Image.open(image)
    img.show()

a = 1
while a <= 10:
    b = f'{a}.jpg'
    c = f'{a}.png'
    d = f'{a}.gif'
    e = a
    a = a + 1
    if os.path.exists(b):
        print(b+"图片存在")
        path = b
        data = open(f'{e}.txt', 'w+')
        base_str = image_to_base64(path)
        print(base_str, file=data)
        data.close()
    elif os.path.exists(c):
        print(c+"图片存在")
        path = c
        data = open(f'{e}.txt', 'w+')
        base_str = image_to_base64(path)
        print(base_str, file=data)
        data.close()
    elif os.path.exists(d):
        print(d+"图片存在")
        path = d
        data = open(f'{e}.txt', 'w+')
        base_str = image_to_base64(path)
        print(base_str, file=data)
        data.close()
    else:
        print("该图片不存在")
'''
b64_encode = ""
a = 0
#打开图片
def open_image(img_b64decode):
    image = io.BytesIO(img_b64decode)
    print(image)
    img = Image.open(image)
    img.show()

base64转换成图片


```ruby

```python
def base64_to_image(base64_encod_str):
    res=base64_encod_str.split(',')[1]
    img_b64decode = base64.b64decode(res)
    if a == 1:
        with open(f'{a}.jpg', 'wb') as png:
            png.write(img_b64decode)
    elif a > 1 & a < 7:
        with open(f'{a}.gif', 'wb') as png:
            png.write(img_b64decode)
    elif a == 7:
        with open(f'{a}.png', 'wb') as png:
            png.write(img_b64decode)
while a < 2:
    a = a + 1
    if a == 1:
        base64_to_image(b64_encode1)
        from PIL import Image
        img=Image.open(f'{a}.jpg')
        img.show()
    elif a > 1 & a < 7:
        if a == 2:
            base64_to_image(b64_encode2)
        # 在工作目录中选择一个gif动画文件
        ag_file = f'{a}.gif'
        animation = pyglet.resource.animation(ag_file)
        sprite = pyglet.sprite.Sprite(animation)
        # 创建一个窗口并将其设置为图像大小
        win = pyglet.window.Window(width=sprite.width, height=sprite.height)
        # 设置窗口背景颜色 = r, g, b, alpha
        # 每个值从 0.0 到 1.0
        green = 0, 1, 0, 1
        pyglet.gl.glClearColor(*green)
        @win.event
        def on_draw():
            win.clear()
            sprite.draw()
        pyglet.app.run()
    elif a == 7:
        base64_to_image(b64_encode7)
        img = cv2.imread(f'{a}.png', cv2.IMREAD_UNCHANGED)
        cv2.imshow('image',img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

files= os.listdir() #得到文件夹下的所有文件名称
print(files[0:10])#看一看文件都有啥
for pic in files: #遍历文件夹
    if pic.endswith(".png"):
        os.remove(pic)
    elif pic.endswith(".gif"):
        os.remove(pic)
    elif pic.endswith(".jpg"):
        os.remove(pic)

版权声明:本文为qq_46201532原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。