2
\$\begingroup\$

I have a simple job class in Laravel, that will do the following:

  1. Get all the images in a folder /original
  2. Perform some image manipulation on each image
  3. Save each image in a new folder /preprocessed

All of these steps are added to a queue.

However, I have taken an alternative approach and it is using python to do the actual image manipulation.

This is my code:

$images = Storage::allFiles($this->document->path('original'));

foreach ($images as $key => $image) {
    $file = storage_path() . '/app/' . $image; //token/unique_id/original/1.jpg
    $savePath = storage_path() . '/app/' . $this->document->path('preprocessed');

    $filename = $key . '.jpg';

    //Scale and save the image in "/preprocessed"
    $process = new Process("python3 /Python/ScaleImage.py {$file} {$savePath} {$filename}");
    $process->run();

    // executes after the command finishes
    if (!$process->isSuccessful()) {
        throw new ProcessFailedException($process);
        return false;
    }
}

In my python file ScaleImage.py, it simply performs some image manipulation and saves the image to the /preprocessed folder.

    def set_image_dpi(file)
       
        //Image manipulation - removed from example
    
        //save image in /preprocessed
        im.save(SAVE_PATH + FILE_NAME, dpi=(300, 300))
        
        return SAVE_PATH + FILE_NAME
    
    print(set_image_dpi(FILE_PATH))

The above code works.

In the future, I might need to do even more image manipulation such as noise removal, skew correction, etc.

My final goal is to use Tesseract OCR on the preprocessed image to grab the text content of the image.

Now I am sure I could achieve similar results, for example using ImageMagick in PHP.

However, I've read that for image processing and OCR, Python's performance is a lot better.

Is the above approach a good idea? Is there anything that can be improved?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Anything that can be improved?

This line uses interpolation with complex (curly) syntax:

$process = new Process("python3 /Python/ScaleImage.py {$file} {$savePath} {$filename}");

The simple syntax can be used - i.e. curly braces can be removed

$process = new Process("python3 /Python/ScaleImage.py $file $savePath $filename");

Though there is an argument to maintain the habit of including curly braces for the times when it is required and also for clarity.

Now I am sure I could achieve similar stuff with for example ImageMagick in PHP.

Yes the PHP Image Processing and GD library has functions like imageresolution() to set the resolution of an image. It can be used with functions like imagecreatefromjpeg() to create the image object and imagejpeq() to output the image to a file. And yes there is an ImageMagick function Imagick::setImageResolution() which could be used instead.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.