相关文章推荐
腼腆的柠檬  ·  python ...·  3 周前    · 
有情有义的大白菜  ·  python ...·  3 周前    · 
完美的馒头  ·  python QTreeWidget ...·  2 周前    · 
失眠的烤红薯  ·  python qt textBrowser ...·  2 周前    · 
憨厚的芒果  ·  mybatis ...·  2 月前    · 
咆哮的生菜  ·  SpringBoot配置Logback ...·  3 月前    · 
光明磊落的杯子  ·  Java 实现Rtsp ...·  1 年前    · 
玩篮球的创口贴  ·  Data Aggregation in ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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:

"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."

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

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

"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."

pajton's answer is the one you want. You use 190, but I would use p > 191 (I know, the difference is minor :). tzot Jun 26, 2011 at 18:56 Thanks for the answers, but a funny thing happened on the way to implementation. My images actually start as TIFF,CCITT group4. I thought it would be easy to convert to 8-bit grey. However, PIL reports "decoder group4 not available". Is there another imaging library that supports group4? tahoar Jun 27, 2011 at 5:40

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)  
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.