PHP   发布时间:2019-10-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了用PHP读取图像中的文本大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试从此图像中读取文字:

我想读价格,例如“EUR42721.92”

我试过这些图书馆:

> phpclasses.org/package/2874-PHP-Recognize-text-objects-in-graphical-images.html
> phpocr.sourceforge.net

但他们不工作我如何阅读文本?

解决方法

尝试这个(它与我一起工作):
$imagick = new Imagick($filePath);

$size = $imagick->getImageGeometry();
$width     = $size['width'];
$height    = $size['height'];
unset($sizE);

$textBottomPosition = $height-1;
$textrightPosition = $width;

$black = new ImagickPixel('#000000');
$gray  = new ImagickPixel('#C0C0C0');

$textright  = 0;
$textLeft   = 0;
$textBottom = 0;
$textTop    = $height;

$foundGray = false;

for($x= 0; $x < $width; ++$X) {
    for($y = 0; $y < $height; ++$y) {
        $pixel = $imagick->getImagePixelColor($x,$y);
        $color = $pixel->getColor();
        // remove alpha component
        $pixel->setColor('rgb(' . $color['r'] . ','
                         . $color['g'] . ','
                         . $color['b'] . ')');

        // find the first gray pixel and ignore pixels below the gray
        if( $pixel->isSimilar($gray,.25) ) {
            $foundGray = true;
            break;
        }

        // find the text boundaries 
        if( $foundGray && $pixel->isSimilar($black,.25) ) {
            if( $textLeft === 0 ) {
                $textLeft = $x;
            } else {
                $textright = $x;
            }

            if( $y < $textTop ) {
                $textTop = $y;
            }

            if( $y > $textBottom ) {
                $textBottom = $y;
            }
        }
    }
}

$textWidth = $textright - $textLeft;
$textHeight = $textBottom - $textTop;
$imagick->cropImage($textWidth+10,$textHeight+10,$textLeft-5,$textTop-5);
$imagick->ScaleIR_913_11845@age($textWidth*10,$textHeight*10,truE);

$textFilePath = tempnam('/temp','text-ocr-') . '.png';
$imagick->writeImage($textFilePath);

$text = str_replace(' ','',sHell_exec('gocr ' . escapesHellarg($textFilePath)));
unlink($textFilePath);
var_dump($text);

需要安装ImageMagick扩展和GOCR来运行它.
如果您不能或不想安装ImageMagick扩展名,我将向您发送一个具有函数来计算颜色距离的GD版本(它只是一个扩展的毕达哥拉斯定理).

不要忘记设置$filePath值.

该图像显示它查找一个灰色像素来更改$foundGray标志.之后,它会从左侧和从顶部查找第一个和最后一个像素.它用一些填充来裁剪图像,生成的图像被调整大小,并将其保存到临时文件中.之后,很容易使用gocr(或任何其他OCR命令或库).之后可以删除临时文件.

大佬总结

以上是大佬教程为你收集整理的用PHP读取图像中的文本全部内容,希望文章能够帮你解决用PHP读取图像中的文本所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。