Python method to convert pdf to image

不言
Release: 2018-04-23 15:15:12
Original
6639 people have browsed it

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
Copy after login

2. Unlink 7.x version

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

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
Copy after login

4 .export environment variable

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

ok, the above solves the imagemagick version problem.

Install gs

Must install gs, otherwise pdf cannot be converted.

brew install gs
Copy after login

Install wand

pip3 install wand
Copy after login

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')
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!