简体   繁体   中英

How to correctly save an image file to a directory?

I need help for a Python Program I started this morning, I'm struggling with two errors, one when launching the program but that doesn't do anything, the program still continues to run, and another one when finishing the saving process.

Here is the entire code, just in case:

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()

First error (at launch)

Second error (at saving process)

Change your code in line 54 to this:

img.save('codes/{}.jpg'.format(qr_name))

You have an error at line 54
Use an f-string for variable to be set into your path
img.save(f'codes/{qr_name}.jpg')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM