How Python turns a PDF into a picture

Source: Internet
Author: User
Tags imagemagick
Below for you to share a Python pdf into a picture of the method, has a good reference value, I hope to be helpful to everyone. Come and see it together.

This article records how to use Python to slice a PDF file into a single picture, including environment configuration, version compatibility issues.

Environment configuration (MAC)

Installing ImageMagick

Brew Install ImageMagick

Here is a pit, brew installation is 7.x version, using wand error, you need to install 6.x version.

Workaround:

1. Install the 6.x version

Brew Install Imagemagick@6

2. Unlink 7.x version

Brew Unlink imagemagickunlinking/usr/local/cellar/imagemagick/7.0.7-4 ... Symlinks removed

3. Mandatory Link 6.x version

Brew Link imagemagick@6--forcelinking/usr/local/cellar/imagemagick@6/6.9.9-15 ... Symlinks created

4.export Environment variables

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

OK, the above resolves the ImageMagick version issue.

Install GS

The GS must be installed, otherwise the PDF cannot be converted.

Brew Install GS

Installing Wand

PIP3 Install Wand

I use Python3 here, so I need to use PIP3.

Code implementation

From wand.image import imagedef 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

I will be a book more than 400 pages have been turned out, we can also go to try it.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.