Home  >  Article  >  Backend Development  >  Python method to convert pdf to image

Python method to convert pdf to image

不言
不言Original
2018-04-23 15:15:126647browse

Below I will share with you a Python method to convert pdf into pictures. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

This article records how to use Python to split a PDF file into pictures, including environment configuration and version compatibility issues.

Environment configuration (mac)

Install ImageMagick

brew install imagemagick

There is a pit here , brew installations are all version 7.x. When using wand, an error will occur, and you need to install version 6.x.

Solution:

1. Install version 6.x

brew install imagemagick@6

2. Unlink 7.x version

brew unlink imagemagick
Unlinking /usr/local/Cellar/imagemagick/7.0.7-4… 71 symlinks removed

3. Force link to 6.x version

 brew link imagemagick@6 --force
Linking /usr/local/Cellar/imagemagick@6/6.9.9-15… 75 symlinks created

4 .export environment variable

echo 'export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"' >> ~/.bash_profile

ok, the above solves the imagemagick version problem.

Install gs

Must install gs, otherwise pdf cannot be converted.

brew install gs

Install wand

pip3 install wand

I am using python3 here, so I need to use pip3.

Code implementation

from wand.image import Image
def convert_pdf_to_jpg(filename):
 with Image(filename=filename) as img :
  print('pages = ', len(img.sequence))
  with img.convert('jpeg') as converted:
   converted.save(filename='image/page.jpeg')

Effect

The author has transferred more than 400 pages of a book, and you can try it too.



##

The above is the detailed content of Python method to convert pdf to image. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn