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?

Sᴀᴍ Onᴇᴌᴀ
29.5k16 gold badges45 silver badges201 bronze badges
asked May 2, 2019 at 13:05
\$\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.

answered May 17, 2022 at 21:20
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.