python image thumbnail generator?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Dewin

    python image thumbnail generator?

    Hi. I run a website for my band, and the other guys want an image gallery.

    I'm thinking it would be nice and easy, if we could just upload a jpg into
    a dir called "gallery/". When the client clicks the "gallery" link, a
    cgi script could search the gallery/ dir, and create thumbnails of any
    jpeg images that don't already have a thumbnail associated with them. The
    script could then generate a page of clickable thumbnails.

    A few questions:

    (1) Can this be done with python? If so, what module do I need to look up?

    (2) If it can't be done with python, is there some small utility that will
    do it for me? Something I could easily install locally in my own
    "public_htm l" dir on my website?

    (3) Is this the sort of thing which, if done regularly, would hog far far
    too much of my webhosts system resources?

    (4) Am I talking about re inventing the wheel?

  • Paul Rubin

    #2
    Re: python image thumbnail generator?

    "Chris Dewin" <chris@wintergr een.in> writes:[color=blue]
    > (1) Can this be done with python? If so, what module do I need to look up?[/color]

    You could do it with PIL, or run jpegtran in an external process.
    jpegtran may be easier.

    Comment

    • Mike C. Fletcher

      #3
      Re: python image thumbnail generator?

      Chris Dewin wrote:
      [color=blue]
      >Hi. I run a website for my band, and the other guys want an image gallery.
      >
      >I'm thinking it would be nice and easy, if we could just upload a jpg into
      >a dir called "gallery/". When the client clicks the "gallery" link, a
      >cgi script could search the gallery/ dir, and create thumbnails of any
      >jpeg images that don't already have a thumbnail associated with them. The
      >script could then generate a page of clickable thumbnails.
      >
      >A few questions:
      >
      >(1) Can this be done with python? If so, what module do I need to look up?
      >
      >[/color]
      PIL can certainly handle the functionality.
      [color=blue]
      >(2) If it can't be done with python, is there some small utility that will
      >do it for me? Something I could easily install locally in my own
      >"public_html " dir on my website?
      >
      >[/color]
      The core function looks something like this:

      import Image # this is PIL

      def getThumbnail( filename, size = (32,32) ):
      '''Get a thumbnail image of filename'''
      image = Image.open(file name)
      rx, ry = image.size[0]/float(size[0]), image.size[1]/float(size[1])
      if rx > ry:
      resize = int(size[0]), int(round(image .size[1]*(1.0/rx), 0))
      else:
      resize = int(round(image .size[0]*(1.0/ry), 0)), int(size[1])
      image = image.resize( resize, Image.BILINEAR )
      newimage = Image.new( 'RGB', size, )
      x,y = image.size
      newimage.paste(
      image, ((size[0]-x)/2, (size[1]-y)/2),
      )
      return newimage

      then you call newImage.save( filename, format ) to save the result.
      [color=blue]
      >(3) Is this the sort of thing which, if done regularly, would hog far far
      >too much of my webhosts system resources?
      >
      >[/color]
      As long as you *only* generate images for sources that don't yet have
      (or are newer than) their thumbnail you should be fine.
      [color=blue]
      >(4) Am I talking about re inventing the wheel?
      >
      >[/color]
      Probably. ZPhotoSlides (a Zope product) provides thumnailing,
      slideshows and the like, probably closer to what you'd want for this
      kind of application. You'll probably find other non-Zope versions as well.

      Good luck,
      Mike

      --
      _______________ _______________ _______________ ___
      Mike C. Fletcher
      Designer, VR Plumber, Coder



      Comment

      • Wouter van Ooijen

        #4
        Re: python image thumbnail generator?

        >I'm thinking it would be nice and easy, if we could just upload a jpg into[color=blue]
        >a dir called "gallery/". When the client clicks the "gallery" link, a
        >cgi script could search the gallery/ dir, and create thumbnails of any
        >jpeg images that don't already have a thumbnail associated with them. The
        >script could then generate a page of clickable thumbnails.[/color]

        I dunno about the scripting aspect, but I use Python to generate my
        website, I already had the images (of the products I sell). I used PIL
        to generate thumbnails of the images. I think it took 2 or 3 lines of
        Python - that is: for me. PIL is of course a bit more.




        Wouter van Ooijen

        -- ------------------------------------

        Webshop for PICs and other electronics

        Teacher electronics and informatics

        Comment

        • Terry Hancock

          #5
          Re: python image thumbnail generator?

          On Saturday 27 August 2005 09:06 pm, Chris Dewin wrote:[color=blue]
          > I'm thinking it would be nice and easy, if we could just upload a jpg into
          > a dir called "gallery/". When the client clicks the "gallery" link, a
          > cgi script could search the gallery/ dir, and create thumbnails of any
          > jpeg images that don't already have a thumbnail associated with them. The
          > script could then generate a page of clickable thumbnails.[/color]

          This is trivial to do with Zope. I wrote a "VarImage" product for it
          that will do the image manipulation you want handily. I never really
          created a "gallery" product for it because it's so trivial to do with a
          regular Zope folder and a couple of scripts, but I would be happy to
          share my Gallery template with you if your interested.

          The current version of VarImage even implements things like HTTP_REFERER
          blocking (or watermarking if you prefer), to discourage remote-linking
          of your images, etc.

          An online example can be seen here: http://narya.net/Gallery

          Not sure about CGI methods of doing it, though.
          --
          Terry Hancock ( hancock at anansispacework s.com )
          Anansi Spaceworks http://www.anansispaceworks.com

          Comment

          • Fredrik Lundh

            #6
            Re: python image thumbnail generator?

            Paul Rubin wrote
            [color=blue][color=green]
            >> (1) Can this be done with python? If so, what module do I need to look
            >> up?[/color]
            >
            > You could do it with PIL, or run jpegtran in an external process.
            > jpegtran may be easier.[/color]

            eh? are you sure you know what jpegtran does?

            JPEGTRAN(1)
            JPEGTRAN(1)

            NAME
            jpegtran - lossless transformation of JPEG files

            SYNOPSIS
            jpegtran [ options ] [ filename ]

            DESCRIPTION
            jpegtran performs various useful transformations of JPEG files. It
            can
            translate the coded representation from one variant of JPEG to
            another,
            for example from baseline JPEG to progressive JPEG or vice versa.
            It
            can also perform some rearrangements of the image data, for
            example
            turning an image from landscape to portrait format by rotation.

            jpegtran works by rearranging the compressed data (DCT
            coefficients),
            without ever fully decoding the image /.../

            </F>



            Comment

            • Fredrik Lundh

              #7
              Re: python image thumbnail generator?

              Mike C. Fletcher wrote:
              [color=blue]
              > The core function looks something like this:
              >
              > import Image # this is PIL
              >
              > def getThumbnail( filename, size = (32,32) ):
              > '''Get a thumbnail image of filename'''
              > image = Image.open(file name)
              > rx, ry = image.size[0]/float(size[0]), image.size[1]/float(size[1])
              > if rx > ry:
              > resize = int(size[0]), int(round(image .size[1]*(1.0/rx), 0))
              > else:
              > resize = int(round(image .size[0]*(1.0/ry), 0)), int(size[1])
              > image = image.resize( resize, Image.BILINEAR )[/color]

              footnote: if you're creating thumbnails from JPEG or PhotoCD
              images, using the "thumbnail" method can be a lot more efficient
              (unlike resize, it tweaks the codec so it doesn't have to load the
              entire full-sized image).

              footnote 2: Image.ANTIALIAS is slower, but tends to give a
              much better result than Image.BILINEAR, especiall for "noisy"
              images.

              </F>



              Comment

              • Paul Rubin

                #8
                Re: python image thumbnail generator?

                "Fredrik Lundh" <fredrik@python ware.com> writes:[color=blue][color=green]
                > > You could do it with PIL, or run jpegtran in an external process.
                > > jpegtran may be easier.[/color]
                >
                > eh? are you sure you know what jpegtran does?
                >
                > JPEGTRAN(1)[/color]

                Whoops, sorry, right, jpegtran is for rotating the images. I meant:
                use a pipeline like

                djpeg -scale 1/4 | cjpeg

                That's how I usually do it. Main disadvantage is the scale factor has
                to be 1/2, 1/4, 1/8, etc., not arbitrary amounts like 0.3456.

                Comment

                • Damjan

                  #9
                  Re: python image thumbnail generator?

                  Chris Dewin wrote:
                  [color=blue]
                  > Hi. I run a website for my band, and the other guys want an image gallery.
                  >
                  > I'm thinking it would be nice and easy, if we could just upload a jpg into
                  > a dir called "gallery/". When the client clicks the "gallery" link, a
                  > cgi script could search the gallery/ dir, and create thumbnails of any
                  > jpeg images that don't already have a thumbnail associated with them. The
                  > script could then generate a page of clickable thumbnails.[/color]

                  Once I made an example mod_python handler, that resized images on the fly.
                  For example:
                  http://server/folder/image.jpg - would give you the original image, served
                  directly by apache without any performance hit.

                  http://server/folder/image.jpg?thumbnail - would resize the picture (and
                  cache the result on disk) and return that, on a second request it would
                  return the cached image very fast by calling an apache.send_fil e function.

                  see


                  --
                  damjan

                  Comment

                  Working...