ImageMagick not authorized to convert PDF to an image - python

I have a program, in which I need to convert a PDF to an image using Image Magick. I do that using the subprocess package:
cmd = 'magick convert -density 300 '+pdfFile+'['+str(rangeTuple[0])+'-'+str(rangeTuple[1])+'] -depth 8 '+'temp.tiff' #WINDOWS
if(os.path.isfile('temp.tiff')):
os.remove('temp.tiff')
subprocess.call(cmd,shell=True)
im = Image.open('temp.tiff')
The error I got is:
convert-im6.q16: not authorized `temp2.pdf' # error/constitute.c/ReadImage/412.
convert-im6.q16: no images defined `temp.tiff' # error/convert.c/ConvertImageCommand/3258.
Traceback (most recent call last):
File "UKExtraction2.py", line 855, in <module>
doItAllUpper("A0","UK5.csv","temp",59,70,"box",2,1000,firstPageCoordsUK,boxCoordUK,voterBoxCoordUK,internalBoxNumberCoordUK,externalBoxNumberCoordUK,addListInfoUK)
File "UKExtraction2.py", line 776, in doItAllUpper
doItAll(tempPDFName,outputCSV,2,pdfs,formatType,n_blocks,writeBlockSize,firstPageCoords,boxCoord,voterBoxCoord,internalBoxNumberCoord,externalBoxNumberCoord,addListInfo,pdfName)
File "UKExtraction2.py", line 617, in doItAll
mainProcess(pdfName,(0,noOfPages-1),formatType,n_blocks,outputCSV,writeBlockSize,firstPageCoords,boxCoord,voterBoxCoord,internalBoxNumberCoord,externalBoxNumberCoord,addListInfo,bigPDFName,basePages)
File "UKExtraction2.py", line 542, in mainProcess
im = Image.open('temp.tiff')
File "/home/rohit/.local/lib/python3.6/site-packages/PIL/Image.py", line 2609, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'temp.tiff'
The most important of which is:
convert-im6.q16: not authorized `temp2.pdf' # error/constitute.c/ReadImage/412.
I think this is because ImageMagick isn't authorized to access the PDF. What should be done now? I'm on a Linux server. Any help is appreciated.

emcconville is correct. More specifically edit the Imagemagick policy.xml file to uncomment this line:
<!-- <policy domain="module" rights="none" pattern="{PS,PDF,XPS}" /> -->
And change it from rights="none" to rights="read|write"
<policy domain="module" rights="read|write" pattern="{PS,PDF,XPS}" />
This was a recent addition to the policy.xml file, I believe, due to a security flaw found in the Ghostscript delegate. I think that flaw has now been fixed in the current version of Ghostscript, which is 9.25.
NOTE: On some systems the policy line will have domain="coder" rather than domain="module"

Quick and easy solution:
sudo mv /etc/ImageMagick-6/policy.xml /etc/ImageMagick-6/policy.xml.off
When done, you can restore the original with
sudo mv /etc/ImageMagick-6/policy.xml.off /etc/ImageMagick-6/policy.xml

I am using Dockerfile to update an image, and suddenly I got the policy.xml file in my way. although the version of Ubuntu (xenial) was the same and ImageMagick as well.
I ended up removing the single line causing my problem.
RUN sed -i 's/^.*policy.*coder.*none.*PDF.*//' /etc/ImageMagick-6/policy.xml
hope this helps someone

Use the below command to delete the policy file to fix it, If required you can also take backup of this policy file.
rm /etc/<ImageMagick_PATH>/policy.xml
for me it was ImageMagick6 and the command was :
sudo rm /etc/ImageMagick-6/policy.xml

Related

PDF file not downloading, JPG file ok

I recently re-activated a python project from last year. It still worked. I recently bought a new computer and upgraded to Windows 11. I also updated my Pycharm to the latest and updated to python 10.1. The program no longer works. Which update is the culprit?
Below shows the essence of the code which does not work. I can successfully download .jpg files but not .pdf files.
from wand.image import Image
file1 = 'U:/Image Files/Bonnie Dundee.jpg'
print(file1)
img1 = Image(filename=file1, resolution=300)
file2 = 'U:/Image Files/Frosty Morning.pdf'
print(file2)
img2 = Image(filename=file2, resolution=300)
The error trace is:
C:\Users\jradc\PycharmProjects\Scratch\venv\Scripts\python.exe C:/Users/jradc/PycharmProjects/Scratch/main.py
U:/Image Files/Bonnie Dundee.jpg
U:/Image Files/Frosty Morning.pdf
Traceback (most recent call last):
File "C:\Users\jradc\PycharmProjects\Scratch\main.py", line 7, in <module>
img2 = Image(filename=file2, resolution=300)
File "C:\Users\jradc\PycharmProjects\Scratch\venv\lib\site-packages\wand\image.py", line 9144, in __init__
self.read(filename=filename)
File "C:\Users\jradc\PycharmProjects\Scratch\venv\lib\site-packages\wand\image.py", line 9815, in read
self.raise_exception()
File "C:\Users\jradc\PycharmProjects\Scratch\venv\lib\site-packages\wand\resource.py", line 222, in raise_exception
raise e
wand.exceptions.DelegateError: FailedToExecuteCommand `"gswin64c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r300x300" -dPrinted=false "-sOutputFile=C:/Users/jradc/AppData/Local/Temp/magick-xx4FiylIV-id96p7igp2OZkQb3jLaJ_t%d" "-fC:/Users/jradc/AppData/Local/Temp/magick-wcsbcSOqVJ1dFFk0CAU_gvAVIYrZi1Ut" "-fC:/Users/jradc/AppData/Local/Temp/magick-1GWjBArMZle6Xrv-HTjtyKzCqE1Csf8H"' (The system cannot find the file specified.
) # error/delegate.c/ExternalDelegateCommand/516
Process finished with exit code 1
Could anyone point to what I need to change to get this back to working?
I corrected the file extension.
Looks like the system could not find gswin64c.exe.
py-Wand uses that to convert PDF to PNG.
Check that ghostscript (you probably want the AGPL release) is installed and in your Path.
In the "explorer", search for gswin64c.exe and note the directory where it is.
Then look for "Environment variables" in the start menu and verify that the aforementioned directory is in the Path. If not, edit the path and add it.

How do I download a file from a website in python without the users username?

So I am trying to make a download tool to download stuff via a link here is a snippet from my code
if download_choice == "14":
print("Downloading test file to Desktop...")
myfile14 = requests.get(url14)
open('c:/users/%userprofile%/desktop/', 'wb').write(myfile14.content)
I want to make a program that can download stuff via a link. I want it to run on all PCs not only on mine. Here is the error:
Traceback (most recent call last): File "C:\Users\David\Desktop\Windows Download Tool\Python\WDT.py", line 100, in <module> open('c:/users/%userprofile%/desktop/', 'wb').write(myfile14.content) FileNotFoundError: [Errno 2] No such file or directory: 'c:/users/%userprofile%/desktop/'
Python does not use %userprofile% to get the username of the executing user.
To achieve this, you need to import the package os and run it's getlogin function. This returns a string with the current username
Example:
import os
username = os.getlogin()
if download_choice == "14":
print("Downloading test file to Desktop...")
myfile14 = requests.get(url14)
open(f'C:/Users/{username}/desktop/', 'wb').write(myfile14.content)
I am using an f-string for opening the file, since it is preferred by PEP-8 (it's just correct code-styling)
Attention: This only works on a windows machine

Why is Python pyusb usb.core access denied due to permissions and why won't the rules.d fix it?

I have a USB device which I'm tring to talk to using pyusb 1.0.2 on linux (Linux tpad 4.15.0-38-generic #41~16.04.1-Ubuntu SMP Wed Oct 10 20:16:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux). Running python 3.5 I get the following error (full trace at bottom of this post):
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)
Mounting the usb and using lsusb and then inspecting it:
udevadm info -a -p $(udevadm info -q path -n /dev/bus/usb/006/011)
shows
looking at device '/devices/pci0000:00/0000:00:10.0/usb6/6-1':
KERNEL=="6-1"
SUBSYSTEM=="usb"
DRIVER=="usb"
ATTR{authorized}=="1"
ATTR{avoid_reset_quirk}=="0"
ATTR{bConfigurationValue}=="1"
ATTR{bDeviceClass}=="00"
ATTR{bDeviceProtocol}=="00"
ATTR{bDeviceSubClass}=="00"
ATTR{bMaxPacketSize0}=="8"
ATTR{bMaxPower}=="98mA"
ATTR{bNumConfigurations}=="1"
ATTR{bNumInterfaces}==" 1"
ATTR{bcdDevice}=="0111"
ATTR{bmAttributes}=="80"
ATTR{busnum}=="6"
ATTR{configuration}==""
ATTR{devnum}=="11"
ATTR{devpath}=="1"
ATTR{idProduct}=="0001"
ATTR{idVendor}=="17a4"
ATTR{ltm_capable}=="no"
ATTR{manufacturer}=="Concept2"
ATTR{maxchild}=="0"
ATTR{product}=="Concept2 Performance Monitor 3 (PM3)"
ATTR{quirks}=="0x0"
ATTR{removable}=="unknown"
ATTR{serial}=="300118412"
ATTR{speed}=="12"
ATTR{urbnum}=="12"
ATTR{version}==" 1.10"
So I wrote a rule in /etc/udev/rules.d/10-local.rules like this (FYI -- I've also tried "user1" which is the user python shows it is running under and I've tried both ":=" and "="):
SUBSYSTEMS=="usb", ATTRS{idVendor}=="17a4", ATTRS{idProduct}=="0001", GROUP:="users", MODE="0777"
Then I ran udevadm test /devices/pci0000:00/0000:00:10.0/usb6/6-1
which shows:
calling: test
version 229
This program is for debugging only, it does not run any program
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.
=== trie on-disk ===
tool version: 229
file size: 7049340 bytes
header size 80 bytes
strings 1759644 bytes
nodes 5289616 bytes
Load module index
timestamp of '/etc/systemd/network' changed
timestamp of '/lib/systemd/network' changed
Parsed configuration file /lib/systemd/network/99-default.link
Created link configuration context.
timestamp of '/etc/udev/rules.d' changed
Reading rules file: /etc/udev/rules.d/10-local.rules
Reading rules file: /lib/udev/rules.d/40-crda.rules
[removed long list of rule files...]
Skipping empty file: /etc/udev/rules.d/99-usbftdi.rules
rules contain 393216 bytes tokens (32768 * 12 bytes), 33403 bytes strings
25688 strings (211409 bytes), 22263 de-duplicated (181432 bytes), 3426 trie nodes used
GROUP 100 /etc/udev/rules.d/10-local.rules:1
MODE 0777 /etc/udev/rules.d/10-local.rules:1
value '[dmi/id]sys_vendor' is 'LENOVO'
value '[dmi/id]sys_vendor' is 'LENOVO'
IMPORT builtin 'usb_id' /lib/udev/rules.d/50-udev-default.rules:13
IMPORT builtin 'hwdb' /lib/udev/rules.d/50-udev-default.rules:13
MODE 0664 /lib/udev/rules.d/50-udev-default.rules:41
PROGRAM 'mtp-probe /sys/devices/pci0000:00/0000:00:10.0/usb6/6-1 6 11' /lib/udev/rules.d/69-libmtp.rules:1923
starting 'mtp-probe /sys/devices/pci0000:00/0000:00:10.0/usb6/6-1 6 11'
'mtp-probe /sys/devices/pci0000:00/0000:00:10.0/usb6/6-1 6 11'(out) '0'
Process 'mtp-probe /sys/devices/pci0000:00/0000:00:10.0/usb6/6-1 6 11' succeeded.
handling device node '/dev/bus/usb/006/011', devnum=c189:650, mode=0664, uid=0, gid=100
set permissions /dev/bus/usb/006/011, 020664, uid=0, gid=100
setting mode of /dev/bus/usb/006/011 to 020664 failed: Operation not permitted
setting owner of /dev/bus/usb/006/011 to uid=0, gid=100 failed: Operation not permitted
ACTION=add
BUSNUM=006
DEVNAME=/dev/bus/usb/006/011
DEVNUM=011
DEVPATH=/devices/pci0000:00/0000:00:10.0/usb6/6-1
DEVTYPE=usb_device
DRIVER=usb
ID_BUS=usb
ID_MODEL=Concept2_Performance_Monitor_3__PM3_
ID_MODEL_ENC=Concept2\x20Performance\x20Monitor\x203\x20\x28PM3\x29
ID_MODEL_FROM_DATABASE=Performance Monitor 3
ID_MODEL_ID=0001
ID_REVISION=0111
ID_SERIAL=Concept2_Concept2_Performance_Monitor_3__PM3__300118412
ID_SERIAL_SHORT=300118412
ID_USB_INTERFACES=:030000:
ID_VENDOR=Concept2
ID_VENDOR_ENC=Concept2
ID_VENDOR_FROM_DATABASE=Concept2
ID_VENDOR_ID=17a4
MAJOR=189
MINOR=650
PRODUCT=17a4/1/111
SUBSYSTEM=usb
TYPE=0/0/0
USEC_INITIALIZED=3850171749
Unload module index
Unloaded link configuration context.
However after doing this python still reports access errors to the USB device.
Traceback (most recent call last):
File "/home/user1/PycharmProjects/PyRow/statshow.py", line 22, in <module>
erg = pyrow.pyrow(ergs[0])
File "/home/user1/PycharmProjects/PyRow/pyrow.py", line 61, in __init__
usb.util.claim_interface(erg, INTERFACE)
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/util.py", line 205, in claim_interface
device._ctx.managed_claim_interface(device, interface)
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/core.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/core.py", line 159, in managed_claim_interface
self.managed_open()
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/core.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/core.py", line 120, in managed_open
self.handle = self.backend.open_device(self.dev)
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/backend/libusb1.py", line 786, in open_device
return _DeviceHandle(dev)
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/backend/libusb1.py", line 643, in __init__
_check(_lib.libusb_open(self.devid, byref(self.handle)))
File "/home/user1/PycharmProjects/camera/venv/lib/python3.5/site-packages/usb/backend/libusb1.py", line 595, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)
I'm out of ideas on how to fix this. Are there any suggestions?
Using this answer I found I could correct the permissions problem using the plugdev group.
So in a terminal I used
groups
groups [myuserid]
and verified plugdev was there and the user was part of that group. Then I put the following line in /etc/udev/rules.d/10-local.rules
SUBSYSTEMS=="usb", ENV{DEVTYPE}=="usb_device",
ATTRS{idVendor}=="17a4", ATTRS{idProduct}=="0001", GROUP="plugdev",
MODE="0777"
I'm not sure if the devtype and if 0777 or 0666 is proper, but this is what worked.
After making the changes I also ran the following commands to reset the rules for the system:
sudo udevadm control --reload
sudo udevadm trigger
given that this is the first result on google, and that i don't see the answer that fixed this issue for me, I figured i'd post.
the udev rules do not allow comments and seems to fail pretty much silently, so if you tried adding comments to keep track of your devices, you'll have broken the permissions for those devices.
SUBSYSTEMS=="usb", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="faf0", GROUP="plugdev", MODE="0777"
will work, but
SUBSYSTEMS=="usb", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="faf0", GROUP="plugdev", MODE="0777" # thorlabs KB101 motor controller
will not

Write permission running Python in Notepad++

I'm using Winpython on Windows 10 and notepad++, and want to run the following Python code:
import wave, struct, math
sampleRate = 44100.0 # hertz
duration = 1.0 # seconds
frequency = 440.0 # hertz
wavef = wave.open('sound.wav','w')
wavef.setnchannels(1) # mono
wavef.setsampwidth(2)
wavef.setframerate(sampleRate)
for i in range(int(duration * sampleRate)):
value = int(32767.0*math.cos(frequency*math.pi*float(i)/float(sampleRate)))
data = struct.pack('<h', value)
wavef.writeframesraw( data )
wavef.writeframes('')
wavef.close()
However I get the following error:
Traceback (most recent call last):
File "C:\Users\HP\Desktop\WinPython-64bit-2.7.10.3\robin\sound\wav_00.py", line 7, in <module>
wavef = wave.open('sound.wav','w')
File "C:\Users\HP\Desktop\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\lib\wave.py", line 513, in open
return Wave_write(f)
File "C:\Users\HP\Desktop\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\lib\wave.py", line 308, in __init__
f = __builtin__.open(f, 'wb')
IOError: [Errno 13] Permission denied: 'sound.wav'
I'm running the script using C:\Users\HP\Desktop\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\python.exe -i "$(FULL_CURRENT_PATH)"
When I run the script outside of the Winpython folder with my registered Python installation it works fine, but I want to make use of packages that are installed in the Winpython version, so this solution is not adequate
Please could someone explain how to make this work?
Try running as administrator.
If you running from the windows command prompt:
Right click windows button
Select "Command Prompt(Admin)"
Execute the script
Hope this helps

