Solved

Convert PDF to IMAGE with perl/pythjon

Posted on 2013-11-24
10
1,467 Views
Last Modified: 2013-11-25
OK
We've no more time to spend searching for a solution because bug is too old now :(.

SO let's start with a perl or python script. Please help me to write a script  according to specs below :
- choice of perl or python will be done according to easiest way to install and configure perl/python for a web server and restrict to some directory only on a VirtualHost apache conf.

- script will be called from an GET HTTP call.
- arguments of script are:
  * docroot relative path of source pdf file
  * docroot relative path of a destination lowres folder
  * docroot relative path of a destination thumbnail folder

e.g. http://my.project.com/foo/convertpdf.pl?src=data/upload/ftp/blah/myfile.pdf&lowres=/data/upload/_LOWRES/ftp/blah&thumb=/data/upload/_THUMB/ftp/blah

- script will produces 2 jpg images from 1st page of the pdf:
  * lowres file will be jpeg of 1st pdf page with max size = 220x220 pixels. w/h ratio of pdf page is preserved of course.
  * thumb file will be jpeg of 1st pdf page with max size = 48x48 pixels. w/h ratio of pdf page is preserved of course.


Could you help me to solve this ?
0
Comment
Question by:pelnisgproup
  • 5
  • 4
10 Comments
 
LVL 17

Expert Comment

by:Dushan911
Comment Utility
0
 

Author Comment

by:pelnisgproup
Comment Utility
@Dushan911 I am not asking for tool.I am asking about perl scrtip or python.

Thanks
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
Dushan is right. You need something faster than pure Python/Perl implementation of the task.

I suggest to use the matured ImageMagick (http://www.imagemagick.org/) set of utilities. The one you want is named convert. You can use the PythonMagick (http://www.imagemagick.org/download/python/) wrapper.
0
 

Author Comment

by:pelnisgproup
Comment Utility
Yes, just for information , we need

1)script will be called from an GET HTTP call ( check a specs above).
2) we not working on window platform.


and the tool suggested by @Dushan911 is .

Free PDF to Image Converter works on Windows XP, Windows Vista, Windows 7 and Windows 8, both 32-bit and 64-bit versions.

Thanks
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
OK. Use the ImageMagick. It is implemented for your platform. Here is how the conversion can be done:
import PythonMagick

img = PythonMagick.Image('a.pdf')    # use your full path here
img.write('original.jpg')

imgLowRes = PythonMagick.Image(img)  # make a copy
imgLowRes.resize('220x')
imgLowRes.write('lowRes.jpg')

img.resize('48x')
img.write('thumb.jpg')

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:pelnisgproup
Comment Utility
@pepr

Why the produce images are in black.?

Thanks
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
Try to use img.sample(...) instead of the resize.

Also, img = PythonMagick.Image('a.pdf[0]') -- notice the zero in the brackets--will extract the first page.
0
 

Author Comment

by:pelnisgproup
Comment Utility
Hello pepr,

- script will produces 2 jpg images from 1st page of the pdf:
  * lowres file will be jpeg of 1st pdf page with max size = 220x220 pixels. w/h ratio of pdf page is preserved of course.
  * thumb file will be jpeg of 1st pdf page with max size = 48x48 pixels. w/h ratio of pdf page is preserved of course.

Could you improve that

#!c:/Python27/python.exe -u
import PythonMagick
img = PythonMagick.Image('Hello.pdf')    # use your full path here
img.write('original.jpeg')

imgLowRes = PythonMagick.Image(img)  # make a copy
imgLowRes.sample('220x')
imgLowRes.write('lowRes.jpeg')

img.sample('48x')
img.write('thumb.jpeg')

Open in new window


From that script still produces images in bad quality.

Any idea?

Thanks
0
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
Comment Utility
For the bad quality first. (I did not notice.) PDF is basically a vector format. This way the image object needs to render it to pixels first, It probably uses some default (rather low) resolution. Try to create the empty image first, set the wanted density (DPI as string), and only then load the PDF file into:
import PythonMagick

pdfname = 'a.pdf'             # use your full path here

img = PythonMagick.Image()    # empty object first
img.density('300')            # set the density for reading (DPI); must be as a string
img.read(pdfname + '[0]')     # read the first page of PDF
img.write('original.jpg')

imgLowRes = PythonMagick.Image(img)  # make a copy
imgLowRes.sample('x220')
imgLowRes.write('lowRes.jpg')

Open in new window

If I understand the first part of the question... Do you know whether the PDF files are always potraits? I.e., should always the height be converted to 220?

Warning: The bigger density you use, the longer it takes to render the PDF. Good compromise should be found.
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
Did you solve all the problems? You may like more:
#!python3

import os
import PythonMagick


def smallAndThumbnail(fname, smallpath, thumbnailpath):

    name, ext = os.path.splitext(os.path.basename(fname))
    smallfname = os.path.join(smallpath, name + '.jpg')
    thumbfname = os.path.join(thumbnailpath, name + '.jpg')
 
    img = PythonMagick.Image()    # empty object first
    img.density('300')            # set the density for readeing; must be as a string
    img.read(fname + '[0]')       # read the first page of PDF

    # Background color.
    bgcolor = 'white'             # can have the form '#ffffff'
    
    # The small image first.
    geometry = '220x220'
    imgResult = PythonMagick.Image(geometry, bgcolor)  # init -- the square of the colour
    imgCopy = PythonMagick.Image(img)                  # make a copy
    imgCopy.sample(geometry)
    imgResult.composite(imgCopy, 
                        PythonMagick.GravityType.CenterGravity, 
                        PythonMagick.CompositeOperator.CopyCompositeOp)
    imgResult.write(smallfname)

    # The thumbnail.
    geometry = '48x48'
    imgResult = PythonMagick.Image(geometry, bgcolor)  # init -- the square of the colour
    imgCopy = PythonMagick.Image(img)                  # make a copy
    imgCopy.sample(geometry)
    imgResult.composite(imgCopy, 
                        PythonMagick.GravityType.CenterGravity, 
                        PythonMagick.CompositeOperator.CopyCompositeOp)
    imgResult.write(thumbfname)


if __name__ == '__main__':

    smallAndThumbnail('./pdf/a.pdf', './small', './thumbnail')

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now