我現在只能 批量. tiff 轉換 .png
想問一下python 能不能整個folder 內的 .png圖片進行 threshold 轉換, 再把圖片按原檔的名字儲存在另一個指定文件夾。
如果可以請問能不能教一教。
import os
# Get list of all .png files in the folder
folder = '/path/to/folder'
filenames = [f for f in os.listdir(folder) if f.endswith('.png')]
from PIL import Image #pip install Pillow
# Loop through all the files
for filename in filenames:
# Load the image
img = Image.open(os.path.join(folder, filename))
# Convert the image to black and white using thresholding
img = img.convert('L')
img = img.point(lambda x: 0 if x < 128 else 255, '1')
# Save the image to a new folder
img.save(os.path.join('/path/to/new/folder', filename))