4

给定一个带有新闻文章的 HTML 页面,我试图从文章中检测相关图像。为此,我正在查看图像的大小(如果它们太小,可能是导航元素),但我不想下载每张图像。

有没有办法在不下载完整图像的情况下获取图像的宽度和高度?

4

2 回答 2

2

不知道它是否会帮助您加快申请速度,但可以做到。查看这两篇文章:

http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/用于 JPEG

PNG http://www.herrodius.com/blog/265

它们都适用于 ActionScript,但该原则当然也适用于其他语言。

我使用 C# 制作了一个示例。它不是最漂亮的代码,它只适用于 JPEG,但也可以轻松扩展到 PNG:

var request = (HttpWebRequest) WebRequest.Create("http://unawe.org/joomla/images/materials/posters/galaxy/galaxy_poster2_very_large.jpg");
using (WebResponse response = request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
{
    int r;
    bool found = false;
    while (!found && (r = responseStream.ReadByte()) != -1)
    {
        if (r != 255) continue;

        int marker = responseStream.ReadByte();

        // App specific
        if (marker >= 224 && marker <= 239)
        {
            int payloadLengthHi = responseStream.ReadByte();
            int payloadLengthLo = responseStream.ReadByte();
            int payloadLength = (payloadLengthHi << 8) + payloadLengthLo;
            for (int i = 0; i < payloadLength - 2; i++)
                responseStream.ReadByte();
        }
        // SOF0
        else if (marker == 192)
        {
            // Length of payload - don't care
            responseStream.ReadByte();
            responseStream.ReadByte();

            // Bit depth - don't care
            responseStream.ReadByte();

            int widthHi = responseStream.ReadByte();
            int widthLo = responseStream.ReadByte();
            int width = (widthHi << 8) + widthLo;

            int heightHi = responseStream.ReadByte();
            int heightLo = responseStream.ReadByte();
            int height = (heightHi << 8) + heightLo;

            Console.WriteLine(width + "x" + height);
            found = true;
        }
    }
}

编辑:我不是 Python 专家,但这篇文章似乎描述了一个 Python 库这样做(最后一个示例):http ://effbot.org/zone/pil-image-size.htm

于 2011-02-13T12:39:14.700 回答
1

不,这是不可能的。但是您可以从img标签中获取信息,但不能从背景中获取信息。

于 2011-02-13T11:14:43.307 回答