如何使用PIL Image.point(table)方法将阈值应用于256灰度图像? [英] How to I use PIL Image.point(table) method to apply a threshold to a 256 gray image?

查看:801
本文介绍了如何使用PIL Image.point(table)方法将阈值应用于256灰度图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有8位灰度TIFF图像,我想使用75%的白色(十进制190)阈值转换为单色.在Image.convert(mode)方法部分中,PIL手册说:

I have 8-bit greyscale TIFF images that I want to convert to Monochrome using a 75% white (decimal 190) threshold. In the Image.convert(mode) method section, the PIL manual says:

将灰度图像转换为位图像(模式"1")时,所有非零值均设置为255(白色).要使用其他阈值,请使用点方法."

"When translating a greyscale image into a bitlevel image (mode "1"), all non-zero values are set to 255 (white). To use other thresholds, use the point method."

Image.point(table)方法表示它会映射给定表中的每个像素.

The Image.point(table) method says that it maps each pixel through the given table.

im.point(表格,模式)=>图片
im.point(功能,模式)=>图片

im.point(table, mode) => image
im.point(function, mode) => image

通过表格映射图像,并进行即时转换.在PIL的当前版本中,这只能用于一步将'L'和'P'图像转换为'1',例如将阈值限制为图片."

"Map the image through table, and convert it on fly. In the current version of PIL , this can only be used to convert 'L' and 'P' images to '1' in one step, e.g. to threshold an image."

如何创建与我所需的75%阈值相对应的表(或函数)?

How do I create the table (or function) that corresponds to the 75% threshold I need?

推荐答案

我在此答案中找到了完整的解决方案"

I found the complete solution in this answer "Write TIFF file in python from String". The function must include "and 255"

threshold = 191  
im = im.point(lambda p: p > threshold and 255)  

这篇关于如何使用PIL Image.point(table)方法将阈值应用于256灰度图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