如何使圖像亮度均勻(使用 Python/PIL) (How to Make an Image Uniform Brightness (using Python/PIL))


問題描述

如何使圖像亮度均勻(使用 Python/PIL) (How to Make an Image Uniform Brightness (using Python/PIL))

I want to take an image of a document that was photographed and make it look like it was scanned.  Since a scanner will put a constant light source over the whole document, I want to achieve that effect on a photo of a document.  The desired effect would be to remove any shadows or areas of low light (or at least make them less noticeable) and have the whole photo be fairly bright.

My first thought would be to locate the brightest part of the target image, and them make the whole image that brightness.  Assuming that's even the right algorithm, how would I do it in PIL?  Is there a get brightness method? etc?

(This is a follow‑up to this earlier question.)

‑‑‑‑‑

參考解法

方法 1:

As a first attempt, try thresholding the image.  Dark areas become black, light areas become white.  I haven't used PIL, but I imagine there's any easy way to do it.

方法 2:

Try ImageChops.screen(image1, image2) with 2 copies of the image. If that's not satisfactory, try some of the other functions in the ImageChops module.

Also, you may want to convert it to grayscale first: ImageOps.grayscale(image). 

方法 3:

First try it manually in a image editing program, like GIMP. I think what you're looking for is adjusting brightness and contrast.

方法 4:

What type of image?  If it's supposed to be ideally pure black and white, as with text pages, then your raw data probably is something like a grayscale gradient with varying levels of not‑quite‑black letters. Thresholding against a constant may give good results, but not if the illumination is too uneven or lens glare interferes. Threshold the image against a smoothed version of itself.  Smooth it using PIL_usm.gblur(image, radius) where radius (in pixels) is something like ten, twenty or some value comparable to the width of elements of the letters.  Quick hackcode from old notes just for illustration:

import Image
import PIL_usm
# see http://www.cazabon.com/pyCMS/PIL_usm.html for PIL_usm

img = Image.open(...)
sm = PIL_usm(img, 10)
thr = Image.ImageChops.subtract(img,sm, .001, 128)  
# or whatever works 4u...

OTOH, if these documents have photographs or other non‑bilevel graphics, you'll need to be more clever.  

(by GregMr FoozcdonnerJack HaDarenW)

參考文件

  1. How to Make an Image Uniform Brightness (using Python/PIL) (CC BY‑SA 3.0/4.0)

#image-processing #Python #python-imaging-library #image #brightness






相關問題

在 matlab 中用 imread 讀取圖像文件會給出什麼樣的表示? (reading a image file with imread in matlab gives what kind of representation?)

使用 CRF 的圖像標記性能 (Image labeling performance using CRF)

Opencv:獲取圖像中的段大小並刪除小段 (Opencv: Get segments sizes in image and remove small segments)

將 PHP 頁面作為圖像返回 (Return a PHP page as an image)

我在哪裡可以找到有關雙三次插值和 Lanczos 重採樣的好讀物? (Where can I find a good read about bicubic interpolation and Lanczos resampling?)

從圖像中刪除白色背景 (Remove white backgrounds from images)

如何填充投影圖像的空白部分? (How to fill empty parts of a projected image?)

如何使圖像亮度均勻(使用 Python/PIL) (How to Make an Image Uniform Brightness (using Python/PIL))

圖像處理公式可生成類似通過 Mac 相機拍攝的照片的效果 (Image manipulation formula to generate effects like pictures taken via Mac's camera)

從照片生成漂亮的直方圖? (generating nice looking histogram from photo?)

使用 DjVu 工具進行背景/前景分離? (Using the DjVu tools to for background / foreground seperation?)

如何操縱跟踪器區域使其變成方形? (How can I manipulate the tracker area to make it into a square shape?)







留言討論