Simple script to make .png thumbnails from .zip archive...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K P S

    #1

    Simple script to make .png thumbnails from .zip archive...

    Hi.
    I'm looking for a small script that will take a .zip archive and pull
    the first .jpg from the archive and convert it to a .png.

    The reason for this is I want to have tuhmbnails for these archives in
    nautilus under gnome. I would like something similar to the following
    code, which will pull a thumbnail from an openoffice.org (oasis)
    document. What I want is a little more involved, I guess, since I
    don't know the name of the file (for the zip.read command), and I need
    to convert the file from .jpg to .png once I get it. Any help would be
    appreciated. Including a pointer to a web page of a manual with
    examples. :-)

    #!/usr/bin/python

    import zipfile
    import sys
    import gnomevfs

    inURL=gnomevfs. get_local_path_ from_uri(sys.ar gv[1])
    outURL=sys.argv[2]

    zip=zipfile.Zip File(inURL,mode ="r")
    picture=zip.rea d("Thumbnails/thumbnail.png")
    thumbnail=open( outURL,"w")
    thumbnail.write (picture)
    thumbnail.write ("/n")
    zip.close()
    thumbnail.close ()

  • hdante

    #2
    Re: Simple script to make .png thumbnails from .zip archive...

    Hi,

    I don't know zipfile by heart, but python official documentation is
    always good ( docs.python.org ). You need a loop in the file list like
    this:

    for file in zip:
    process(file)

    Unfortunatelly, there are too many ways to create a thumbnail from an
    image. I'll cite one, using the "python image" external module, that
    I've found to be very easy:

    import Image
    def process(file):
    try:
    image = Image.open(file )
    image.thumbnail ((128,128), Image.ANTIALIAS )
    image.save (file + '.thumb.png')
    except:
    print 'Skipping file', file

    Links:
    http://docs.python.org/lib/lib.html - Python Library Reference
    http://www.pythonware.com/library/pi...book/image.htm - The Image
    Module

    K P S wrote:[color=blue]
    > Hi.
    > I'm looking for a small script that will take a .zip archive and pull
    > the first .jpg from the archive and convert it to a .png.
    >
    > The reason for this is I want to have tuhmbnails for these archives in
    > nautilus under gnome. I would like something similar to the following
    > code, which will pull a thumbnail from an openoffice.org (oasis)
    > document. What I want is a little more involved, I guess, since I
    > don't know the name of the file (for the zip.read command), and I need
    > to convert the file from .jpg to .png once I get it. Any help would be
    > appreciated. Including a pointer to a web page of a manual with
    > examples. :-)
    >
    > #!/usr/bin/python
    >
    > import zipfile
    > import sys
    > import gnomevfs
    >
    > inURL=gnomevfs. get_local_path_ from_uri(sys.ar gv[1])
    > outURL=sys.argv[2]
    >
    > zip=zipfile.Zip File(inURL,mode ="r")
    > picture=zip.rea d("Thumbnails/thumbnail.png")
    > thumbnail=open( outURL,"w")
    > thumbnail.write (picture)
    > thumbnail.write ("/n")
    > zip.close()
    > thumbnail.close ()[/color]

    Comment

    • Scott David Daniels

      #3
      Re: Simple script to make .png thumbnails from .zip archive...

      hdante wrote:[color=blue]
      > .... there are too many ways to create a thumbnail from an
      > image. I'll cite one, using the "python image" external module, that
      > I've found to be very easy:
      >
      > import Image
      > def process(file):
      > try:
      > image = Image.open(file )
      > image.thumbnail ((128,128), Image.ANTIALIAS )
      > image.save (file + '.thumb.png')
      > except:
      > print 'Skipping file', file
      >
      > Links:
      > http://docs.python.org/lib/lib.html - Python Library Reference
      > http://www.pythonware.com/library/pi...book/image.htm - The Image
      > Module[/color]

      That, by the way, is the "PIL" library that you'll see a lot about --
      The Python Imaging Library that the effbot is justly proud of. You
      won't do better than that.

      --Scott David Daniels
      scott.daniels@a cm.org

      Comment

      • K P S

        #4
        Re: Simple script to make .png thumbnails from .zip archive...

        Thanks a lot.
        I'm really new to python, and haven't coded in over a decade, so
        please be patient. :-)

        I'm able to read a .jpg from a .zip archive, but can't seem to
        manipulate it. If I do this:

        zip=zipfile.Zip File(inURL,mode ="r")
        picture=zip.rea d("00.jpg")

        I get the image, but it is of "type" ZipFile. How can I change it to
        type Image? Or am I thinking about this in the wrong way? I would
        like to follow this with something like:

        picture.thumbna il((128, 128), Image.ANTIALIAS )

        But obviously I can't do this directly. What am I missing?

        hdante wrote:[color=blue]
        > Hi,
        >
        > I don't know zipfile by heart, but python official documentation is
        > always good ( docs.python.org ). You need a loop in the file list like
        > this:
        >
        > for file in zip:
        > process(file)
        >
        > Unfortunatelly, there are too many ways to create a thumbnail from an
        > image. I'll cite one, using the "python image" external module, that
        > I've found to be very easy:
        >
        > import Image
        > def process(file):
        > try:
        > image = Image.open(file )
        > image.thumbnail ((128,128), Image.ANTIALIAS )
        > image.save (file + '.thumb.png')
        > except:
        > print 'Skipping file', file
        >
        > Links:
        > http://docs.python.org/lib/lib.html - Python Library Reference
        > http://www.pythonware.com/library/pi...book/image.htm - The Image
        > Module
        >
        > K P S wrote:[color=green]
        > > Hi.
        > > I'm looking for a small script that will take a .zip archive and pull
        > > the first .jpg from the archive and convert it to a .png.
        > >
        > > The reason for this is I want to have tuhmbnails for these archives in
        > > nautilus under gnome. I would like something similar to the following
        > > code, which will pull a thumbnail from an openoffice.org (oasis)
        > > document. What I want is a little more involved, I guess, since I
        > > don't know the name of the file (for the zip.read command), and I need
        > > to convert the file from .jpg to .png once I get it. Any help would be
        > > appreciated. Including a pointer to a web page of a manual with
        > > examples. :-)
        > >
        > > #!/usr/bin/python
        > >
        > > import zipfile
        > > import sys
        > > import gnomevfs
        > >
        > > inURL=gnomevfs. get_local_path_ from_uri(sys.ar gv[1])
        > > outURL=sys.argv[2]
        > >
        > > zip=zipfile.Zip File(inURL,mode ="r")
        > > picture=zip.rea d("Thumbnails/thumbnail.png")
        > > thumbnail=open( outURL,"w")
        > > thumbnail.write (picture)
        > > thumbnail.write ("/n")
        > > zip.close()
        > > thumbnail.close ()[/color][/color]

        Comment

        • Ant

          #5
          Re: Simple script to make .png thumbnails from .zip archive...

          Try adapting the other posters example with something like:

          import Image, StringIO
          zip=zipfile.Zip File(inURL,mode ="r")
          picture=zip.rea d("00.jpg")
          image = Image.open(Stri ngIO(picture))
          image.thumbnail ((128,128), Image.ANTIALIAS )
          image.save (file + '.thumb.png')

          I haven't tested it, but something like this should work.

          Comment

          • Fredrik Lundh

            #6
            Re: Simple script to make .png thumbnails from .zip archive...

            K P S wrote:
            [color=blue]
            > I'm able to read a .jpg from a .zip archive, but can't seem to
            > manipulate it. If I do this:
            >
            > zip=zipfile.Zip File(inURL,mode ="r")
            > picture=zip.rea d("00.jpg")
            >
            > I get the image, but it is of "type" ZipFile.[/color]

            string, more likely (zip is a ZipFile object, picture is a string of
            bytes). PIL expects a file name or a file object, so you need to use
            the StringIO adapter:

            picture = zip.read(name) # returns a string
            file = StringIO.String IO(picture) # wrap string in file-like object
            image = Image.open(file ) # create image object from file-like object

            </F>

            Comment

            • Fredrik Lundh

              #7
              Re: Simple script to make .png thumbnails from .zip archive...

              K P S wrote:
              [color=blue]
              > What I want is a little more involved, I guess, since I
              > don't know the name of the file (for the zip.read command)[/color]

              the first example on this page shows you how to get the names of all
              files in a zip file:



              </F>

              Comment

              • K P S

                #8
                Re: Simple script to make .png thumbnails from .zip archive...

                Thanks everyone.

                One last thing (I hope).

                How can I get the name of just the first file in a zipfile? I see
                routines to list all the files in a zip archive, but I don't see any to
                list only the first, or only the second, etc. It doesn't look like
                zipfile is storing info in a useful array that I can hash into. Or is
                it?

                Actually, I would like to get the name of the first file with a .jpg
                extension, if that's not asking too much. :-)

                zip=zipfile.Zip File("text.zip" ,mode="r")
                picture=zip.rea d(zip.namelist( 0))

                This doesn't seem to work. :-(

                Comment

                • hdante@gmail.com

                  #9
                  Re: Simple script to make .png thumbnails from .zip archive...

                  K P S wrote:[color=blue]
                  > Thanks everyone.[/color]
                  [color=blue]
                  > routines to list all the files in a zip archive, but I don't see any to
                  > list only the first, or only the second, etc. It doesn't look like[/color]

                  If you can list all the files, then you can list only the first. :-)
                  Don't worry about python internal allocation procedures. It will try to
                  make things in an efficient way. Just make sure your code looks cool.
                  ;-)
                  [color=blue]
                  >
                  > zip=zipfile.Zip File("text.zip" ,mode="r")
                  > picture=zip.rea d(zip.namelist( 0))[/color]

                  That's because you're using the wrong syntax. -> zip.namelist()[0]

                  However, I bet that your first file in the zip is not a jpeg file. Do
                  this, instead:

                  zip = zipfile.ZipFile ('text.zip')
                  jpeglist = [x for x in zip.namelist() if '.jp' in x]
                  try:
                  picture = zip.read(jpegli st[0])
                  except IndexError:
                  print 'No jpeg found'

                  Comment

                  • K P S

                    #10
                    Re: Simple script to make .png thumbnails from .zip archive...

                    Thanks a lot! This is what I ended up with.
                    (I would like to get rar archive support, but browsing the web it looks
                    like rar support isn't in any python library (yet)) :-( Anyway, I was
                    able to use the below code unchanged to create thumbnails in nautilus
                    based on the first .jpg file in a .zip archive.

                    Is there any rar support module in python?

                    Thanks again.


                    #!/usr/bin/python

                    import zipfile
                    import sys
                    import gnomevfs
                    import Image
                    import StringIO

                    inURL=gnomevfs. get_local_path_ from_uri(sys.ar gv[1])
                    outURL=sys.argv[2]

                    zip=zipfile.Zip File(inURL,mode ="r")
                    jpeglist=[x for x in zip.namelist() if '.jp' in x]
                    try:
                    picture=zip.rea d(jpeglist[0])
                    except IndexError:
                    print 'No jpeg found'

                    zip.close() #close the file, since we no longer have need of it
                    image = Image.open(Stri ngIO.StringIO(p icture)) # create image object
                    from file-like object
                    image.thumbnail ((128,128),Imag e.ANTIALIAS) #create the thumbnail
                    image.save (outURL, "PNG") #output the file in the proper format

                    Comment

                    Working...