Tux

...making Linux just a little more fun!

2-cent tip: Convert a bunch of Images from one format to another

Amit Kumar Saha [amitsaha.in at gmail.com]


Tue, 22 Apr 2008 11:21:30 +0530

convert is a command line tool available with ImageMagick which can be used to convert between image formats. This shell script uses it to convert a bunch of images from '.ps' to '.png'. Follow embedded instructions to customize it to your needs.

#Script begins here
 
#!/bin/bash
 
# Uses the 'convert' utility to convert a bunch of images from one
#format to another
# the script should reside in the same directory as the images
# You need to have 'ImageMagick' installed
#(http://www.imagemagick.org/index.php)
 
# By Amit K.Saha
# Help taken from http://howtoforge.com/forums/showthread.php?t=4493
 
#to convert to/from different formats
# change 'ext' to reflect the format you want to convert "from"
# Chnage target to the format you want to convert to
 
ext=".ps"
target="png"
 
for i in *.ps
do
base=$(basename $i $ext) #to extract only the filename and NOT the extension
convert $i $base.$target
 
done
#Script ends here

Try it and suggest improvements!

Thanks, Amit

-- 
Amit Kumar Saha
*NetBeans Community
Docs Coordinator*
http://amitsaha.in.googlepages.com
http://amitksaha.blogspot.com


Top    Back


Kapil Hari Paranjape [kapil at imsc.res.in]


Tue, 22 Apr 2008 11:35:01 +0530

Hello,

On Tue, 22 Apr 2008, Amit Kumar Saha wrote:

> ext=".ps"
> target="png"
> 
> for i in *.ps
> do
> base=$(basename $i $ext) #to extract only the filename and NOT the extension
> convert $i $base.$target
> 
> done
> #Script ends here
> 
> Try it and suggest improvements!

Depending on the context for which you are making the conversion you may need to use additional options for "convert". For example,

	-density        the resolution of the output
	-quality        the compression level of the output

In order to keep track of the precise parameters which you use, the conjure command is useful as it runs a "script".

Finally, note that "convert" uses "ghostscript" to perform the conversion so some people may find it easier to use the latter program directly.

Regards,

Kapil. --


Top    Back


Amit Kumar Saha [amitsaha.in at gmail.com]


Tue, 22 Apr 2008 11:45:39 +0530

On Tue, Apr 22, 2008 at 11:35 AM, Kapil Hari Paranjape <kapil@imsc.res.in> wrote:

> Hello,
>
>
>  On Tue, 22 Apr 2008, Amit Kumar Saha wrote:
>  > ext=".ps"
>  > target="png"
>  >
>  > for i in *.ps
>  > do
>  > base=$(basename $i $ext) #to extract only the filename and NOT the extension
>  > convert $i $base.$target
>  >
>  > done
>  > #Script ends here
>  >
>  > Try it and suggest improvements!
>
>  Depending on the context for which you are making the conversion you
>  may need to use additional options for "convert". For example,
>
>         -density        the resolution of the output
>         -quality        the compression level of the output
>
>  In order to keep track of the precise parameters which you use, the
>  conjure command is useful as it runs a "script".

Wow. Interesting indeed- XML again :-) I love it when i see more & more places where XML is used..

>
>  Finally, note that "convert" uses "ghostscript" to perform the
>  conversion so some people may find it easier to use the latter
>  program directly.

Could you please elucidate this a bit more? Is this a specific ".ps" case you are talking about?

Regards, Amit

-- 
Amit Kumar Saha
*NetBeans Community
Docs Coordinator*
http://amitsaha.in.googlepages.com
http://amitksaha.blogspot.com


Top    Back


Karl-Heinz Herrmann [kh1 at khherrmann.de]


Tue, 22 Apr 2008 08:33:13 +0200

Hi,

> >  Finally, note that "convert" uses "ghostscript" to perform the
> >  conversion so some people may find it easier to use the latter
> >  program directly.
> 
> Could you please elucidate this a bit more? Is this a specific ".ps"
> case you are talking about?
> 

