I have a simple job class in Laravel, that:

  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 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}");

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

In my python file ScaleImage.py, I simply perform 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


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 grap the text content of the image.

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

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

Is above approach a good idea? Anything that can be improved?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.