0

This is the code I have tried:

import os

path = r'/Users/J2015/Desktop'

order = 'convert -density 100 -colorspace rgb {} {}'.format(os.path.join(path, r'testfile.pdf') , os.path.join(path, r'testimage.jpg'))
os.system(order)

However I recieve the following error:

sh: convert: command not found

I wish to use ImageMagic for this conversion. Could you please tell me what's wrong? Do I need read or write to have the final .jpg file? And is it possible to extend it to a couple of .pdf converting?

J2015
  • 320
  • 4
  • 24

2 Answers2

0

try this

PDFTOJPGPATH = r"path of your converter pdftojpg.exe"
PDFFILE = "your pdf file.pdf"

import subprocess
subprocess.Popen('"%s" -jpg "%s" out' % (PDFTJPGPATH, PDFFILE))
Ayoub Benayache
  • 1,046
  • 12
  • 28
  • Could you tell me from where you found pdftojpg, I just checked poppler directory but couldn't find any pdftojpg.exe – J2015 Nov 16 '19 at 12:25
0

The command you are executing is missing magick in the beginning. According to the docs, the command is magick convert ... Hence your code should be like this,

import os

path = r'/Users/J2015/Desktop' # this should be the absolute path to you pdf and image file, here I have simply kept it as the author of the question for easy understanding of the changes I mentioned.

order = 'magick convert -density 100 -colorspace rgb {} {}'.format(os.path.join(path, r'testfile.pdf') , os.path.join(path, r'testimage.jpg'))
os.system(order)
Manu S Pillai
  • 899
  • 8
  • 13
  • The error is actually the shell telling you ```command convert is unknown```, i.e convert is not a recognised command or executable file, which is true, as ```magick convert``` is the command. – Manu S Pillai Nov 16 '19 at 10:29
  • Are you sure you have installed Imagemagic properly? Are you able to do it outside python? I.e directly from the shell? – Manu S Pillai Nov 16 '19 at 10:32
  • `os.system` is deprecated, you need `shell=True`, see [replacing-os-system](https://docs.python.org/3/library/subprocess.html#replacing-os-system) – stovfl Nov 16 '19 at 10:37
  • @ManuSPillai convert: no images defined `/Users/J2015/Desktop/testimage.jpg' @ error/convert.c/ConvertImageCommand/3273. – J2015 Nov 16 '19 at 10:38
  • @stovfl Good to know, Thanks! – J2015 Nov 16 '19 at 10:38
  • Provide the full correct path to the pdf and image, i.e ```C://..``` – Manu S Pillai Nov 16 '19 at 10:39
  • 1
    I am using MacOS and the files are located in the desktop! – J2015 Nov 16 '19 at 10:43
  • 1
    @J2015 are you able to do it through the shell directly? – Manu S Pillai Nov 16 '19 at 10:45
  • Also, if you want to use Imagemagic just to convert your pdf to image. Doing it using a python package is a better approach I suggest, [this](https://stackoverflow.com/questions/46184239/extract-a-page-from-a-pdf-as-a-jpeg#) can help you with that. – Manu S Pillai Nov 16 '19 at 10:48
  • @ManuSPillai Now i'm using subprocess.call(command, shell=True) to run the command, it works perfectly when command is 'date' , however, I think problem is all in the command line and maybe output file as image output. – J2015 Nov 16 '19 at 12:35