I'm trying to read the text from this image:
I want to read the price, e.g. "EUR42721.92"
I tried these libraries:
How to Create a PHP Captcha Decoder with PHP OCR Class: Recognize text & objects in graphical images - PHP Classes
phpOCR: Optical Character Recognizer written in PHP
But they don't work. How can I read the text?
Try this (it worked with me):
$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->scaleImage($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);
You need ImageMagick extension and GOCR installed to run it.
If you can't or don't want to install the ImageMagick extension, I'll send you a GD version with a function to calculate colors distances (it's just an extended Pythagorean Theorem).
Don't forget to set the $filePath value.
The image shows that it looks for a gray pixel to change the $foundGray flag.
After that, it looks for the first and last pixels from the left and from the top.
It crops the image with some padding, the resulting image is resized and it's saved to a temporary file. After that, it's easy to use gocr (or any other OCR command or library). The temporary file can be removed after that.
Improve the quality of the image of the numbers before you start the OCR. Use a drawing program to improve the quality (bigger size, straight lines).
You can either modify the PHP scripts and adapt the pattern recognition to your needs.
https://github.com/ogres/PHP-OCR/blob/master/Image2String.php
Or try out other OCR tools:
https://github.com/thiagoalessio/tesseract-ocr-for-php
Related
I just discovered this article http://jeffkreeftmeijer.com/2011/comparing-images-and-creating-image-diffs/
It is talking about using regular Ruby and ChunkyPNG to do image diffs.
In particular, the first code of doing a loop through all the pixels.
require 'chunky_png'
images = [
ChunkyPNG::Image.from_file('1.png'),
ChunkyPNG::Image.from_file('2.png')
]
diff = []
images.first.height.times do |y|
images.first.row(y).each_with_index do |pixel, x|
diff << [x,y] unless pixel == images.last[x,y]
end
end
puts "pixels (total): #{images.first.pixels.length}"
puts "pixels changed: #{diff.length}"
puts "pixels changed (%): #{(diff.length.to_f / images.first.pixels.length) * 100}%"
x, y = diff.map{ |xy| xy[0] }, diff.map{ |xy| xy[1] }
images.last.rect(x.min, y.min, x.max, y.max, ChunkyPNG::Color.rgb(0,255,0))
images.last.save('diff.png')
I wonder
a) what would be an ideal PHP equivalent of ChunkyPNG?
b) what would be the implementation of the same code in PHP?
You can do with Imagick extension. Imagick is a native php extension to create and modify images using the ImageMagick API.
Example Using Imagick::compareImages():
Compare images and display the reconstructed image.
<?php
$image1 = new imagick("image1.png");
$image2 = new imagick("image2.png");
$result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);
$result[0]->setImageFormat("png");
header("Content-Type: image/png");
echo $result[0];
?>
OR
A PHP shell script to tell you if two images are visually different by comparing them pixel by pixel. If there's a difference, the script creates a third image - black background with the different pixels in green. Here two GD images are created from the input files, also a third GD image with black background to store the diff. Loop through all the pixels and compare them one by one. If one is different, write a green pixel at the same location of the diff image. [Source : http://www.phpied.com/image-diff/ ]
diff.php
<?php
/**
* Shell script to tell if two images are identical.
* If not, a third image is written - black background with the different pixels painted green
* Code partially inspired by and borrowed from http://pear.php.net/Image_Text test cases
*/
// check if there's enough input
if (empty($argv[1]) || empty($argv[2])) {
echo 'gimme at least two image filenames, please.', "\n";
echo 'e.g. "php idiff.php img1.png img2.png"';
echo "\n", 'third filename is the image diff, optional, default is "diffy.png"';
exit(1);
}
// create images
$i1 = #imagecreatefromstring(file_get_contents($argv[1]));
$i2 = #imagecreatefromstring(file_get_contents($argv[2]));
// check if we were given garbage
if (!$i1) {
echo $argv[1] . ' is not a valid image';
exit(1);
}
if (!$i2) {
echo $argv[2] . ' is not a valid image';
exit(1);
}
// dimensions of the first image
$sx1 = imagesx($i1);
$sy1 = imagesy($i1);
// compare dimensions
if ($sx1 !== imagesx($i2) || $sy1 !== imagesy($i2)) {
echo "The images are not even the same size";
exit(1);
}
// create a diff image
$diffi = imagecreatetruecolor($sx1, $sy1);
$green = imagecolorallocate($diffi, 0, 255, 0);
imagefill($diffi, 0, 0, imagecolorallocate($diffi, 0, 0, 0));
// increment this counter when encountering a pixel diff
$different_pixels = 0;
// loop x and y
for ($x = 0; $x < $sx1; $x++) {
for ($y = 0; $y < $sy1; $y++) {
$rgb1 = imagecolorat($i1, $x, $y);
$pix1 = imagecolorsforindex($i1, $rgb1);
$rgb2 = imagecolorat($i2, $x, $y);
$pix2 = imagecolorsforindex($i2, $rgb2);
if ($pix1 !== $pix2) { // different pixel
// increment and paint in the diff image
$different_pixels++;
imagesetpixel($diffi, $x, $y, $green);
}
}
}
if (!$different_pixels) {
echo "Image is the same";
exit(0);
} else {
if (empty($argv[3])) {
$argv[3] = 'diffy.png'; // default result filename
}
imagepng($diffi, $argv[3]);
$total = $sx1 * $sy1;
echo "$different_pixels/$total different pixels, or ", number_format(100 * $different_pixels / $total, 2), '%';
exit(1);
}
?>
Usage (Command line):
php diff.php img1.png img2.png result.png
Ok, so am working on a simple generator using jquery. A user enters a text of his/her choice, selects a font, font-color and font-size. All this properties are displayed on a separate div in real-time (live preview).
I now need to save the generated preview as a picture. For that, I use php GD library. All works fine but with some fonts, everything is just messed up.
In the first image, everything looks alright but the second image, the line height is just messed up.
This is my php script that am using to processes the properties
<?php
//Width and height of desired image
$width = 320;
$height= 320;
//Create an image with specified height and width
$main_img = ImageCreate($width, $height);
$mx = imagesx($main_img);
$my = imagesy($main_img);
//Capture values from form
$main_text = $_POST['rtext'];
$main_text_size = $_POST['rfsize'];
$color = $_POST['rcolor'];
$mt_f = $_POST['rfont'];
$main_text_x = ($mx/2);
// more code here
//wrap text if text too long
$words = explode(' ', $main_text);
$lines = array($words[0]);
$currentLine = 0;
for($i = 1; $i < count($words); $i++)
{
$lineSize = imagettfbbox($main_text_size, 0, $mt_f, $lines[$currentLine] . ' ' . $words[$i]);
if($lineSize[2] - $lineSize[0] < $mx)
{
$lines[$currentLine] .= ' ' . $words[$i];
}
else
{
$currentLine++;
$lines[$currentLine] = $words[$i];
}
}
$line_count = 1;
// Loop through the lines and place them on the image
foreach ($lines as $line)
{
$line_box = imagettfbbox($main_text_size, 0, $mt_f, "$line");
$line_width = $line_box[0]+$line_box[2];
$line_height = $line_box[1]-$line_box[7];
$line_margin = ($mx-$line_width)/2;
$line_y = (($line_height+12) * $line_count);
imagettftext($main_img, $main_text_size, 0, $line_margin, $line_y, $main_text_color, $mt_f, $line);
// Increment Y so the next line is below the previous line
$line_count ++;
}
header("Content-type: image/png");
//code to download the image
?>
Is there a way I can modify the part of the code where I wrap the text to accomodate all fonts? Like automatically calculate the line height based on the font?
Thanks, any help will be appreciated
I found a very useful class at phpclasses imagefittext.class.php http://www.phpclasses.org/browse/file/41869.html. I also found an example script that is implemented using the class http://www.phpclasses.org/browse/file/41870.html. This is exactly what I wanted.
Worked Perfectly!!!1
I have a 96x96 image and i need to divide it in 36 pieces of 16x16 and i have a script given below works fine on my localhost but not working on my webhost.
function makeTiles($name, $imageFileName, $crop_width, $crop_height)
{
$dir = "/pic";
$slicesDir = "/pic/16X16";
// might be good to check if $slicesDir exists etc if not create it.
$ImgExt = split(".",$imageFileName);
$inputFile = $dir . $imageFileName;
$img = new Imagick($inputFile);
$imgHeight = $img->getImageHeight();
$imgWidth = $img->getImageWidth();
$cropWidthTimes = ceil($imgWidth/$crop_width);
$cropHeightTimes = ceil($imgHeight/$crop_height);
for($i = 0; $i < $cropWidthTimes; $i++)
{
for($j = 0; $j < $cropHeightTimes; $j++)
{
$img = new Imagick($inputFile);
$x = ($i * $crop_width);
$y = ($j * $crop_height);
$img->cropImage($crop_width, $crop_height, $x, $y);
$data = $img->getImageBlob();
$newFileName = $slicesDir . $name . "_" . $x . "_" . $y . ".".$ImgExt[1];
$result = file_put_contents ($newFileName, $data);
}
}
}
Getting fatal error
Fatal error: Class 'Imagick' not found in myfile.php line number
My host saying:
Imagick and image magic both are same unfortunately, we do not have
them as a PHP module we have binaries like ImageMagick binary and
Image magic path is /usr/local/bin. Include this function in your
script and check the website functionality from your end.
I dont know how to fix this error.
If I understand it correctly you will have to invoke the imagick binary from your script using exec():
exec('/usr/local/bin/convert '.$inputFile.' -crop 96x96+ox+oy '.$newFilename);
The ox and oy should be replaced with the correct offset values.
Here is are a couple of links that might help:
How do I use Imagick binaries from php
Imagick - Convert Command-line tool
I'm trying to use the IMagick PHP wrapper to assist in chopping a specified image into a set of tiles (the number of which is variable).
In the ImageMagick documentation there is reference to the -crop operator accepting an optional flag of # that will instruct it to cut an image into "roughly equally-sized divisions" (see here), solving the problem of what to do when the image size is not an exact multiple of the desired tile size.
Does anyone know if there is a way to leverage this functionality in the IMagick PHP wrapper? Is there anything I can use besides cropImage()?
I had to do the same thing (if I'm reading your question correctly). Although it does use cropImage...
function slice_image($name, $imageFileName, $crop_width, $crop_height)
{
$dir = "dir where original image is stored";
$slicesDir = "dir where you want to store the sliced images;
mkdir($slicesDir); //you might want to check to see if it exists first....
$fileName = $dir . $imageFileName;
$img = new Imagick($fileName);
$imgHeight = $img->getImageHeight();
$imgWidth = $img->getImageWidth();
$crop_width_num_times = ceil($imgWidth/$crop_width);
$crop_height_num_times = ceil($imgHeight/$crop_height);
for($i = 0; $i < $crop_width_num_times; $i++)
{
for($j = 0; $j < $crop_height_num_times; $j++)
{
$img = new Imagick($fileName);
$x = ($i * $crop_width);
$y = ($j * $crop_height);
$img->cropImage($crop_width, $crop_height, $x, $y);
$data = $img->getImageBlob();
$newFileName = $slicesDir . $name . "_" . $x . "_" . $y . ".jpg";
$result = file_put_contents ($newFileName, $data);
}
}
}
This one might be a little confusing. I'm using AMCharts with rails. Amcharts comes with a PHP script to export images called "export.php"
I'm trying to figure out how to take the code in export.php and put it into a controller.
Here is the code:
<?php
// amcharts.com export to image utility
// set image type (gif/png/jpeg)
$imgtype = 'jpeg';
// set image quality (from 0 to 100, not applicable to gif)
$imgquality = 100;
// get data from $_POST or $_GET ?
$data = &$_POST;
// get image dimensions
$width = (int) $data['width'];
$height = (int) $data['height'];
// create image object
$img = imagecreatetruecolor($width, $height);
// populate image with pixels
for ($y = 0; $y < $height; $y++) {
// innitialize
$x = 0;
// get row data
$row = explode(',', $data['r'.$y]);
// place row pixels
$cnt = sizeof($row);
for ($r = 0; $r < $cnt; $r++) {
// get pixel(s) data
$pixel = explode(':', $row[$r]);
// get color
$pixel[0] = str_pad($pixel[0], 6, '0', STR_PAD_LEFT);
$cr = hexdec(substr($pixel[0], 0, 2));
$cg = hexdec(substr($pixel[0], 2, 2));
$cb = hexdec(substr($pixel[0], 4, 2));
// allocate color
$color = imagecolorallocate($img, $cr, $cg, $cb);
// place repeating pixels
$repeat = isset($pixel[1]) ? (int) $pixel[1] : 1;
for ($c = 0; $c < $repeat; $c++) {
// place pixel
imagesetpixel($img, $x, $y, $color);
// iterate column
$x++;
}
}
}
// set proper content type
header('Content-type: image/'.$imgtype);
header('Content-Disposition: attachment; filename="chart.'.$imgtype.'"');
// stream image
$function = 'image'.$imgtype;
if ($imgtype == 'gif') {
$function($img);
}
else {
$function($img, null, $imgquality);
}
// destroy
imagedestroy($img);
?>
There are some versions in existence in a thread I found here: http://www.amcharts.com/forum/viewtopic.php?id=341
But I have a feeling the PHP code above has changed since then - because neither implementation worked for me.
What this code more or less dose is grabs the informations, that were sent to the script (POST).
The informations include the height and width of the picture and the RGB values of every pixel. The script draws every pixel and sends the images at the end to the client.
You can use Rmagick's method to draw a pixel. This will give you the same result.
The incomming post data looks like this:
height = number -> cast to int
width = number -> cast to int
// first row with a repeating part of R:G:B,R:G:B,... (n = width)
r0 = 255:0:0,150:120:0,77:88:99,...
r1 = ...
.
.
r100 = ... -> the row count is the height - 1
Actually, I found a discussion about speeding up pixel by pixel drawing.
So apparently I was running into other errors which made me think the already existing code didnt work. However, the code on the thread I linked to in the original question does in fact work!