While using Wand and imageMagick to convert a scanned PDF to text, I am getting the following error:
Error:
Traceback (most recent call last):
File "C:/Users/gibin/PycharmProjects/ML/Image_PDF/.ksldwjldf.py", line 28, in <module>
Get_text_from_image(r"C:\Users\gibin\PycharmProjects\ML\Image_PDF\536676972_image.pdf")
File "C:/Users/gibin/PycharmProjects/ML/Image_PDF/.ksldwjldf.py", line 13, in Get_text_from_image
pdf=wi(filename=pdf_path,resolution=300)
File "C:\Users\gibin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\wand\image.py", line 8212, in __init__
units=units)
File "C:\Users\gibin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\wand\image.py", line 8686, in read
self.raise_exception()
File "C:\Users\gibin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\wand\resource.py", line 240, in raise_exception
raise e
wand.exceptions.DelegateError: FailedToExecuteCommand `"gswin32c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r300x300" "-sOutputFile=C:/Users/GIBIN_~1./AppData/Local/Temp/magick-23476_sCYGtEq3gb-%d" "-fC:/Users/GIBIN_~1./AppData/Local/Temp/magick-234763X1vpsurlvH5" "-fC:/Users/GIBIN_~1./AppData/Local/Temp/magick-23476fUlS8Tr85dwk"' (The system cannot find the file specified.
) @ error/delegate.c/ExternalDelegateCommand/459
Code:
import io
from PIL import Image
import pytesseract
from wand.image import Image as wi
import gc
pytesseract.pytesseract.tesseract_cmd = r"C:\Users\gibin\AppData\Local\Tesseract-OCR\tesseract.exe"
def Get_text_from_image(pdf_path):
print(pdf_path)
pdf=wi(filename=pdf_path,resolution=300)
pdfImg=pdf.convert('jpeg')
imgBlobs=[]
extracted_text=[]
for img in pdfImg.sequence:
page=wi(image=img)
imgBlobs.append(page.make_blob('jpeg'))
print(len(imgBlobs))
for imgBlob in imgBlobs:
im=Image.open(io.BytesIO(imgBlob))
text=pytesseract.image_to_string(im)
print(text)
extracted_text.append(text)
return ([i.replace("\n","") for i in extracted_text])
Get_text_from_image(r"C:\Users\gibin\PycharmProjects\ML\Image_PDF\536676972_image.pdf")
This is working fine after installing GHOSTSCRIPT and adding it as an environment variable. Download ghostscript from HERE
After that, you need to set the environment variable. Add a new system variable:
Variable: GS_PROG
Value: Full path to the location of your gswin64c.exe file
Have you seen this Imagemagick Convert PDF to JPEG: FailedToExecuteCommand `"gswin32c.exe" / PDFDelegateFailed ?. Instead you can also use other methods to convert pdf to jpg images page wise. I have used pdf2img library to do that, if you are free to use any library then prefer using pdf2img.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.