he has something like this in mind:

gs -dNOPAUSE -g1024x768 -r205 -sDEVICE=pngalpha \
-sOutputFile=Images_%d.png -dBATCH Vortrag.pdf

this line (-r for resolution) and the target size (-g) is optimized for LaTeX/beamer created slides and creates a series of images -- one each page -- with 1024x768. You can of course modify that to something more reasonable for A4 page sized documents:

gs -dNOPAUSE -r300 -sDEVICE=pngalpha \
-sOutputFile=Images_%d.png -dBATCH file.ps

this will create the image with 300dpi and as alrge as they'll be at that resolution. careful -- this can take long for a large ps/pdf document and eats lots of RAM.

convert itself will not interpret postscript -- it will delegate the postscript to gs in a similar way and take the output of gs. But using gs directly you've better control over the rendering parameters (like pngalpha as target-Device should include antialiasing).

K.-H.


Top    Back


Amit Kumar Saha [amitsaha.in at gmail.com]


Tue, 22 Apr 2008 12:14:51 +0530

On Tue, Apr 22, 2008 at 12:03 PM, Karl-Heinz Herrmann <kh1@khherrmann.de> wrote:

> Hi,
>
>
>  > >  Finally, note that "convert" uses "ghostscript" to perform the
>  > >  conversion so some people may find it easier to use the latter
>  > >  program directly.
>  >
>  > Could you please elucidate this a bit more? Is this a specific ".ps"
>  > case you are talking about?
>  >
>
>  he has something like this in mind:
>
>  ``
>  gs -dNOPAUSE -g1024x768 -r205 -sDEVICE=pngalpha \
>  -sOutputFile=Images_%d.png -dBATCH Vortrag.pdf
>  ''
>
>  this line (-r for resolution) and the target size (-g) is optimized for
>  LaTeX/beamer created slides and creates a series of images -- one each
>  page -- with 1024x768. You can of course modify that to something more
>  reasonable for A4 page sized documents:
>
>  ``
>  gs -dNOPAUSE -r300 -sDEVICE=pngalpha \
>  -sOutputFile=Images_%d.png -dBATCH file.ps
>  ''
>
>  this will create the image with 300dpi and as alrge as they'll be at
>  that resolution. careful -- this can take long for a large ps/pdf
>  document and eats lots of RAM.
>
>
>  convert itself will not interpret postscript -- it will delegate the
>  postscript to gs in a similar way and take the output of gs. But using
>  gs directly you've better control over the rendering parameters (like
>  pngalpha as target-Device should include antialiasing).

Oh, great!

I misinterpreted the "latter program" to be 'conjure',. Oops!

-- 
Amit Kumar Saha
*NetBeans Community
Docs Coordinator*
http://amitsaha.in.googlepages.com
http://amitksaha.blogspot.com


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Tue, 22 Apr 2008 02:39:08 -0400

On Tue, Apr 22, 2008 at 11:21:30AM +0530, Amit Kumar Saha wrote:

> convert is a command line tool available with ImageMagick which can be
> used to convert between image formats. This shell script uses it to
> convert a bunch of images from '.ps' to '.png'. Follow embedded
> instructions to customize it to your needs.
> 
> #Script begins here
> 
> #!/bin/bash
> 
> # Uses the 'convert' utility to convert a bunch of images from one
> #format to another
> # the script should reside in the same directory as the images
> # You need to have 'ImageMagick' installed
> #(http://www.imagemagick.org/index.php)
> 
> # By Amit K.Saha
> # Help taken from http://howtoforge.com/forums/showthread.php?t=4493
> 
> #to convert to/from different formats
> # change 'ext' to reflect the format you want to convert "from"
> # Chnage target to the format you want to convert to
> 
> ext=".ps"
> target="png"
> 
> for i in *.ps
> do
> base=$(basename $i $ext) #to extract only the filename and NOT the extension
> convert $i $base.$target
> 
> done
> #Script ends here
> 
> Try it and suggest improvements!

for i in *.ps; do convert $i ${i%ps}png; done

would do the same thing.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Amit Kumar Saha [amitsaha.in at gmail.com]


Tue, 22 Apr 2008 12:13:45 +0530

On Tue, Apr 22, 2008 at 12:09 PM, Ben Okopnik <ben@linuxgazette.net> wrote:

>
> On Tue, Apr 22, 2008 at 11:21:30AM +0530, Amit Kumar Saha wrote:
>  > convert is a command line tool available with ImageMagick which can be
>  > used to convert between image formats. This shell script uses it to
>  > convert a bunch of images from '.ps' to '.png'. Follow embedded
>  > instructions to customize it to your needs.
>  >
>  > #Script begins here
>  >
>  > #!/bin/bash
>  >
>  > # Uses the 'convert' utility to convert a bunch of images from one
>  > #format to another
>  > # the script should reside in the same directory as the images
>  > # You need to have 'ImageMagick' installed
>  > #(http://www.imagemagick.org/index.php)
>  >
>  > # By Amit K.Saha
>  > # Help taken from http://howtoforge.com/forums/showthread.php?t=4493
>  >
>  > #to convert to/from different formats
>  > # change 'ext' to reflect the format you want to convert "from"
>  > # Chnage target to the format you want to convert to
>  >
>  > ext=".ps"
>  > target="png"
>  >
>  > for i in *.ps
>  > do
>  > base=$(basename $i $ext) #to extract only the filename and NOT the extension
>  > convert $i $base.$target
>  >
>  > done
>  > #Script ends here
>  >
>  > Try it and suggest improvements!
>
>  ```
>  for i in *.ps; do convert $i ${i%ps}png; done
>  '''

The '%' is 'i%ps' strips the '.ps' part ?

Thanks, Amit

-- 
Amit Kumar Saha
*NetBeans Community
Docs Coordinator*
http://amitsaha.in.googlepages.com
http://amitksaha.blogspot.com


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Tue, 22 Apr 2008 02:59:17 -0400

On Tue, Apr 22, 2008 at 12:13:45PM +0530, Amit Kumar Saha wrote:

> On Tue, Apr 22, 2008 at 12:09 PM, Ben Okopnik <ben@linuxgazette.net> wrote:
> >
> >  ```
> >  for i in *.ps; do convert $i ${i%ps}png; done
> >  '''
> 
> The '%' is 'i%ps' strips the '.ps' part ?

The ${param%match} syntax returns the value of the 'param' variable with the shortest string matching 'match' removed from the right side. There's also '%%' (remove longest match from the right side), '#' (remove shortest match from the left side), '##' (remove longest match from the left side), and lots of other handy goodies. See the 'Parameter Expansion' section in the Bash manpage.

I often use "${0##*/}" in my scripts' usage and error messages. Figuring that one out is left as an exercise for the beginning student. :)

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Amit Kumar Saha [amitsaha.in at gmail.com]


Tue, 22 Apr 2008 13:18:16 +0530

On Tue, Apr 22, 2008 at 12:29 PM, Ben Okopnik <ben@linuxgazette.net> wrote:

> On Tue, Apr 22, 2008 at 12:13:45PM +0530, Amit Kumar Saha wrote:
>  > On Tue, Apr 22, 2008 at 12:09 PM, Ben Okopnik <ben@linuxgazette.net> wrote:
>  > >
>
> > >  ```
>  > >  for i in *.ps; do convert $i ${i%ps}png; done
>  > >  '''
>  >
>  > The '%' is 'i%ps' strips the '.ps' part ?
>
>  The ${param%match} syntax returns the value of the 'param' variable with
>  the shortest string matching 'match' removed from the right side.
>  There's also '%%' (remove longest match from the right side), '#'
>  (remove shortest match from the left side), '##' (remove longest match
>  from the left side), and lots of other handy goodies. See the 'Parameter
>  Expansion' section in the Bash manpage.

I am now going to read http://linuxgazette.net/issue55/okopnik.html :-)

>
>  I often use "${0##*/}" in my scripts' usage and error messages. Figuring
>  that one out is left as an exercise for the beginning student. :)

Trying...

Cheers, Amit

-- 
Amit Kumar Saha
*NetBeans Community
Docs Coordinator*
http://amitsaha.in.googlepages.com
http://amitksaha.blogspot.com


Top    Back


Jorge xxxxx [kaeosdonk at gmail.com]


Thu, 24 Apr 2008 10:49:54 +0100

Amit Kumar Saha escribi?:

> On Tue, Apr 22, 2008 at 12:29 PM, Ben Okopnik <ben@linuxgazette.net> wrote:
>> On Tue, Apr 22, 2008 at 12:13:45PM +0530, Amit Kumar Saha wrote:
>>  > On Tue, Apr 22, 2008 at 12:09 PM, Ben Okopnik <ben@linuxgazette.net> wrote:
>>  > >
>>
>>>>  ```
>>  > >  for i in *.ps; do convert $i ${i%ps}png; done
>>  > >  '''
>>  >
>>  > The '%' is 'i%ps' strips the '.ps' part ?
>>
>>  The ${param%match} syntax returns the value of the 'param' variable with
>>  the shortest string matching 'match' removed from the right side.
>>  There's also '%%' (remove longest match from the right side), '#'
>>  (remove shortest match from the left side), '##' (remove longest match
>>  from the left side), and lots of other handy goodies. See the 'Parameter
>>  Expansion' section in the Bash manpage.
> 
> I am now going to read http://linuxgazette.net/issue55/okopnik.html :-)
> 
> 
>>  I often use "${0##*/}" in my scripts' usage and error messages. Figuring
>>  that one out is left as an exercise for the beginning student. :)
> 
> Trying...
> 
> 
> Cheers,
> Amit

<childish-rhyme> I know, I know :P </childish-rhyme>

Strips path Haven't thought about it before, but its quite simplier than using basename all the time.

convert -flip "$i" "`basename "$i" .jpg`".fliped.jpg;
convert -flip "$i" "${i##*/%.jpg}".fliped.jpg

The first is a line I used in an old script The second is a first try to employ the ## construction to get the same result but it still don't look quite proper.Perhaps "${"${i##*/}"%.jpg}"

--


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Thu, 24 Apr 2008 12:13:49 -0400

On Thu, Apr 24, 2008 at 10:49:54AM +0100, Jorge xxxxx wrote:

> Amit Kumar Saha escribi?:
> > On Tue, Apr 22, 2008 at 12:29 PM, Ben Okopnik <ben@linuxgazette.net> wrote:
> >> On Tue, Apr 22, 2008 at 12:13:45PM +0530, Amit Kumar Saha wrote:
> >>  > On Tue, Apr 22, 2008 at 12:09 PM, Ben Okopnik <ben@linuxgazette.net> wrote:
> >>  > >
> >>
> >>>>  ```
> >>  > >  for i in *.ps; do convert $i ${i%ps}png; done
> >>  > >  '''
> >>  >
> >>  > The '%' is 'i%ps' strips the '.ps' part ?
> >>
> >>  The ${param%match} syntax returns the value of the 'param' variable with
> >>  the shortest string matching 'match' removed from the right side.
> >>  There's also '%%' (remove longest match from the right side), '#'
> >>  (remove shortest match from the left side), '##' (remove longest match
> >>  from the left side), and lots of other handy goodies. See the 'Parameter
> >>  Expansion' section in the Bash manpage.
> > 
> > I am now going to read http://linuxgazette.net/issue55/okopnik.html :-)
> > 
> > 
> >>  I often use "${0##*/}" in my scripts' usage and error messages. Figuring
> >>  that one out is left as an exercise for the beginning student. :)
> > 
> > Trying...
> > 
> > 
> > Cheers,
> > Amit
> 
> <childish-rhyme> I know, I know :P
> </childish-rhyme>
> 
> 
> Strips path

Not only that - in the case of ${0##*/}, it strips the path from the curent program name. This means that

[ -z "$1" ] && { printf "${0##*/} [options] <input_file>\n"; exit 1; }

will always report the correct filename for your script rather than whatever it was originally named.

> convert -flip "$i" "`basename "$i" .jpg`".fliped.jpg;
> convert -flip "$i" "${i##*/%.jpg}".fliped.jpg
> 
> The first is a line I used in an old script
> The second is a first try to employ the ## construction to get the same
> result but it still don't look quite proper.Perhaps "${"${i##*/}"%.jpg}"

In this case, you're trying to remove both the prefix and the suffix, so you either need to stick with 'basename' or do it in two steps:

j="${i##*/}"
i="$j%.jpg}"
-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Jorge xxxxx [kaeosdonk at gmail.com]


Fri, 25 Apr 2008 12:31:58 +0100

Ben Okopnik escribi?:

> On Thu, Apr 24, 2008 at 10:49:54AM +0100, Jorge xxxxx wrote:
>> Amit Kumar Saha escribi?:
>>> On Tue, Apr 22, 2008 at 12:29 PM, Ben Okopnik <ben@linuxgazette.net> wrote:
>>>> On Tue, Apr 22, 2008 at 12:13:45PM +0530, Amit Kumar Saha wrote:
>>>>  > On Tue, Apr 22, 2008 at 12:09 PM, Ben Okopnik <ben@linuxgazette.net> wrote:
>>>>
>>>>>>  ```
>>>>  > >  for i in *.ps; do convert $i ${i%ps}png; done
>>>>  > >  '''
>>>>  >
>>>>  > The '%' is 'i%ps' strips the '.ps' part ?
>>>>
>>>>  The ${param%match} syntax returns the value of the 'param' variable with
>>>>  the shortest string matching 'match' removed from the right side.
>>>>  There's also '%%' (remove longest match from the right side), '#'
>>>>  (remove shortest match from the left side), '##' (remove longest match
>>>>  from the left side), and lots of other handy goodies. See the 'Parameter
>>>>  Expansion' section in the Bash manpage.
>>> I am now going to read http://linuxgazette.net/issue55/okopnik.html :-)
>>>
>>>
>>>>  I often use "${0##*/}" in my scripts' usage and error messages. Figuring
>>>>  that one out is left as an exercise for the beginning student. :)
>>> Trying...
>>>
>>>
>>> Cheers,
>>> Amit
>> <childish-rhyme> I know, I know :P
>> </childish-rhyme>
>>
>>
>> Strips path
> 
> Not only that - in the case of ${0##*/}, it strips the path from the
> curent program name. This means that
> 
> ```
> [ -z "$1" ] && { printf "${0##*/} [options] <input_file>\n"; exit 1; }
> '''
> 
> will always report the correct filename for your script rather than
> whatever it was originally named.
> 

I left the meaning of $0 and interpret it was any other variable

>> convert -flip "$i" "`basename "$i" .jpg`".fliped.jpg;
>> convert -flip "$i" "${i##*/%.jpg}".fliped.jpg
>>
>> The first is a line I used in an old script
>> The second is a first try to employ the ## construction to get the same
>> result but it still don't look quite proper.Perhaps "${"${i##*/}"%.jpg}"
> 
> In this case, you're trying to remove both the prefix and the suffix, so
> you either need to stick with 'basename' or do it in two steps:
> 
> ``
> j="${i##*/}"
> i="${j%.jpg}"
> ''
>
After writing and sending I realized it couldn't work as it lacks the variable name (eg there is no variable with name "${i##*/}") but it will simplify the construction as the basename one was getting me fussied over with the order of " ` and any simbols between the operators


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]


Tue, 22 Apr 2008 09:00:42 +0100

On 22/04/2008, Ben Okopnik <ben@linuxgazette.net> wrote:

> ```
>  for i in *.ps; do convert $i ${i%ps}png; done
>  '''
>
>  would do the same thing.

Mwhahahahahaha....

<tongue firmly in cheek> But Ben, my filenames all have spaces in them.....

[thomas@ubuntu ~]$ for i in *.ps; do echo "$i" && convert $i ${i%ps}png; done
example with spaces.ps
convert: unable to open image `with': No such file or directory.
</tongue firmly in cheek>

-- Thomas Adam


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Tue, 22 Apr 2008 15:01:49 -0400

On Tue, Apr 22, 2008 at 09:00:42AM +0100, Thomas Adam wrote:

> On 22/04/2008, Ben Okopnik <ben@linuxgazette.net> wrote:
> > ```
> >  for i in *.ps; do convert $i ${i%ps}png; done
> >  '''
> >
> >  would do the same thing.
> 
> Mwhahahahahaha....
> 
> <tongue firmly in cheek>
> But Ben, my filenames all have spaces in them.....
> 
> ```
> [thomas@ubuntu ~]$ for i in *.ps; do echo "$i" && convert $i ${i%ps}png; done
> example with spaces.ps
> convert: unable to open image `with': No such file or directory.
> '''
> </tongue firmly in cheek>

Yeah, but that's precisely the behavior of Amit's original script - and I did say that it'll do the same thing. :) The problem is trivially easy to fix, though:

for i in *.ps; do convert "$i" "${i%ps}png"; done
-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Jorge xxxxx [kaeosdonk at gmail.com]


Thu, 24 Apr 2008 10:49:20 +0100

Ben Okopnik escribi?:

> On Tue, Apr 22, 2008 at 11:21:30AM +0530, Amit Kumar Saha wrote:
>> convert is a command line tool available with ImageMagick which can be
>> used to convert between image formats. This shell script uses it to
>> convert a bunch of images from '.ps' to '.png'. Follow embedded
>> instructions to customize it to your needs.
>>
>> #Script begins here
>>
>> #!/bin/bash
>>
>> # Uses the 'convert' utility to convert a bunch of images from one
>> #format to another
>> # the script should reside in the same directory as the images
>> # You need to have 'ImageMagick' installed
>> #(http://www.imagemagick.org/index.php)
>>
>> # By Amit K.Saha
>> # Help taken from http://howtoforge.com/forums/showthread.php?t=4493
>>
>> #to convert to/from different formats
>> # change 'ext' to reflect the format you want to convert "from"
>> # Chnage target to the format you want to convert to
>>
>> ext=".ps"
>> target="png"
>>
>> for i in *.ps
>> do
>> base=$(basename $i $ext) #to extract only the filename and NOT the extension
>> convert $i $base.$target
>>
>> done
>> #Script ends here
>>
>> Try it and suggest improvements!
> 
> ```
> for i in *.ps; do convert $i ${i%ps}png; done
> '''
> 
> would do the same thing.
> 
> 
I've done that myself sometimes but you're missing multipage pdf/ps

It would be:

for i in *.ps; do convert "$i" "${i%.ps}"%od.png ; done
 

The %od part is for convert to put a numerical end to the resulting name if it extracts more than one image from one pdf file. It's quite obscured in the ImageMagick help but I found it in an example. Added: I ain't able to find it again. I'm possitive sure it's that way but...

This way what you actually do is "print" a pdf page to an image file. If you want to extract the images embbebed in the pdf/ps you should try pdfimages.Or depending what you're going to do with the images you can try pdftohtml also

This is a script I had to take images from "image only PDFs" and put them into cbr format e-comics. It's very naive and you are advised that it could not work for anyone other than me (and I'm not sure it's the "definitive-usable-version"

#!/bin/bash
BASE="$PWD"
 
for pdf in *.pdf ;
  do
  dir="${pdf%.pdf}"
  mkdir "$dir"
  cd "$dir"
  pdfimages -j ../"$pdf" "$dir"
 
  for i in *.ppm
   do pnmtojpeg --quality 90 --optimize --smooth "$i" > "${i%.ppm}"_P.jpg
  done
 
  jdo -m 50 -cn 100 -q 65 -oa _T -os -ssfvl jdo.log \*.jpg
  find . -type f -not -name '*_T.*' -print0 | xargs -0 rm
  cd "$BASE"
  rar m -m5 -s "$dir".cbr "$dir"
done

jdo & rar are propietary programs used in shareware version, better ignore them.

pdfimages extracts the images that it can in jpg format and the others in ppm format wich is afterwards converted with pnmtojpeg. This behaivour depends on how they were writen into the PDF.

The optimized images (the jdo part) are marked from the originals with _T termination and then the originals erased.

--


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Thu, 24 Apr 2008 11:58:26 -0400

On Thu, Apr 24, 2008 at 10:49:20AM +0100, Jorge xxxxx wrote:

> Ben Okopnik escribi?:
> > On Tue, Apr 22, 2008 at 11:21:30AM +0530, Amit Kumar Saha wrote:
> >> convert is a command line tool available with ImageMagick which can be
> >> used to convert between image formats. This shell script uses it to
> >> convert a bunch of images from '.ps' to '.png'. Follow embedded
> >> instructions to customize it to your needs.

[...]

> > ```
> > for i in *.ps; do convert $i ${i%ps}png; done
> > '''
> > 
> > would do the same thing.
> > 
> > 
> I've done that myself sometimes but you're missing multipage pdf/ps

You're right, and it is an excellent tip - although we have completely drifted away from Amit's original premise, which is simply converting between image formats. On the other hand, it's really useful for multipage PSes and TIFFs.

> It would be:
> 
> ```
> for i in *.ps; do convert "$i" "${i%.ps}"%od.png ; done
> 
> '''

I suspect that you actually meant "%o" or "%d" - or possibly "%0d". These, in order, mean "append an octal number", "append a decimal number", and "append a decimal number preceded by a zero if a field width was defined" (since there isn't one, it means the same thing as the preceding definition.) E.g., if I knew that converting the image resulted in somewhere between 50-70 images and I wanted them properly sorted by the 'ls' command, I'd do this:

for i in *.ps; do convert "$i" "${i%.ps}"%03d.png ; done

This will produce output filenames like 'image001.png', 'image002.png', and so on. Without that '3', though, the numbers wouldn't be prefixed with anything - resulting in a "sorted" list like this:

image1.png
image10.png
image11.png
...
image2.png
image20.png

and so on.

> The %od part is for convert to put a numerical end to the resulting
> name if it extracts more than one image from one pdf file.
> It's quite obscured in the ImageMagick help but I found it in an example.
> Added: I ain't able to find it again. I'm possitive sure it's that way
> but...

It's described at the ImageMagick site, under "Writing a Multi-Image Sequence": http://www.imagemagick.org/Usage/files/#write_seq :

  Not only can you use '%d' for a decimal number, but you can use '%x'
  for a heximedical number (lowercase), '%X' for a heximedical number
  (uppercase), or '%o' for an octal number.
"convert" also has a '-adjoin' option that will auto-number the output images for you without any of the above - but it's not quite as flexible.

> This is a script I had to take images from "image only PDFs" and put
> them into cbr format e-comics.
> It's very naive and you are advised that it could not work for anyone
> other than me (and I'm not sure it's the "definitive-usable-version"
> 
> ```
> #!/bin/bash
> BASE="$PWD"
> 
> for pdf in *.pdf ;
>   do
>   dir="${pdf%.pdf}"
>   mkdir "$dir"

