获取图像大小而不将图像加载到内存中 [英] Get Image size WITHOUT loading image into memory

查看:713
本文介绍了获取图像大小而不将图像加载到内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以使用PIL以下列方式获取图像大小

I understand that you can get the image size using PIL in the following fashion

from PIL import Image
im = Image.open(image_filename)
width, height = im.size

但是,我想得到图像的宽度和高度,而不得不将图像加载到内存中。那可能吗?我只对图像大小进行统计,不关心图像内容。我只是想让我的处理更快。

However, I would like to get the image width and height without having to load the image in memory. Is that possible? I am only doing statistics on image sizes and dont care for the image contents. I just want to make my processing faster.

推荐答案

正如评论所暗示的那样,PIL在调用时不会将图像加载到内存中code>。开。查看 PIL 1.1.7 的文档, .open 的文档字符串说:

As the comments allude, PIL does not load the image into memory when calling .open. Looking at the docs of PIL 1.1.7, the docstring for .open says:

def open(fp, mode="r"):
    "Open an image file, without loading the raster data"

源中有一些文件操作如下:

There are a few file operations in the source like:

 ...
 prefix = fp.read(16)
 ...
 fp.seek(0)
 ...

但这些几乎不构成读取整个文件。实际上 .open 只是在成功时返回一个文件对象和文件名。此外,文档说:

but these hardly constitute reading the whole file. In fact .open simply returns a file object and the filename on success. In addition the docs say:


open(file,mode =r)

open(file, mode="r")

打开并识别给定的图像文件。

Opens and identifies the given image file.

这是一个懒惰的操作;此函数标识文件,但在您尝试处理数据(或调用加载方法)之前,不会从文件中读取实际图像数据。

This is a lazy operation; this function identifies the file, but the actual image data is not read from the file until you try to process the data (or call the load method).

深入挖掘,我们看到 .open 调用 _open 这是图像格式特定的重载。可以在新文件中找到 _open 的每个实现,例如。 .jpeg文件位于 JpegImagePlugin.py 中。让我们深入研究一下。

Digging deeper, we see that .open calls _open which is a image-format specific overload. Each of the implementations to _open can be found in a new file, eg. .jpeg files are in JpegImagePlugin.py. Let's look at that one in depth.

这里的事情似乎有点棘手,其中有一个无限循环,当发现jpeg标记时会被打破:

Here things seem to get a bit tricky, in it there is an infinite loop that gets broken out of when the jpeg marker is found:

    while True:

        s = s + self.fp.read(1)
        i = i16(s)

        if i in MARKER:
            name, description, handler = MARKER[i]
            # print hex(i), name, description
            if handler is not None:
                handler(self, i)
            if i == 0xFFDA: # start of scan
                rawmode = self.mode
                if self.mode == "CMYK":
                    rawmode = "CMYK;I" # assume adobe conventions
                self.tile = [("jpeg", (0,0) + self.size, 0, (rawmode, ""))]
                # self.__offset = self.fp.tell()
                break
            s = self.fp.read(1)
        elif i == 0 or i == 65535:
            # padded marker or junk; move on
            s = "\xff"
        else:
            raise SyntaxError("no marker found")

如果格式错误,可以读取整个文件。如果它读取信息标记OK,它应该提前爆发。函数 handler 最终设置 self.size 这是图像的尺寸。

Which looks like it could read the whole file if it was malformed. If it reads the info marker OK however, it should break out early. The function handler ultimately sets self.size which are the dimensions of the image.

这篇关于获取图像大小而不将图像加载到内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