[英]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.