I've downloaded and installed PythonMagick for python 2.7, 64 bit Windows 7, from the Unofficial Windows Binaries.
I am trying to run this code (Processor.py)
import PythonMagick
pdf = 'test.pdf'
p = PythonMagick.Image()
p.density('600')
p.read(pdf)
p.write('doc.jpg')
within this folder (D:\Python Projects\Sheet Music Reader)
However, using that relative pdf path or pdf = "D:\\Python Projects\\Sheet Music Reader" results in this error;
Traceback (most recent call last):
File "D:/Python Projects/Sheet Music Reader/Processor.py", line 6, in <module>
p.read(pdf)
RuntimeError: Magick: PostscriptDelegateFailed `D:\Python Projects\Sheet Music Reader\test.pdf':
No such file or directory # error/pdf.c/ReadPDFImage/664
I simply don't understand why it can't find my pdf; it's in the same directory as the python script.
What's causing this error, and how do I fix it?
(I've on the impression that converting pdfs to images in python is a night mare)
I had exactly the same problem couple of days ago. While converting from .gif (oder something else) to .jpg worked really fine, converting from .pdf to .jpg produced exactly the same error. Thats happing because ImageMagick uses Ghostscript for reading/converting PDFs.
You can solve the problem by installing Ghostscript (only 32-bit version works). Don't forget to add "C:\Program Files (x86)\gs\gs9.06\bin" to your systempath.
Here a step-by-step-guide how I was getting PythonMagick work:
(I'm using Python 2.7.3 32-bit on Windows 7 64-bit.)
Install the newest version of ImageMagick ("ImageMagick-6.8.1-1-Q16-windows-dll.exe" at the moment of writing. Note that this is the 32-bit version; 64-bit works for me fine too).
DON'T forget to check the option "Install development headers and libraries for C and C++".
Set "MAGICK_HOME" environment to the path of ImageMagick (for me C:\Program Files (x86)\ImageMagick-6.8.1-Q16).
Additional set this path to your systemwide-path at the very first position if it isn't already there.
Download and install the 32-bit version of GhostScript (64 bit won't work, even if you have installed the 64-bit version of ImageMagick).
Set C:\Program Files (x86)\gs\gs9.06\bin to your systemwide-path, right after ImageMagick.
Check if your setup works. Try convert some.pdf some.jpg in the command line. If it doesn't work you've done something wrong at point 1-3.
Install PythonMagick with the unofficial binary, not with easy_install or pip.
(Again: I'm using the 32-bit Python 2.7.3 interpreter, so I took "PythonMagick-0.9.7.win32-py2.7.exe" for that.)
Start you Python command line util and try something like this:
from PythonMagick import Image
im = Image()
im.read(r"C:\Path\To\Some.pdf")
im.write("some.jpg")
Additional an example for a PDF with multiple pages:
import os
from pyPdf import PdfFileReader, PdfFileWriter
from tempfile import NamedTemporaryFile
from PythonMagick import Image
reader = PdfFileReader(open("some.pdf", "rb"))
for page_num in xrange(reader.getNumPages()):
writer = PdfFileWriter()
writer.addPage(reader.getPage(page_num))
temp = NamedTemporaryFile(prefix=str(page_num), suffix=".pdf", delete=False)
writer.write(temp)
temp.close()
im = Image()
im.density("300") # DPI, for better quality
im.read(temp.name)
im.write("some_%d.jpg" % (page_num))
os.remove(temp.name)
That's the only workaround for that problem which comes into my mind.
Related
I'm trying to generate a pdf file from a .dot file, I'm using python and the IDE pycharm but for some reason it is giving me that error, I already have Graphviz installed.
This is the code I used to generate the .pdf file but it gives me the error
dirname = os.path.dirname(__file__)
direcc = os.path.join(dirname, 'ast.dot')
file = open(direcc, "w+", encoding="utf-8")
file.write(graph)
file.close()
os.system('dot -T pdf -o ast.pdf ast.dot')
The code generates the ast.dot file tough. But when trying to generate the pdf file is when I get the error.
I already have Grpahviz added to me Environment Variables
Not sure what the problem is since I've created images and pdf files using graphviz in other programming languages. I'm using Pycharm's latest version, not sure if it has something to do with this issue.
This is the error I am getting:
OS Error: Ghostscript not found on paths
When running the following function:
from PIL import Image
def convert_ps_to_png(ps_file):
img = Image.open(ps_file)
img = Image.save("file.png")# Error occurs on this line
convert_ps_to_png(ps_file_path)
Any solutions would be appreciated. The end goal is to convert a .ps file to a .png file through a script.
As per the answers to your previous questions, you need to have Ghostscript installed, and available in the system environment variable $PATH.
I'm trying to generate a pdf417 barcode in python using treepoem but pycharm keeps giving me the following error:
Traceback (most recent call last):
File "C:/Users/./Documents/barcodes.py", line 175, in
image = generate_barcode(barcode_type="pdf417",data=barcode, options=dict(eclevel=5, rows=27, columns=12))
File "C:\Users.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\treepoem__init__.py", line 141, in generate_barcode
bbox_lines = _get_bbox(code)
File "C:\Users.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\treepoem__init__.py", line 81, in _get_bbox
ghostscript = _get_ghostscript_binary()
File "C:\Users.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\treepoem__init__.py", line 108, in _get_ghostscript_binary
'Cannot determine path to ghostscript, is it installed?'
treepoem.TreepoemError: Cannot determine path to ghostscript, is it installed?
I've tried to install ghostcript, using both the .exe I found online and using pip install ghostscript (successfully completed the first time, and now tells me the requirement is satisfied), yet I still keep getting this error. Any ideas on how to fix it?
You are installing on Windows, the Windows binary differs in name from the Linux binaries and indeed differs depending whether you installed the 64 or 32-bit version.
On Linux (and MacOS) the Ghostscript binary is called 'gs', on Windows its 'gswin32' or 'gswin64' or 'gswin32c' or 'gswin64c' depending on whether you want the 32 or 64 bit version, and the command line or windowed executable.
My guess is that your script is looking for simply 'gs' and is probably expecting the path to be in the $PATH environment variable, its not clear to me what its expecting.
You could probably 'fix' this by making sure the installation path is in the $PATH environment variable and copying the executable to 'gs.exe' in that directory.
Other than that you'll need someone who can tell you what the script is looking for. Quite possibly you could just grep it.
Another solution is to edit the C:\Users\Windows.UserName\AppData\Local\Programs\Python\Python37\Lib\site-packages\treepoem__init__.py
the script is looking for gs.exe, change to gswin32.exe as shown below.
Then add the GhostScriptInstallDir\bin in the PATH in windows.
def _get_ghostscript_binary():
binary = "gswin32" # changed from 'gs' to 'gswin32'
if sys.platform.startswith("win"):
binary = EpsImagePlugin.gs_windows_binary
if not binary:
raise TreepoemError(
"Cannot determine path to ghostscript, is it installed?"
)
return binary
Hi I am trying the python library pytesseract to extract text from image.
Please find the code:
from PIL import Image
from pytesseract import image_to_string
print image_to_string(Image.open(r'D:\new_folder\img.png'))
But the following error came:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string
config=config)
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract
stderr=subprocess.PIPE)
File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
I did not found a specific solution to this. Can anyone help me what to do. Anything more to be downloaded or from where i can download it etc..
Thanks in advance :)
I had the same trouble and quickly found the solution after reading this post:
OSError: [Errno 2] No such file or directory using pytesser
Just need to adapt it to Windows, replace the following code:
tesseract_cmd = 'tesseract'
with:
tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract'
(need double \\ to escape first \ in the string)
You're getting exception because subprocess isn't able to find the binaries (tesser executable).
The installation is a 3 step process:
1.Download/Install system level libs/binaries:
For various OS here's the help. For MacOS you can directly install it using brew.
Install Google Tesseract OCR (additional info how to install the
engine on Linux, Mac OSX and Windows). You must be able to invoke the
tesseract command as tesseract. If this isn’t the case, for example
because tesseract isn’t in your PATH, you will have to change the
“tesseract_cmd” variable at the top of tesseract.py. Under
Debian/Ubuntu you can use the package tesseract-ocr. For Mac OS users.
please install homebrew package tesseract.
For Windows:
An installer for the old version 3.02 is available for Windows from
our download page. This includes the English training data. If you
want to use another language, download the appropriate training data,
unpack it using 7-zip, and copy the .traineddata file into the
'tessdata' directory, probably C:\Program Files\Tesseract-OCR\tessdata.
To access tesseract-OCR from any location you may have to add the
directory where the tesseract-OCR binaries are located to the Path
variables, probably C:\Program Files\Tesseract-OCR.
Can download the .exe from here.
2.Install Python package
pip install pytesseract
3.Finally, you need to have tesseract binary in you PATH.
Or, you can set it at run-time:
import pytesseract
pytesseract.pytesseract.tesseract_cmd = '<path-to-tesseract-bin>'
For Windows:
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
The above line will make it work temporarily, for permanent solution add the tesseract.exe to the PATH - such as PATH=%PATH%;"C:\Program Files (x86)\Tesseract-OCR".
Beside that make sure that TESSDATA_PREFIX Windows environment variable is set to the directory, containing tessdata directory. For example:
TESSDATA_PREFIX=C:\Program Files (x86)\Tesseract-OCR
i.e. tessdata location is: C:\Program Files (x86)\Tesseract-OCR\tessdata
Your example:
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
print pytesseract.image_to_string(Image.open(r'D:\new_folder\img.png'))
You need Tesseract OCR engine ("Tesseract.exe") installed in your machine. If the path is not configured in your machine, provide complete path in pytesseract.py(tesseract.py).
README
Install Google Tesseract OCR (additional info how to install the engine on Linux, Mac OSX and Windows). You must be able to invoke the tesseract command as tesseract. If this isn't the case, for example because tesseract isn't in your PATH, you will have to change the "tesseract_cmd" variable at the top of tesseract.py. Under Debian/Ubuntu you can use the package tesseract-ocr. For Mac OS users. please install homebrew package tesseract.
Another thread
I have also faced the same problem regarding pytesseract.
I would suggest you to work in linux environment, to solve such errors.
Do the following commands in linux:
pip install pytesseract
sudo apt-get update
sudo apt-get install pytesseract-ocr
Hope this will do the work..
I have a peculiar problem at the intersection of the EMF image format, the python PIL (as well as Pillow) image libraries, and the Pyinstaller program for packaging Python into a Windows executable.
I have a script that uses PIL/Pillow to convert an EMF file to JPEG. This works correctly when I run the python script in python. However, when I package it into an EXE using Pyinstaller.exe -F, it does not work.
With the Pillow version, I get a simple error saying
"Cannot convert image1.emf".
With the PIL version, I get a longer message that says:
Traceback (most recent call last):
File "", line 38, in
File "", line 27, in convertImageFile
File "C:\Embibe\Git\content-ingestion\src\build\convertImage\out00-PYZ.pyz\PIL
.Image", line 2126, in open
IOError: cannot identify image file 'image1.emf'
Has anyone else encountered this and found a working solution?
The gory details follow, if you need them... :-)
OS: Windows 7 64-bit (but all software is 32 bit)
Software:: Python:2.7.5, Pyinstaller:2.1, PIL:built-in with Python, Pillow: 2.4.0
Python script convImg.py:
from __future__ import print_function
import os, sys
from PIL import Image
for infile in sys.argv[1:]:
f, e = os.path.splitext(infile)
outfile = f + ".jpg"
if infile != outfile:
try:
Image.open(infile).convert('RGB').save(outfile)
except IOError:
print("cannot convert", infile)
run as: convImg.py image1.emf works correctly and generates image1.jpg.
When packaged to exe using \python27\scripts\pyinstaller.exe -F convImg.py and run as convImg.exe image1 gives the errors listed above for the Pillow and PIL versions.
I found a related post here, Pyinstaller troubles with Pillow but the solution for it, namely using py2app instead of pyinstaller is not an option for me since that is for MacOS and I need Windows. I considered using similar alternatives for windows, py2exe and cx_freeze, but they do not create a single self-contained exe like pyinstaller does.
Thanks,
Amit
Ok, I found the answer to my own question at http://www.py2exe.org/index.cgi/py2exeAndPIL
The issue is that PIL relies on dynamically loading many image plugins, and when packaged using pyinstaller or py2exe, it cannot find these plugins. So the key is to
a. explicitly import all the plugins in your code
b. mark the Image class status as already initialized
c. explicitly specify the target format to the save command
So, I modify convImag.py to be:
from __future__ import print_function
import os, sys
from PIL import Image
from PIL import BmpImagePlugin,GifImagePlugin,Jpeg2KImagePlugin,JpegImagePlugin,PngImagePlugin,TiffImagePlugin,WmfImagePlugin # added this line
Image._initialized=2 # added this line
for infile in sys.argv[1:]:
f, e = os.path.splitext(infile)
outfile = f + ".jpg"
if infile != outfile:
try:
Image.open(infile).convert('RGB').save(outfile,"JPEG") # added "JPEG"
except IOError:
print("cannot convert", infile)
After this, the pyinstaller tool works like a charm and the packaged exe runs correctly :-)
Thanks to g.d.d.c. for confirming I was on the right track with the solution!