This is where we rotate images. Here's the source code...

Attach additional images (gif/bmp/jpg/png) to add your another picture to the rotation pool. Right now they rotate once an hour via a cron job. The script automatically uses ImageMagick to create thumbnails, so it's ok to upload images that look larger than the one you see here.

TODO: since I wrote this thing I now know how to execute ImageMagick directly from Perl, so I don't have to execute a shell command ( convert ) for each image I resize.

UPDATE 8-14-02: Made a number of changes. Now I output the filename, description and un-thumbnailed size to a template file. This file gets read by the default twiki template file, which in turns makes the HTML code for the header. Now, I have the capability of showing the full size version of the file as well as the description as a tooltip. Also eliminates the need to keep copying the thumbnail to the home.gif file.

#!/usr/bin/python

import os
import re
import sys
import string

PREFIX_DIR = "/home/httpd/twiki"

# directory where we store the thumbnails
THUMB_DIR = "%s/pub/Main/ImageRotater/thumbs" %PREFIX_DIR

# directory where we find the original images that get
#  thumbnailed
BASE_DIR = "%s/pub/Main/ImageRotater" %PREFIX_DIR

# The topic file itself.  Needed to snag out the description 
TOPIC_FILE = "%s/data/Main/ImageRotater.txt" %PREFIX_DIR

# the template file we write the picture description into.
#  this allows us to do fun things in the TWiki standardheader
TEMPLATE_FILE = "%s/templates/hourlypicture.tmpl" %PREFIX_DIR

# directory where the rotated image gets copied to
DEST_DIR = "%s/pub" %PREFIX_DIR

THUMB_SIZE_URL_BASE = "/twiki/pub/Main/ImageRotater/thumbs/"
FULL_SIZE_URL_BASE = "/twiki/pub/Main/ImageRotater/"

# get a list of all the files in the directory
fileList = os.listdir(BASE_DIR)

# make sure we have a thumbnail directory.  It'd be easier
#  to do an os.access(THUMB_DIR), but this makes for a good
#  example of exception handling

try:
   os.listdir(THUMB_DIR)
except OSError, (errno, strerror):
   if errno == 2:
      print "Creating thumbnail directory %s" %THUMB_DIR
      os.makedirs(THUMB_DIR)
   else:
      print "Unknown error accessing thumbnail directory: '%s'" %strerror

imageList = []
imageDict = {}

# sort out any non-image files.  Note we have to carefully ignore
#  files that end in ',v' as TWiki sticks these here when you upload
#
# We then sort the images in alphabetical order
#
# If there are no images to rotate we simply exit with
#  an error
fileMatcher = re.compile('.*\.(jpg|gif|bmp|png)$', re.IGNORECASE)

for thisOne in fileList:
   if fileMatcher.match(thisOne):
      imageList.append(thisOne)

if not imageList:
   print "No images to rotate!"
   sys.exit(1)

imageList.sort()


# Walk through our images.  Create thumbnails for files that
#  don't have them already.  We put the index of the image
#  as the value in the dictionary so that we can figure out the
#  next image later
indexNow = 0
for thisOne in imageList:
   origFile = "%s/%s" %(BASE_DIR, thisOne)
   thumbFile = "%s/%s" %(THUMB_DIR, thisOne)
   if not os.access(thumbFile, os.F_OK):
      print "generating thumbnail for %s" %thisOne
      os.system("/usr/local/bin/convert -scale '96x96>' +profile \"*\" %s %s" %  (origFile, thumbFile))
   imageDict[thisOne] = indexNow   
   indexNow = indexNow + 1

hasCurrentFile = 0

# open the 'currentRotatedImage' file to see what image
#  we displayed last.  If the file doesn't exist we
#  create a new one
try:
   f = open(BASE_DIR + '/currentRotatedImage')
   hasCurrentFile = 1
   readInFileName = string.strip(f.read())
   print "current file: " + readInFileName
   f.close()

except IOError, msg:
   print "creating new \'currentRotateImage\' file"

# try open the 'currentRotatedImage' file for writing
try:
   f = open(BASE_DIR + '/currentRotatedImage', 'w')

except IOError, msg:
   print "can't create new \'currentRotateImage\' file"
   sys.exit(1)

newIndex = 0

# figure out the next image to display.  If we're at the
#  end of the list, or if we have no previous image, we
#  just display the first one

if hasCurrentFile:
   if imageDict.has_key(readInFileName):
      oldIndex = int(imageDict[readInFileName])
      if oldIndex < len(imageList) - 1:
         newIndex = oldIndex + 1   

newFileName = imageList[newIndex]

print "new file: " + newFileName

# if we can't get a description, we use the filename itself
#  as the description

description = newFileName
# try and get the comment from TWiki
try:
   ftopic = open(TOPIC_FILE, 'r')

# note, need a greedy search (.*?)
   commentMatcher = re.compile('.*META:FILEATTACHMENT\{name=\"%s\".*comment=\"(.*?)\".*' %newFileName, re.IGNORECASE)

   done = 0
   while not done:
      thisLine = ftopic.readline()
      if thisLine:
         matchResult = commentMatcher.match(thisLine)
         if matchResult:
            if string.strip(matchResult.group(1)):
               description = matchResult.group(1)
      else:
         done = 1
         ftopic.close()

except IOError, msg:
   print "can't open topic file to get description"

print "Using description %s" %description


try:
   ftemplate = open(TEMPLATE_FILE, 'w')
   ftemplate.write("%%TMPL:DEF{\"hourlypicturedescription\"}%%Picture of the hour:%s%%TMPL:END%%\n" % description)
   ftemplate.write("%%TMPL:DEF{\"hourlypicturethumbnailfilename\"}%%%s%s%%TMPL:END%%\n" %(THUMB_SIZE_URL_BASE, newFileName ))
   ftemplate.write("%%TMPL:DEF{\"hourlypicturefilename\"}%%%s%s%%TMPL:END%%\n" %(FULL_SIZE_URL_BASE, newFileName ))
   ftemplate.close()

except IOError, msg:
   print "can't output template file!"


# write out the 'currentRotatedImage' file for next time
f.write(newFileName)
f.close()


Spirit76</A dick.gif colorfulChristmas.jpg newAbCorner.jpg
Topic attachments
I Attachment Action Size Date Who Comment
JPEGjpg DanSandin_small.jpg manage 124.9 K 30 Jun 2003 - 20:26 MattWalsh  
JPEGjpg LIS_smithRobot.jpg manage 47.3 K 04 Jan 2002 - 04:47 MattWalsh  
JPEGjpg SmallAbby.jpg manage 33.9 K 03 Jan 2004 - 21:00 MattWalsh  
JPEGjpg alcatrazIndustrialArea.jpg manage 32.8 K 02 Jan 2002 - 23:08 MattWalsh  
JPEGjpg newAbCorner.jpg manage 38.7 K 05 May 2004 - 19:26 MattWalsh  
JPEGjpg pinballCloseup.jpg manage 79.7 K 02 Jan 2002 - 23:09 MattWalsh  
JPEGjpg r2d2.jpg manage 29.3 K 02 Jan 2002 - 23:13 MattWalsh Artoo!
JPEGjpg shdecolg2.jpg manage 92.7 K 04 Jan 2002 - 04:53 MattWalsh  
GIFgif usflag300.gif manage 2.2 K 05 Apr 2003 - 00:04 MattWalsh  
GIFgif vader.gif manage 4.1 K 02 Jan 2002 - 23:14 MattWalsh  
GIFgif writtenInVi.gif manage 5.7 K 19 Dec 2001 - 18:59 MattWalsh  
Topic revision: r25 - 05 May 2004 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback