Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the ability to process image only from disk to from both disk and memory #260

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

jingchaoluan
Copy link

Goal: Extend the ability to process image only from disk to from both disk and memory.

Updates: changed methods 'read_image_gray()' and 'read_image_binary' in module 'ocrlib/common.py'.

Description: The method can only read an image from disk i.e. from a filename (string). The new method extends the ability to read an image from both disk and memory. Read image from memory means that it can also accept file object or PIL.Image.Image object as the parameter of this method.

My application context: Now I developed OCRopus related services and used the library 'ocrlib', but it can only read an image from disk (the parameter type must be 'string'). While for my service, I used the Django framework, which receives the image uploaded by users as an 'InMemoryUploadFile' type which is a file type object. Thus, the 'ocrlib' can not take it as a parameter of the read_image_ methods and thus cannot process. So I changed the methods and now it can process image from disk (string type) and memory (file object or PIL.Image.Image)

… method can only read image from disk i.e. from a filename (string). The new method extend the ability to read image from both disk and memory. Read image from memory means that it can also accept file object or PIL.Image.Image object as the parameter of this method.
@jingchaoluan jingchaoluan changed the title Changed method 'read_image_gray()' and 'read_image_binary()'. The old… Extend the ability to process image only from disk to from both disk and memory Nov 16, 2017
@kba
Copy link
Collaborator

kba commented Dec 8, 2017

I think this is a useful extension, thanks.

Can you create a short python script that tests this behavior, i.e. passing a PIL.Image and a file name to the functions and having the same result? This can be really short and should use the assets in the test directory. Then we can this as a unit test.

…ss instances for methods 'read_image_gray()' and 'read_image_binary()'
…ss instances for methods 'read_image_gray()' and 'read_image_binary()'
…ss instances for methods 'read_image_gray()' and 'read_image_binary()'
@jingchaoluan
Copy link
Author

Thank you. @kba

I write a simple script to only call methods "read_image_gray()" and "read_image_binary()" with a parameter 'file name' and 'PIL.Image.Image instance' respectively. And print their return value (array type) to compare.

I didn't put these two methods in image processing to compare their output, in which I need to change the script 'ocropus-nlbin' and 'ocropus-gpageseg' to call these two methods with PIL.Image.Image instance respectively. Certainly, I also test them in local, if you need I can upload the script and updated scripts 'ocropus-nlbin' and 'ocropus-gpageseg' which only changed two code lines to call these two methods with PIL.Image.Image instance.

Besides, I upload the script in TXT file tyle for the limitation of uploaded file type.

ocrlib_disk_mem.txt

@jingchaoluan
Copy link
Author

Hi @kba

Sorry for replying the unit test so late. The attached file is the unit test code for testing functions read_image_gray() and read_image_binary().

Because these two functions accept a raw image and a binarized image respectively, I used two images (tests/testpage.png and tests/010030.bin.png) and their related PIL.Image objects to test.

You can run this test code by uncomprocessing it under folder 'ocropy', and certainly with the updated read_image_gray() and read_image_binary().

test_read_image.py.zip

kba added a commit to jingchaoluan/ocropy that referenced this pull request Feb 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants