简体   繁体   English

如何使图像亮度均匀(使用 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?假设这甚至是正确的算法,我将如何在 PIL 中做到这一点? Is there a get brightness method?有没有获取亮度的方法? etc? ETC?

(This is a follow-up to this earlier question .) (这是这个早先问题的后续。)

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.我没有使用过 PIL,但我想有任何简单的方法可以做到这一点。

Try ImageChops.screen(image1, image2) with 2 copies of the image.使用 2 个图像副本尝试 ImageChops.screen(image1, image2)。 If that's not satisfactory, try some of the other functions in the ImageChops module.如果不满意,请尝试 ImageChops 模块中的其他一些功能。

Also, you may want to convert it to grayscale first: ImageOps.grayscale(image).此外,您可能需要先将其转换为灰度:ImageOps.grayscale(image)。

First try it manually in a image editing program, like GIMP.首先在图像编辑程序中手动尝试,如 GIMP。 I think what you're looking for is adjusting brightness and contrast.我认为您正在寻找的是调整亮度和对比度。

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.使用 PIL_usm.gblur(image, radius) 对其进行平滑处理,其中半径(以像素为单位)类似于十、二十或与字母元素宽度相当的某个值。 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. OTOH,如果这些文件有照片或其他非双层图形,你需要更聪明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python Image Library(PIL)标准化一组图像的直方图(亮度和对比度) - Normalize histogram (brightness and contrast) of a set of images using Python Image Library (PIL) 如何使用 python 中的 openCV 使图像中的照明均匀,或如何增强非均匀照明图像中的照明 - How to make uniform illumination in a image using openCV in python or how to enhance illumination in non-uniformly illuminated image Python:如何使用PIL模块调整图像大小 - Python: How to resize an image using PIL module Python-如何使用PIL分割图像 - Python - How to split image using PIL 如何使用Python PIL或CV裁剪图像? - How to crop the Image using Python PIL or CV? 如何使用PIL分析python中的位图图像? - How to analyse bitmap image in python, using PIL? 如何使用 python / PIL 将图像存储到 redis - how to store an image into redis using python / PIL 通过使用python pil,我想使图像居中,并包含来自url的文本并下载。 怎么做? - By Using python pil, I want to make image centered with text from url and download. How to do it? 如何在不改变图像分辨率的情况下使用 OpenCV 或 Python 中的 PIL 使形状变大或变小 - How to make a shape larger or smaller without changing the resolution of the image using OpenCV or PIL in Python 使用PIL在Python中调整图像大小 - Resizing an image in Python using PIL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM