如何使图像亮度均匀(使用Python/PIL)

发布于 2024-07-15 04:22:16 字数 354 浏览 5 评论 0原文

我想拍摄一张已拍照的文档的图像,并使其看起来像是经过扫描的。 由于扫描仪会在整个文档上放置恒定的光源,因此我想在文档的照片上实现这种效果。 所需的效果是消除任何阴影或低光区域(或至少使它们不那么明显)并使整张照片相当明亮。

我的第一个想法是找到目标图像中最亮的部分,然后它们使整个图像变得明亮。 假设这是正确的算法,我将如何在 PIL 中做到这一点? 有获取亮度的方法吗? ETC?

(这是这个早期问题的后续问题。 )

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弱骨蛰伏 2024-07-22 04:22:16

作为第一次尝试,尝试对图像进行阈值处理。 深色区域变为黑色,浅色区域变为白色。 我没有使用过 PIL,但我想有任何简单的方法可以做到这一点。

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.

荒路情人 2024-07-22 04:22:16

尝试使用 ImageChops.screen(image1, image2) 和 2 个图像副本。 如果这不满意,请尝试 ImageChops 模块中的一些其他功能。

另外,您可能想先将其转换为灰度:ImageOps.grayscale(image)。

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

愛上了 2024-07-22 04:22:16

首先在图像编辑程序(如 GIMP)中手动尝试。 我认为您正在寻找的是调整亮度和对比度。

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

天荒地未老 2024-07-22 04:22:16

什么类型的图像? 如果它应该是理想的纯黑白,就像文本页面一样,那么您的原始数据可能类似于灰度渐变,具有不同级别的非完全黑色字母。 针对常数进行阈值可能会产生良好的结果,但如果照明太不均匀或镜头眩光干扰,则不会产生良好的结果。 根据图像本身的平滑版本设置阈值。 使用 PIL_usm.gblur(image, radius) 对其进行平滑处理,其中半径(以像素为单位)类似于十、二十或与字母元素的宽度相当的某个值。 来自旧笔记的快速黑客代码只是为了说明:

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,如果这些文档有照片或其他非双层图形,您需要更加聪明。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文