简体   繁体   English

如何仅使用标准库以Python读取任意图像文件格式(PNG,JPEG,TIFF,BMP)?

[英]How do I read an arbitrary image file format (PNG, JPEG, TIFF, BMP) in Python using only the standard library?

I'm an experienced Python programmer with plenty of image manipulation and computer vision experience. 我是一位经验丰富的Python程序员,具有丰富的图像处理和计算机视觉经验。 I'm very familiar with all of the standard tools like PIL , Pillow , opencv , numpy , and scikit-image . 我对PILPillowopencvnumpyscikit-image等所有标准工具都非常熟悉。

How would I go about reading an image into a Python data format like a nested list, bytearray, or similar, if I only had the standard library to work with? 如果我只有标准库可以使用,如何将图像读取为Python数据格式(如嵌套列表,字节数组或类似格式)?

I realize that different image formats have different specifications. 我意识到不同的图像格式具有不同的规格。 My question is how I would even begin to build a function that reads any given format. 我的问题是,我什至会开始构建一个读取任何给定格式的函数。

NOTE Python 2.6 had a jpeg module in the standard library that has since been deprecated. 注意 Python 2.6在标准库中有一个jpeg模块,此模块已被弃用。 Let's not discuss that since it is unsupported. 我们不讨论这一点,因为它不受支持。

If you're asking how to implement these formats "from scratch" (since the standard libraries don't do this), then a good starting point would be the format specification. 如果您要问如何“从头开始”实现这些格式(由于标准库不这样做),那么格式规范就是一个很好的起点。

For PNG, this is https://www.w3.org/TR/2003/REC-PNG-20031110/ . 对于PNG,这是https://www.w3.org/TR/2003/REC-PNG-20031110/ It defines the makeup of a PNG stream, consisting of the signature (eight bytes, 8950 4e47 0d0a 1a0a , which identifies the file as a PNG image) and a number of data chunks that contain meta data, palette information and the image itself. 它定义了一个PNG流的组成,包括签名(八个字节, 8950 4e47 0d0a 1a0a ,将文件标识为PNG图像)以及许多包含元数据,调色板信息和图像本身的数据块。 (It's certainly a substantial project to take on, if you really don't want to use the existing libraries, but not overly so.) (如果您确实不想使用现有的库,但绝对不要过度使用,那肯定是一个需要承担的重大项目。)

For BMP, it's a bit easier since the file already contains the uncompressed pixel data and you only need to know how to find the size and offset; 对于BMP,这有点容易,因为文件已经包含未压缩的像素数据,并且您只需要知道如何找到大小和偏移量即可; some of the format definition is on Wikipedia ( https://en.wikipedia.org/wiki/BMP_file_format ) and here: http://www.digicamsoft.com/bmp/bmp.html 一些格式定义在Wikipedia( https://en.wikipedia.org/wiki/BMP_file_format )上,在这里: http : //www.digicamsoft.com/bmp/bmp.html

JPG is much trickier. JPG比较棘手。 The file doesn't store pixels, but rather "wavelets" which are transformed into the pixel map you see on the screen. 该文件不存储像素,而是存储“小波”,这些小波将转换为您在屏幕上看到的像素图。 To read this format, you'll need to implement this transformation function. 要读取此格式,您需要实现此转换功能。

暂无
暂无

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

相关问题 tensorflow.python.framework.errors_impl.InvalidArgumentError:未知的图像文件格式。 需要 JPEG、PNG、GIF、BMP 之一 - tensorflow.python.framework.errors_impl.InvalidArgumentError: Unknown image file format. One of JPEG, PNG, GIF, BMP required 未知的图像文件格式。 需要 JPEG、PNG、GIF、BMP 之一 - Unknown image file format. One of JPEG, PNG, GIF, BMP required Tensorflow Keras 错误:未知的图像文件格式。 需要 JPEG、PNG、GIF、BMP 之一 - Tensorflow Keras error: Unknown image file format. One of JPEG, PNG, GIF, BMP required Python:如何读取这个多页 TIFF 文件中的数据以生成所需的图像? - Python: How do I read the data in this multipage TIFF file to produce the desired image? 如何从 tiff 文件中获取 jpeg 图像? - How to get jpeg image from a tiff file? Tecplot,Python或Vim帮助:如何使用当前工作目录作为名称写出图像(PNG,JPEG)文件? - Tecplot, Python or Vim assistance: How can I write out an image (PNG, JPEG) file using the current working directory as the name? 如何在 python 中将 jpeg 转换为 tiff 文件 - how to convert jpeg to tiff file in python "如何使用 python 将 3 个 TIFF 图像组合成 1 个 PNG 图像?" - How to combine 3 TIFF images into 1 PNG image with python? “OSError: 2” 当使用 Python 图像库将 TIFF 图像转换为 PNG 时 - "OSError: 2" When converting TIFF image to PNG with Python Image Library 如何在wxpython中获取png或jpeg或bmp图片 - How to get png or jpeg or bmp pictures in wxpython
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM