简体   繁体   English

base64编码图像; binascii.Error: 无效的 base64 编码

[英]base64 encoding image; binascii.Error: Invalid base64-encoded

as a project for a course, I'm developing an image coder/decoder using python, but it seems I got a little stuck and I would really apreciate getting some help作为一门课程的项目,我正在使用 python 开发一个图像编码器/解码器,但似乎我有点卡住了,我真的很想得到一些帮助

The error I'm getting is:我得到的错误是:

runfile('D:/Documentos/Python/Proyecto_Final/Main.py', wdir='D:/Documentos/Python/Proyecto_Final')
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\...\anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "D:\Documentos\Python\Proyecto_Final\Main.py", line 32, in decode64
    imagen.write(base64.decodebytes(baset.encode()))
  File "C:\...\anaconda3\lib\base64.py", line 546, in decodebytes
    return binascii.a2b_base64(s)
binascii.Error: Invalid base64-encoded string: number of data characters (1) cannot be 1 more than a multiple of 4

My code is:我的代码是:

from tkinter import *
import os
import base64

def browseBtn():
    filename = filedialog.askopenfilename()
    texto.insert(0, filename)

def convbase64():
    path = str(texto.get())
    imagen = open(path, 'rb')
    leeimg = imagen.read()
    codigo64 = base64.encodebytes(leeimg)
    texto2.insert("1.0", codigo64)

def decode64():
    myFormats = [('JPEG / JFIF','*.jpg'),\
                     ('Portable Network Graphics','*.png'),\
                     ('Windows Bitmap','*.bmp'),('CompuServer GIF','*.gif'),]
    baset = texto2.get(1.0)
    filepath = filedialog.asksaveasfilename(filetypes=myFormats)
    imagen = open(filepath, 'wb')
    imagen.write(base64.decodebytes(baset.encode()))
    imagen.close()

ventana = Tk()
ventana.title("Convertidor imagen a Base64")
ventana.geometry("800x480")
letrero = Label(ventana, text = "Imagen:")
texto = Entry(ventana, width = 100)
buscarImg = Button(ventana, text = "Elegir", command=browseBtn)
letrero2 = Label(ventana, text = "codigo base64:")
texto2 = Text(width = 75, height = 1)
btnconvertir = Button(ventana, text = "Codificar", command=convbase64)
btndecodificar = Button(ventana, text = "decodificar", command=decode64)
letrero.place(x = 32, y = 32)
letrero2.place(x = 16, y = 64)
texto.place(x = 114, y = 35)
texto2.place(x = 114, y = 69)
buscarImg.place(x = 724, y = 32)
btnconvertir.place(x = 724, y = 64)
btndecodificar.place (x = 724, y = 96)

ventana.mainloop()

I'm using anaconda's Spyder 3.7我正在使用 anaconda 的 Spyder 3.7

When using Text.get to get more than one character, you need to give it an end position.当使用Text.get获取多个字符时,需要给它一个结束位置。 You can use the special string "end" for this, but ignoring the last newline character that tkinter adds with "end-1c" :您可以"end"使用特殊字符串"end" ,但忽略 tkinter 添加的最后一个换行符"end-1c"

baset = texto2.get("1.0", "end-1c")

See this answer for more information有关更多信息,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM