Solved

Convert PDF to IMAGE with perl/pythjon

Posted on 2013-11-24
10
1,499 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
ID: 39673680
0
 

Author Comment

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

Thanks
0
 
LVL 28

Expert Comment

by:pepr
ID: 39673819
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
ID: 39673839
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
ID: 39673864
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:pelnisgproup
ID: 39673880
@pepr

Why the produce images are in black.?

Thanks
0
 
LVL 28

Expert Comment

by:pepr
ID: 39673886
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
ID: 39673922
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
ID: 39673965
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
ID: 39674325
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to roll back kernel via script 4 62
regex to extract ip:john 17 69
how to split multiple lines delimiter : 8 78
Modify a small python script 19 96
The purpose of this article is to demonstrate how we can use conditional statements using Python.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

911 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

18 Experts available now in Live!

Get 1:1 Help Now