38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
import os
|
|
import cv2
|
|
|
|
|
|
def convert_and_delete_png(input_folder):
|
|
# 確保資料夾存在
|
|
if not os.path.exists(input_folder):
|
|
print("指定的資料夾不存在!")
|
|
return
|
|
|
|
# 遍歷資料夾中的所有檔案
|
|
for filename in os.listdir(input_folder):
|
|
file_path = os.path.join(input_folder, filename)
|
|
|
|
# 只處理 PNG 檔案
|
|
if filename.lower().endswith('.png'):
|
|
# 讀取影像
|
|
image = cv2.imread(file_path)
|
|
if image is None:
|
|
print(f"無法讀取檔案: {filename}")
|
|
continue
|
|
|
|
# 轉換並存成 BMP
|
|
new_filename = os.path.splitext(filename)[0] + ".bmp"
|
|
new_file_path = os.path.join(input_folder, new_filename)
|
|
cv2.imwrite(new_file_path, image)
|
|
print(f"已轉換: {filename} -> {new_filename}")
|
|
|
|
# 刪除原 PNG 檔案
|
|
try:
|
|
os.remove(file_path)
|
|
print(f"已刪除: {filename}")
|
|
except Exception as e:
|
|
print(f"刪除 {filename} 失敗: {e}")
|
|
|
|
# 設定目標資料夾路徑 (請修改為你的資料夾)
|
|
input_folder = "E:/AP/YOLODataset/images/val" # 請修改為實際的資料夾路徑
|
|
convert_and_delete_png(input_folder) |