[英]how to get decoded string with "\" using base64 module
我无法使用 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)我打印它返回正常,但编码后我没有在打印的字符串中得到斜杠如果有任何其他错误否则这也请帮我弄清楚
我认为您以错误的方式使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.