[英]Saving image of every python execution in a folder with a serial number as file name
I am a beginner in Python and would like to execute a code which saves an image into particular directory.我是 Python 的初学者,想执行一个将图像保存到特定目录的代码。 The thing is, I would like to save image with a serial number so that I can have many images(each execution gives one image) in the directory.问题是,我想用序列号保存图像,这样我就可以在目录中有很多图像(每次执行都会给出一个图像)。
plt.savefig('Images/Imageplot.png') ## Image saved to particular folder
About the serial number, you can use the uuid library to generate a random and unique id for an image.关于序列号,您可以使用 uuid 库为图像生成一个随机且唯一的 id。 See more here: https://www.geeksforgeeks.org/generating-random-ids-using-uuid-python/在此处查看更多信息: https ://www.geeksforgeeks.org/generating-random-ids-using-uuid-python/
To save images, it is a bit more complicated.要保存图像,它有点复杂。 It requires you to import the os library.它要求您导入 os 库。
Here is an example:这是一个例子:
import os
UPLOADED_FILE_DIR_PATH = os.path.join(os.path.dirname(__file__), "static", "uploaded-images")
my_file_path = os.path.join(UPLOADED_FILE_DIR_PATH, my_filename)
image_file.save(my_file_path)
This is a block of code I used previously for my website, so it may not apply for you, depending on your situation.这是我以前用于我的网站的代码块,因此它可能不适用于您,具体取决于您的情况。 I personnaly like that method, but if you are unsatisfied, take a look at this for more options: https://towardsdatascience.com/loading-and-saving-images-in-python-ba5a1f5058fb我个人喜欢这种方法,但如果您不满意,请查看此方法以获取更多选项: https ://towardsdatascience.com/loading-and-saving-images-in-python-ba5a1f5058fb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.