Showing ValueError: invalid literal for int() with base 10: '\n'

Showing ValueError: invalid literal for int() with base 10: '\n'

这里我正在制作一个加密图像的项目,当我 运行 代码显示 ValueError: invalid literal for int() with base 10: '\n'

我的代码:

from tkinter import Tk
from tkinter import *
from tkinter import filedialog
def encript():
    root = Tk()
    root.geometry("200x160")
    def encript_image():
        file1 = filedialog.askopenfile(mode='r', filetype=[('jpg file', '*.jpg')])
        if file1 is not None:
            # print(file1)
            file_name = file1.name
            # print(file_name)
            key = entry1.get(1.0, END)
            print(file_name, key)
            fi = open(file_name, 'rb')
            image = fi.read()
            fi.close()
            image = bytearray(image)
            for index, value in enumerate(image):
                image[index] = value^int(key)
            fi1 = open(file_name, 'wb')
            fi1.write(image)
            fi1.close()


    b1 = Button(root, text="encript", command=encript_image)
    b1.place(x=70, y=20)
    entry1 = Text(root, height=1, width=10)

    entry1.place(x=50, y=50)

    root.mainloop()
encript()

关于 运行 这段代码我得到了错误:

ValueError: invalid literal for int() with base 10: '\n'

我不知道我要做什么

嗯,错误指出“\n”不是有效的文字,因为“\n”不是整数。从提供的代码来看,我看到您正在尝试使用指定的密钥使用 XOR 加密(^ 符号)来加密图像。为此,您试图获取 ASCII 字符的整数版本,但是您使用的是 int(),它将字符串文字中的整数转换为可用整数。

x = int(“12”)
print(x) # Prints the integer 12

要获取 ASCII 字符的整数版本,请使用 ord()

x = ord(‘a’)
print(x) # Prints the integer 97