How to reduce wand memory usage?

前端 未结 4 1632
南方客
南方客 2021-01-13 09:36

I am using wand and pytesseract to get the text of pdfs uploaded to a django website like so:

image_pdf = Image(blob=read_pdf_file, resolution=300)
image_png         


        
4条回答
  •  情话喂你
    2021-01-13 10:02

    I was also suffering from memory leaks issues. After some research and tweaking the code implementation, my issues were resolved. I basically worked correctly using with and destroy() function.

    In some cases I could use with to open and read the files, as in the example below:

    with Image(filename = pdf_file, resolution = 300) as pdf:
    

    This case, using with, the memory and tmp files are correctly managed.

    And in another case I had to use the destroy() function, preferably inside a try / finally block, as below:

    try:
        for img in pdfImg.sequence:
        # your code
    finally:
        pdfImg.destroy()
    

    The second case, is an example where I cann't use with because I had to iterate the pages through the sequence, so, I already had the file open and was iterating your pages.

    This conbination of solution resolved my problems with memory leaks.

提交回复
热议问题