Форум сайта python.su
0
Доброго времени суток.
Мне нужно преобразовать цветное изображение в чёрно-белое. Использую convert из PIL:
import Image imageFile = "snim.png" im1 = Image.open(imageFile) im1.convert('1').save('end.JPG','JPEG')
Офлайн
173
When converting to a bilevel image (mode “1”), the source image is first converted to black and white. Resulting values larger than 127 are then set to white, and the image is dithered. To use other thresholds, use the point method.
from PIL import Image img = Image.open("image1.png") def bw_filter(threshold=127): def table_gen(x): return 0 if x < threshold else 255 return table_gen new = img.convert("L").point(bw_filter(200)) new.show()
new = img.convert("L").point(bw_filter(200)).convert("1")
Отредактировано reclosedev (Фев. 9, 2013 16:02:27)
Офлайн
0
Спасибо. Только надо
return 0 if x < threshold else 255
Офлайн
173
nikkТочно, поправил.
return 0 if x < threshold else 255
иначе получается белый текст на чёрном фоне.
Офлайн