سلام من قطعه کدی رو نوشتم و میخوام عکس ها رو ازیک دایرکتوری بگیره و اونا رو سیاه سفید کنه و در دایرکتوری دیگه ذخیره کنه.
کد من بدون حلقه for کار می کنه ولی با حلقه نه

from PIL import Image
import numpy as np

col = Image.open("cat-tied-icon.png")
gray = col.convert('L')

# Let numpy do the heavy lifting for converting pixels to pure black or white
bw = np.asarray(gray).copy()

# Pixel range is 0...255, 256/2 = 128
bw[bw < 128] = 0    # Black
bw[bw >= 128] = 255 # White

# Now we put it back in Pillow/PIL land
imfile = Image.fromarray(bw)
imfile.save("result_bw.png")

اما با حلقه for جواب نمیده کسی میتونه کمک کنه ( کد زیر برای for)

import glob
import numpy as np
from PIL import Image

path = r'input/*.png'
output = 'output'

filenames = glob.glob(path + "/*.png") 

for x in filenames:
    myarray = np.asarray(gray).copy()
    bw[bw < 128] = 0   # Black
    bw[bw >= 128] = 255 # White
    im = Image.fromarray(myarray)
    im.save(output + '/*.png')
آخرین ویرایش: 25-07-2019 ???? 11:49، توسط mohammad-heidari