Python cPickle unable to load an OCR model library

I have just installed ocropus OCR with all dependencies in my windows 7 machine. (I am using 32bit python 2.7) It seems to be working fine except that I cannot load the default OCR model: en-default.pyrnn.gz. , and receiving a Traceback. I am using the following syntax:
python ocropus-rpred -m en-default.pyrnn.gz book\0001\*.png
here is the error
INFO: #inputs47
# loading object /usr/local/share/ocropus/en-default.pyrnn.gz
Traceback (most recent call last):
File "ocropus-rpred" line 109, in <module>
network = ocrolib.load_object(args.model,verbose=1)
File "C:\anaconda32\lib\site-packages\ocrolib\common.py", line 513, in load_object
return unpickler.load()
EOFError
I have checked the file is not empty; also double checked the binary mode flag enabled i.e. "wb" and "rb"; also converted the newlines of common.py using dos2unix. I am being unable to unable to solve this problem. If anyone have expereinced similar issues, kindly share.
import cPickle
import gzip
def save_object(fname,obj,zip=0):
if zip==0 and fname.endswith(".gz"):
zip = 1
if zip>0:
# with gzip.GzipFile(fname,"wb") as stream:
with os.popen("gzip -9 > '%s'"%fname,"wb") as stream:
cPickle.dump(obj,stream,2)
else:
with open(fname,"wb") as stream:
cPickle.dump(obj,stream,2)
def unpickle_find_global(mname,cname):
if mname=="lstm.lstm":
return getattr(lstm,cname)
if not mname in sys.modules.keys():
exec "import "+mname
return getattr(sys.modules[mname],cname)
def load_object(fname,zip=0,nofind=0,verbose=0):
"""Loads an object from disk. By default, this handles zipped files
and searches in the usual places for OCRopus. It also handles some
class names that have changed."""
if not nofind:
fname = ocropus_find_file(fname)
if verbose:
print "# loading object",fname
if zip==0 and fname.endswith(".gz"):
zip = 1
if zip>0:
# with gzip.GzipFile(fname,"rb") as stream:
with os.popen("gunzip < '%s'"%fname,"rb") as stream:
unpickler = cPickle.Unpickler(stream)
unpickler.find_global = unpickle_find_global
return unpickler.load()
else:
with open(fname,"rb") as stream:
unpickler = cPickle.Unpickler(stream)
unpickler.find_global = unpickle_find_global
return unpickler.load()
UPDATE: Hi, please note that I have used Python's native gzip, and it is working fine. Thank you for pointing that out. Here is the correct syntax that is working on Windows: {with gzip.GzipFile(fname,"rb") as stream:}
Your use of gunzip (in the load_object function) is incorrect. Unless passed the -c argument, gunzip writes the decompressed data to a new file, not to its stdout (which is what you seem to be attempting to do).
As a result, it doesn't write anything to its stdout, and your stream variable contains no data, hence the EOFError.
A quick fix is to change your gunzip command line to give it the -c argument.
More info here: http://linux.die.net/man/1/gzip
That said, why are you even shelling out to gunzip to decompress your data? Python's built-in gzip module should handle that without problems.

Categories