我制作了一個程式來從陣列創建影像。最后,我使用 PIL 的 .fromarray() 轉換它,然后使用 .save() 方法保存它。我沒有指定檔案路徑,因為我想將檔案保存在與程式所在的路徑相同的路徑中。而不是這樣做,我的影像被保存在一個完全不同的不相關路徑中。如何在不指定檔案路徑的情況下將影像保存在與程式相同的路徑中?
程式在路徑:“C:\Users\Bloo\Desktop\extra\skins\skin maker”
影像被保存到路徑:“C:\Users\Bloo\Desktop\Homework\Computer Science”
這是我的代碼的保存部分。
# convert the array to an image
image = Image.fromarray(colorData)
# ask for the file name
fileName = input("Please enter the file name you would like for the skin image. Format is not required\n")
fileName = ".png" # add the file format to end of file name
# save the image
image.save(fileName, "PNG")
uj5u.com熱心網友回復:
將此添加到檔案的開頭:
import os
program_directory_name = os.path.dirname(os.path.abspath(__file__))
os.chdir(program_directory_name)
那么您的作業目錄將與您運行的檔案目錄相同。
uj5u.com熱心網友回復:
如果您沒有指定路徑并將檔案保存在不同的目錄中,那么您的作業目錄可能與您的檔案路徑不同。
檢查您的作業目錄:
import os
print(os.getcwd())
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/400216.html
上一篇:縮短if陳述句Python
