I have a simple job class in Laravel, that will do the following:
- Get all the images in a folder
/original
- Perform some image manipulation on each image
- 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?