我无法使用base64模块获得正确的路径,因为预期的输出应该像
C:\ Users \ User_Name \ Documents \ photos \ photo.png
我在以前的方法变量image_open
中打印并得到了斜杠,但是当我对其进行解码和编码并尝试在对其进行编码后打印变量image_file
时,最终在两者之间没有任何斜杠定义的路径...
import os
import base64
from tkinter import *
from PIL import ImageTK,Image
def pick_photo():
global image
global image_file_name
#label_path_of_photo = tk.StringVar()
ask_path_of_photo = filedialog.askopenfilename(initialdir = "C:/Users/User _Name/Documents/photos/",
title = "Open File",
filetypes = (("PNG","*.png"),("JPG","*jpg"),("All file","*.*")))
image_file_name =os.path.abspath(ask_path_of_photo)
image_open = Image.open(general_path_to_photo + image_file_name)
image = ImageTk.PhotoImage(image_open)
image_show = tk.Label(root, image = image, textvariable = label_path_of_photo)
image_show.grid(row = 0, column = 3, rowspan = 3, columnspan = 6, padx = 15, pady = 25)
path_decode(image_file_name)
def path_decode(image_file_name):
decoded_path_of_image = base64.b64decode(image_file_name)
print(decoded_path_of_image)
encodig_path = base64.b64encode(decoded_path_of_image)
print(encodig_path)
我希望输出按定义的斜线进行打印(在 io.path.abspath(ask_path_of_photo)中),我可以很好地打印输出,但是在打印时没有得到斜线编码后的字符串 如果遇到其他错误,请帮我找出来
答案 0 :(得分:0)
我认为您以错误的方式使用base64
。
您应该首先创建filename.encode('utf-8')
,然后再使用b64encode
,而不是b64decode
。然后在base64中获得字符串。
现在使用b64decode
和.decode('utf-8)
,您可以找回路径
import base64
def path_decode(filename):
bytes_name = filename.encode('utf-8')
encodig_path = base64.b64encode(bytes_name)
print(encodig_path)
bytes_name = base64.b64decode(encodig_path)
filename = bytes_name.decode('utf-8')
print(filename)
image_file_name = r'C:\Users\User_Name\Documents\photos\photo.png'
path_decode(image_file_name)
结果
b'QzpcVXNlcnNcVXNlcl9OYW1lXERvY3VtZW50c1xwaG90b3NccGhvdG8ucG5n'
C:\Users\User_Name\Documents\photos\photo.png