This could create a problem if "$dir" already exists. Perhaps this could be more useful:

dir=`mktemp -d "${pdf%.pdf}.XXXXXX"`

This will strip off the ".pdf" extension, create a directory with that name extended by a six-character random string (which will be tested to ensure that the name does not already exist), and the resulting name will be saved in the "dir" variable.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Jorge xxxxx [kaeosdonk at gmail.com]


Fri, 25 Apr 2008 12:29:52 +0100

Ben Okopnik escribi?:

> On Thu, Apr 24, 2008 at 10:49:20AM +0100, Jorge xxxxx wrote:
>> Ben Okopnik escribi?:
>>> On Tue, Apr 22, 2008 at 11:21:30AM +0530, Amit Kumar Saha wrote:
>>>> convert is a command line tool available with ImageMagick which can be
>>>> used to convert between image formats. This shell script uses it to
>>>> convert a bunch of images from '.ps' to '.png'. Follow embedded
>>>> instructions to customize it to your needs.
> 
> [...]
> 
>>> ```
>>> for i in *.ps; do convert $i ${i%ps}png; done
>>> '''
>>>
>>> would do the same thing.
>>>
>>>
>> I've done that myself sometimes but you're missing multipage pdf/ps
> 
> You're right, and it is an excellent tip - although we have completely
> drifted away from Amit's original premise, which is simply converting
> between image formats. On the other hand, it's really useful for
> multipage PSes and TIFFs.

I didn't consider .ps(.pdf) as an image format but as a container so if I can extract the "(not so)original" image is better and in the other case the conversion page by page. The pdf format in last versions keeps the jpeg images and embeds them without changes.

>  
>> It would be:
>>
>> ```
>> for i in *.ps; do convert "$i" "${i%.ps}"%od.png ; done
>>
>> '''
You're right I thought I had put %0d.

> 
> I suspect that you actually meant "%o" or "%d" - or possibly "%0d".
> These, in order, mean "append an octal number", "append a decimal
> number", and "append a decimal number preceded by a zero if a field
> width was defined" (since there isn't one, it means the same thing as
> the preceding definition.) E.g., if I knew that converting the image
> resulted in somewhere between 50-70 images and I wanted them properly
> sorted by the 'ls' command, I'd do this:
> 
> ```
> for i in *.ps; do convert "$i" "${i%.ps}"%03d.png ; done
> '''
> 
> This will produce output filenames like 'image001.png', 'image002.png',
> and so on. Without that '3', though, the numbers wouldn't be prefixed
> with anything - resulting in a "sorted" list like this:
> 
> ``
> image1.png
> image10.png
> image11.png
> ...
> image2.png
> image20.png
> ''
> 
> and so on.
> 
>> The %od part is for convert to put a numerical end to the resulting
>> name if it extracts more than one image from one pdf file.
>> It's quite obscured in the ImageMagick help but I found it in an example.
>> Added: I ain't able to find it again. I'm possitive sure it's that way
>> but...
> 
> It's described at the ImageMagick site, under "Writing a Multi-Image
> Sequence": http://www.imagemagick.org/Usage/files/#write_seq :
> 
> ``
>   Not only can you use '%d' for a decimal number, but you can use '%x'
>   for a heximedical number (lowercase), '%X' for a heximedical number
>   (uppercase), or '%o' for an octal number.
> ''
>  
> "convert" also has a '-adjoin' option that will auto-number the output
> images for you without any of the above - but it's not quite as
> flexible.
>>   dir="${pdf%.pdf}"
>>   mkdir "$dir"
> 
> This could create a problem if "$dir" already exists. Perhaps this could
> be more useful:
> 
> ```
> dir=`mktemp -d "${pdf%.pdf}.XXXXXX"`
> '''
> 
> This will strip off the ".pdf" extension, create a directory with that
> name extended by a six-character random string (which will be tested to
> ensure that the name does not already exist), and the resulting name
> will be saved in the "dir" variable.
>

Well it was in a directory only with the pdfs so it couldn't happen to be any directory before. That's only the particular case where it was used. I lost some of the scripts I'd developed from this, and any improvement is very welcomed as it work in very particular situation where you really control the enviroment.(the part where I mark the good ones and try to delete the rest gives me pain and that it alone is a reason to keep it from the hands of any living being and probably in a lost paper buried deep)


Top    Back