简体   繁体   English

如何正确地将图像文件保存到目录?

[英]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.我需要帮助我今天早上开始的 Python 程序,我正在努力解决两个错误,一个是在启动程序时但没有任何作用,程序仍然继续运行,另一个是在完成保存过程时。

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:将第 54 行中的代码更改为:

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

You have an error at line 54你在第 54 行有一个错误
Use an f-string for variable to be set into your path使用 f 字符串将变量设置到您的路径中
img.save(f'codes/{qr_name}.jpg')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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