[英]How to correctly save an image file to a directory?
我需要幫助我今天早上開始的 Python 程序,我正在努力解決兩個錯誤,一個是在啟動程序時但沒有任何作用,程序仍然繼續運行,另一個是在完成保存過程時。
這是整個代碼,以防萬一:
1 # not important (comments)
2 # not important (comments)
3 # not important (comments)
4
5 import qrcode
6 from os import listdir, remove, mkdir
7 from os.path import isdir, isfile
8 from pystyle import Anime, Colors, Colorate, System, Center, Write
9
10 banner = r"""
11 █▀▀▀▀▀█ ███ ▀▄▀▀█ █▀▀▀▀▀█
12 █ ███ █ █▄▀█ ▄▀ ▄ █ ███ █
13 █ ▀▀▀ █ ▄ ▄▄ ▀▀█ █ ▀▀▀ █
14 ▀▀▀▀▀▀▀ ▀▄▀ █ █ █ ▀▀▀▀▀▀▀
15 ▀█ ██▀ ▄▄▀██▄▄▄ ▀▄█▀█▀
16 ▄███ ▀██ ▄█ ▀▄▀▀▀▀█▀▀█▄
17 █▀█▄▄▄▀▄▀▄▀█ █▄█ █▀▀▄▀▀█▀
18 ▄▄▀▀▀▀▄ █▀█▀▄▀█▄ ██▀█▄
19 ▀▀▀▀ ▀▀▀█▄ █▀ ▄ █▀▀▀█▀▀
20 █▀▀▀▀▀█ ▄ █▀▄▀█ █ ▀ █▄▄ ▄
21 █ ███ █ ▀ █ ▀ ▄▄▀███▀▀█▄▄
22 █ ▀▀▀ █ ▄▀▄█▄ ▀ ▀██▄▄█▄█
23 ▀▀▀▀▀▀▀ ▀ ▀ ▀ ▀ ▀▀▀"""
24
25 ascii_art = r"""
26
27 ___________ _____ _
28 | _ | ___ \ | __ \ | |
29 | | | | |_/ / | | \/ ___ _ __ ___ _ __ __ _| |_ ___ _ __
30 | | | | / | | __ / _ \ '_ \ / _ \ '__/ _` | __/ _ \| '__|
31 \ \/' / |\ \ | |_\ \ __/ | | | __/ | | (_| | || (_) | |
32 \_/\_\_| \_| \____/\___|_| |_|\___|_| \__,_|\__\___/|_|"""[1:]
33
34 def init():
35 if not isdir('codes'):
36 mkdir('codes')
37 System.Clear()
38 System.Size(160, 50)
39 System.Title("QR Generator")
40 Anime.Fade(text=Center.Center(banner), color=Colors.white_to_green, mode=Colorate.Vertical, enter=True)
41
42 def main():
43 global qr_url
44 global qr_name
45 System.Clear()
46 print('\n'*2)
47 print(Colorate.Diagonal(color=Colors.white_to_green, text=Center.XCenter(ascii_art)))
48 print('\n'*3)
49 qr_url = Write.Input(text="Enter the link that will be opened when the QR Code will be scanned > ", color=Colors.white_to_green, interval=0.005)
50 print()
51 qr_name = Write.Input(text="Enter the name so the program can save the code with a custom name > ", color=Colors.white_to_green, interval=0.005)
52 print()
53 img = qrcode.make(qr_url)
54 img.save('codes/'(qr_name).jpg)
55
56 if __name__ == '__main__':
57 init()
58 while True:
59 main()
將第 54 行中的代碼更改為:
img.save('codes/{}.jpg'.format(qr_name))
你在第 54 行有一個錯誤
使用 f 字符串將變量設置到您的路徑中img.save(f'codes/{qr_name}.jpg')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.