简体   繁体   English

将每个 python 执行的图像保存在以序列号作为文件名的文件夹中

[英]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.

相关问题 在 python 的目标文件夹中保存名称包含 / 的文件 - Saving file with a name containing / at the destination folder in python 在Python中保存json文件时如何定义文件夹名称? - How to define folder name when saving json file in Python? 如果在文本文件中找到特定的字符串,则从文件夹内读取文本文件并保存文件夹的名称-Python - Reading a text file from within a folder and saving the name of the folder if a particular string is found in the text file - Python Python Tkinter保存图像名称 - Python tkinter saving image name 在循环python的每次运行中将数字保存到变量中 - saving the number into the variable in every run of cycle python Python 3.4 - 为文件夹中的所有文件添加文件名+行号 - Python 3.4 - Add file name + line number to all files in a folder 如何在调整图像大小并将其保存到另一个文件夹时保留图像的文件名? - How do I keep the file name of an image when resizing it and saving it to another folder? 使用python和.bat文件获取计算机序列号 - Getting computer serial number with python and .bat file Python:在文本文件中添加序列号和 header - Python : to add serial number in a text file and header 将文件夹中的所有文件数量合并为一个文件 - Combine every number of files in a folder into one file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM