首页 \ 问答 \ 使用Python图像库(PIL)规格化一组图像的直方图(亮度和对比度)(Normalize histogram (brightness and contrast) of a set of images using Python Image Library (PIL))

使用Python图像库(PIL)规格化一组图像的直方图(亮度和对比度)(Normalize histogram (brightness and contrast) of a set of images using Python Image Library (PIL))

我有一个脚本,它使用Google Maps API下载一系列相同大小的方形卫星图像并生成PDF。 图像需要事先旋转,我已经使用PIL来完成。

我注意到,由于不同的光照和地形条件,一些图像太亮,另一些太暗,并且由此产生的pdf最终变得有点丑陋,“在现场”的阅读条件不甚理想(这是穷国山地自行车,在那里我想打印特定十字路口的缩略图)。

(编辑)然后,目标是让所有图像以相似的亮度和对比度结束。 所以,太亮的图像必须变暗,而黑暗的图像必须变亮。 (顺便说一下,我曾经使用过imagemagick auto-gamma autocontrast ,或auto-gammaequalize ,或auto-gamma或类似的东西,在医学图像中有趣的结果,但不知道如何在PIL中做这些)。

转换成灰度图后(我刚才使用了灰度打印机),我已经使用了一些图像校正功能,但结果也不好。 这是我的灰度代码:

#!/usr/bin/python

def myEqualize(im)
    im=im.convert('L')
    contr = ImageEnhance.Contrast(im)
    im = contr.enhance(0.3)
    bright = ImageEnhance.Brightness(im)
    im = bright.enhance(2)
    #im.show()
    return im

此代码对每个图像都独立工作。 我想知道是不是先分析所有图像然后“归一化”它们的视觉特性(对比度,亮度,伽玛等)会更好。

此外,我认为有必要在图像中进行一些分析(直方图?),以便根据每幅图像应用自定义校正,而不是对所有图像进行相同的校正(尽管任何“增强”功能隐含地考虑初始接头)。

有没有人有这样的问题和/或知道一个很好的替代方案与彩色图像做到这一点(无灰度)?

任何帮助将不胜感激,谢谢阅读!


I have a script which uses Google Maps API to download a sequence of equal-sized square satellite images and generates a PDF. The images need to be rotated beforehand, and I already do so using PIL.

I noticed that, due to different light and terrain conditions, some images are too bright, others are too dark, and the resulting pdf ends up a bit ugly, with less-than-ideal reading conditions "in the field" (which is backcountry mountain biking, where I want to have a printed thumbnail of specific crossroads).

