Python:在 PIL 和/或 pygame 中操作 16 位 .tiff 图像:以某种方式转换为 8 位?
大家好,
我正在开发一个程序,该程序可以根据照片确定酵母的平均菌落大小,并且它与我测试过的 .bmp 图像配合良好。该程序使用pygame,稍后可能会使用PIL。
然而,我们在实验室中使用的相机/软件组合只能保存 16 位灰度 tiff,而 pygame 似乎无法识别 16 位 tiff,只能识别 8 位。在过去的几个小时里,我一直在阅读解决此问题的简单方法,但即使是 Python 成像库似乎也无法使用 16 位 .tiff,我已经尝试过,但得到了“IOError:无法识别图像”文件”。
import Image
img = Image.open("01 WT mm.tif")
我的最终目标是让这个程序用户友好且易于安装,因此我试图避免添加额外的模块或要求人们安装 ImageMagick 之类的东西。
有谁知道使用免费软件或纯Python解决这个问题的简单方法?我对图像了解不多:位深度操作超出了我的范围。但我相当确定我不需要所有 16 位,并且可能只有大约 8 位实际上具有真实数据。事实上,我曾经使用 ImageMagick 尝试转换它们,这导致了全白图像:我从那以后读到我应该使用命令“-auto-levels”,因为数据实际上并不包含 16-位范围。
我非常感谢您的帮助,并对我的知识缺乏表示歉意。
PS:有谁有关于如何使我的Python程序易于非程序员安装的提示吗?例如,有没有一种方法可以将其与 Python 和 pygame 捆绑在一起,这样只需安装一次? Windows 和 Mac 都可以这样做吗?谢谢。
编辑:我尝试在 GIMP 中打开它,并收到 3 个错误:
1)字段“DateTime”的计数不正确(27,期望 20);标签已修剪 2) 抱歉,无法处理 12 位样本的图像 3) 不支持的布局,没有 RGBA 加载器
这是什么意思以及如何安装它?
Hello all,
I am working on a program which determines the average colony size of yeast from a photograph, and it is working fine with the .bmp images I tested it on. The program uses pygame, and might use PIL later.
However, the camera/software combo we use in my lab will only save 16-bit grayscale tiff's, and pygame does not seem to be able to recognize 16-bit tiff's, only 8-bit. I have been reading up for the last few hours on easy ways around this, but even the Python Imaging Library does not seem to be able to work with 16-bit .tiff's, I've tried and I get "IOError: cannot identify image file".
import Image
img = Image.open("01 WT mm.tif")
My ultimate goal is to have this program be user-friendly and easy to install, so I'm trying to avoid adding additional modules or requiring people to install ImageMagick or something.
Does anyone know a simple workaround to this problem using freeware or pure python? I don't know too much about images: bit-depth manipulation is out of my scope. But I am fairly sure that I don't need all 16 bits, and that probably only around 8 actually have real data anyway. In fact, I once used ImageMagick to try to convert them, and this resulted in an all-white image: I've since read that I should use the command "-auto-levels" because the data does not actually encompass the 16-bit range.
I greatly appreciate your help, and apologize for my lack of knowledge.
P.S.: Does anyone have any tips on how to make my Python program easy for non-programmers to install? Is there a way, for example, to somehow bundle it with Python and pygame so it's only one install? Can this be done for both Windows and Mac? Thank you.
EDIT: I tried to open it in GIMP, and got 3 errors:
1) Incorrect count for field "DateTime" (27, expecting 20); tag trimmed
2) Sorry, can not handle images with 12-bit samples
3) Unsupported layout, no RGBA loader
What does this mean and how do I fit it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用的是 Windows 系统,py2exe 是打包应用程序的方法。
关于16位tiff问题:
此示例 http://ubuntuforums.org/showthread.php?t=1483265 展示了如何使用 PIL 进行转换以供显示。
现在,对于未提出的部分问题:进行图像分析时,您希望在图像处理中尽可能长时间地保持尽可能高的动态范围 - 这样您会丢失更少的信息。您可能知道也可能不知道,PIL 为您提供了许多过滤器/变换,可以让您增强图像的对比度、均匀光照水平或执行边缘检测。您可能要考虑的未来方向是在经过边缘检测处理的缩放图像旁边显示原始图像(当然缩放到 8 位)。
请查看 http://code.google.com/p/pyimp/wiki/screenshots 有关更多示例和示例代码。
py2exe is the way to go for packaging up your application if you are on a windows system.
Regarding the 16bit tiff issue:
This example http://ubuntuforums.org/showthread.php?t=1483265 shows how to convert for display using PIL.
Now for the unasked portion question: When doing image analysis, you want to maintain the highest dynamic range possible for as long as possible in your image manipulations - you lose less information that way. As you may or may not be aware, PIL provides you with many filters/transforms that would allow you enhance the contrast of an image, even out light levels, or perform edge detection. A future direction you might want to consider is displaying the original image (scaled to 8 bit of course) along side a scaled image that has been processed for edge detection.
Check out http://code.google.com/p/pyimp/wiki/screenshots for some more examples and sample code.
我会看看 pylibtiff,它有一个纯 python tiff 阅读器。
对于捆绑,最好的选择可能是 py2exe 和 py2app。
I would look at pylibtiff, which has a pure python tiff reader.
For bundling, your best bet is probably py2exe and py2app.
这实际上是一个由两部分组成的问题:
1)Python 的 16 位图像数据整理 - 我通常使用 GDAL + Numpy。这可能有点超出您的要求,您可以使用 PIL + Numpy 代替。
2) 发布工程 Python 应用程序可能会变得混乱。根据您的应用程序的复杂程度,您可以使用 py2deb、py2app 和 py2exe。学习 distutils 也会有帮助。
This is actually a 2 part question:
1) 16 bit image data mangling for Python - I usually use GDAL + Numpy. This might be a bit too much for your requirements, you can use PIL + Numpy instead.
2) Release engineering Python apps can get messy. Depending on how complex your app is you can get away with py2deb, py2app and py2exe. Learning distutils will help too.