首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python检测和转换渐进式jpeg

如何使用python检测和转换渐进式jpeg
EN

Stack Overflow用户
提问于 2010-10-23 08:02:15
回答 1查看 1.7K关注 0票数 2

我希望能够使用python检测渐进式jpeg,并将其转换为非渐进式。

(我正在为android编写一个管理图像的工具,渐进式的jpegs似乎打破了它。)

EN

回答 1

Stack Overflow用户

发布于 2013-03-16 08:47:13

我提前道歉,因为我提供了一个基于php的答案,而问题是关于python的。尽管如此,我认为它增加了价值,可以是有用的。在尝试将渐进式图像转换为非渐进式图像之前,最好有一种用于渐进式Jpeg的检测方法。

下面是php函数,可以很容易地用其他语言重写(python可能是一个候选),因为它读取二进制数据和Jpeg标记(因此不依赖于特定于语言的库)。

代码语言:javascript
复制
    public function checkProgressiveJPEG($filepath) {
    $result = false;
    // $this->log = 'started analysis...';

    // http://en.wikipedia.org/wiki/Jpeg 
    // for more details on JPEG structure

    // SOI  [0xFF, 0xD8] = Start Of Image
    // SOF0 [0xFF, 0xC0] = Start Of Frame (Baseline DCT)
    // SOF2 [0xFF, 0xC2] = Start Of Frame (Progressive DCT)
    // SOS  [0xFF, 0xDA] = Start Of Scan

    if(file_exists($filepath)) {
        $fs = @fopen($filepath, "rb");

        $bytecount = 0;
        $byte_last = 0;
        $buffer = 0;
        $buffer_length = 4*1024;
        $begins_with_SOI = false;

        while($buffer = fread($fs, $buffer_length)) {

            // always carry over previous ending byte
            // just in case the buffer is read after a 0xFF marker
            if($byte_last) {
                $buffer = $byte_last.$buffer;
            }
            $byte_last = 0;
            preg_match("/\.$/", $buffer, $matches); 
            if(count($matches)) { 
                $byte_last = $matches[0];
            }

            // check if it begins with SOI marker
            if(!$begins_with_SOI) {
                preg_match("/^\\xff\\xd8/", $buffer, $matches); 
                if(count($matches)) { 
                    $begins_with_SOI = true;
                } else {
                    // $this->log = 'does not start with SOI marker';
                    $result = false;
                    break;
                }
            }

            // check if SOS or SOF2 is reached
            preg_match("/\\xff(\\xda|\\xc2)/", $buffer, $matches); 
            if(count($matches)) {
                if(bin2hex($matches[0]) == 'ffda') {
                    // $this->log = 'SOS is reached and SOF2 has not been detected, so image is not progressive.';
                    $result = false;
                    break;
                } else if(bin2hex($matches[0]) == 'ffc2') {
                    // $this->log = 'SOF2 is reached, so image is progressive.';
                    $result = true;
                    break;
                }

            } 
        } // end while

        fclose($fs);
    } // end if
    return $result;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4001757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档