1

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

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

petezurich
  • 9,280
  • 9
  • 43
  • 57
LuigiZard
  • 13
  • 3
  • 1
    @Ruzihm I am not Python-er, so I am not able to understand this typo. Some typos are worth an answer, are you sure this one doesn't need further explanation? – Dharman Mar 20 '20 at 20:04
  • hi, so, i tried doing it but it didn't work, i still get the same error – LuigiZard Mar 20 '20 at 20:06
  • _but it seems I got a little stuck and I would really apreciate getting some help_ Help with what? – AMC Mar 20 '20 at 22:31

1 Answers1

1

When using Text.get to get more than one character, you need to give it an end position. You can use the special string "end" for this, but ignoring the last newline character that tkinter adds with "end-1c":

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

See this answer for more information

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • `"end"` will get one more character out than was put in, because tkinter automatically adds a trailing newline. If you want to get out exactlly what was put in you should use `"end-1c"`. – Bryan Oakley Mar 21 '20 at 15:24