(EDIT) The goal then is to make all images end up with similar apparent brightness and contrast. So, the images that are too bright would have to be darkened, and the dark ones would have to be lightened. (by the way, I once used imagemagick autocontrast, or auto-gamma, or equalize, or autolevel, or something like that, with interesting results in medical images, but don't know how to do any of these in PIL).

I already used some image corrections after converting to grayscale (had a grayscale printer a time ago), but the results weren't good, either. Here is my grayscale code:

#!/usr/bin/python

def myEqualize(im)
    im=im.convert('L')
    contr = ImageEnhance.Contrast(im)
    im = contr.enhance(0.3)
    bright = ImageEnhance.Brightness(im)
    im = bright.enhance(2)
    #im.show()
    return im

This code works independently for each image. I wonder if it would be better to analyze all images first and then "normalize" their visual properties (contrast, brightness, gamma, etc).

Also, I think it would be necessary to perform some analysis in the image (histogram?), so as to apply a custom correction depending on each image, and not an equal correction for all of them (although any "enhance" function implicitly considers initial contitions).

Does anybody had such problem and/or know a good alternative to do this with the colored images (no grayscale)?

Any help will be appreciated, thanks for reading!


原文:https://stackoverflow.com/questions/7116113
更新时间:2022-05-04 09:05

最满意答案

a是一个数组引用,所以要将其推入b ,你需要复制它:

b.push(a.dup)

这类似于在C中使用strdup ,其中字符串是指针。


a is an array reference, so to push its value into b, you'll need to copy it:

b.push(a.dup)

This is similar to using strdup in C, where strings are pointers.

相关问答

更多
  • a是一个数组引用,所以要将其值推入b ,你需要复制它: b.push(a.dup) 这类似于在C中使用strdup ,其中字符串是指针。 a is an array reference, so to push its value into b, you'll need to copy it: b.push(a.dup) This is similar to using strdup in C, where strings are pointers.
  • 是的, ensure确保代码总是被评估。 这就是为什么它被称为ensure 。 所以,它finally等同于Java和C#。 begin / rescue / else / ensure / end的一般流程如下所示: begin # something which might raise an exception rescue SomeExceptionClass => some_variable # code that deals with some exception rescue SomeO ...
  • 确保块的返回值被丢弃 - 它只是在函数执行任何应该做的事情之后进行清理的方法(并返回适当的值)。 原因是因为它允许您在函数体中的不同位置放置几个return语句(或raise语句),而无需在函数的不同位置复制清除代码。 The ensure block's return value is discarded -- it's just a way to clean up after the function does whatever it's supposed to (and returns the app ...
  • 这不是范围问题,它是一个传递问题的参数 Ruby中的所有变量都是对象的引用。 将对象传递给方法时,将创建该对象的引用副本并将其传递给该对象。 这意味着您的方法中的变量array和顶级变量a引用完全相同的数组。 对array所做的任何更改也将作为对a更改显示,因为这两个变量都指向同一个对象。 你的方法通过调用Array#keep_if来修改数组。 keep_if方法keep_if修改数组。 修复 解决这个问题的最佳方法是让你的方法不会修改传入的数组。这可以使用Enumerable#any来完成。 和Enume ...
  • 你可以这样做: Thread.new do begin Process.wait Process.spawn 'find /oeinfsroif' raise unless $?.exitstatus == 0 rescue retry end end.join 要管理失败前的尝试次数: Thread.new do max_attempts = 10 attempts = 0 begin Process.wait Process.spawn 'fin ...
  • 仅使一个值唯一: a = ["dog", "cat", "cat", "hamster", "rabbit", "dog"] (a.count("cat") - 1).times { a.delete_at(a.index("cat"))} #=> ["dog", "cat", "hamster", "rabbit", "dog"] 这样可以保留数组中元素的顺序。 要删除连续的项目: 使用Enumerable#chunk : a = ["dog", "cat", "cat", "hamster", " ...
  • 使用Array#产品 : a = [1, 2] b = [:a] a.product(b) => [[1, :a], [2, :a]] Use the Array#product: a = [1, 2] b = [:a] a.product(b) => [[1, :a], [2, :a]]
  • 密切关注挑战中问题给出的提示,然后使用String的tr方法: "test string".tr('a-z', 'c-zab') # => "vguv uvtkpi" 解决问题的另一个提示是,您应该只处理字符。 标点符号和空格应该保持不变。 在Python Challenge中的字符串上使用上面的tr ,你会明白我的意思。 Pay close attention to the hint given by the question in the Challenge, then use String's tr ...
  • Ruby使用按值传递,或者更确切地说,是传递值的特殊情况,传递的值始终是指针。 这种特殊情况有时也被称为共享呼叫,按对象共享呼叫或按对象呼叫。 这与Java(对象),C#(默认用于引用类型),Smalltalk,Python,ECMAScript / JavaScript以及所创建的每种面向对象语言所使用的约定是相同的。 注意:在所有现有的Ruby实现中, Symbol s, Fixnum和Float实际上是直接通过值传递的,而不是通过中间指针传递的。 然而,由于这三者是不可变的,所以在这种情况下,在价值传 ...
  • 您的语法似乎不符合以下文档: 对于索引小于i的任何元素,该块返回false 对于索引大于或等于i的任何元素,该块返回true。 这个i永远不会存在于你提供它的块中。 例如,在元素11(x = 1)的循环中,对于x < i和x> i ,块将返回false,并且对于每个后续元素也是如此。 您需要将块修改为: array.bsearch { |x| x >= 11 } # 11 array.bsearch_index { |x| x >= 11 } # 1 在这种情况下,使用相同的示例,对于索引<1的所有元素, ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)