<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: html code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 06 Oct 2008 12:34:17 GMT</pubDate>
    <description>DZone Snippets: html code</description>
    <item>
      <title>[Multiplatform] Thumbnails of all (gif, jpeg, and png) images in a directory</title>
      <link>http://snippets.dzone.com/posts/show/1297</link>
      <description>Thanks Dorrin for your Thumbnailer!&lt;br /&gt;I hope you don't mind, but I did some minor fixes and made your script more portable. Now, you can use it on Windows too.&lt;br /&gt;By the way, as I revealed there is no dependency of "from mod_python import apache", so I removed it.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import os&lt;br /&gt;import re&lt;br /&gt;from PIL import Image&lt;br /&gt;&lt;br /&gt;header = '''&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;&lt;br /&gt;&lt;html&gt;&lt;br /&gt;&lt;head&gt;&lt;title&gt;Directory Listing&lt;/title&gt;&lt;/head&gt;&lt;br /&gt;&lt;body&gt;'''&lt;br /&gt;images_per_row = 5&lt;br /&gt;max_width,max_height = 150,150&lt;br /&gt;&lt;br /&gt;path = os.path.dirname(__file__)&lt;br /&gt;  &lt;br /&gt;def thumbnail(filename):&lt;br /&gt;  thumbsdir = os.path.join(path, ".thumbnails")&lt;br /&gt;  thumbfile = os.path.join(thumbsdir, filename)&lt;br /&gt;  if os.path.exists(thumbfile):&lt;br /&gt;    return&lt;br /&gt;&lt;br /&gt;  if not os.path.exists(thumbsdir):&lt;br /&gt;    os.mkdir(thumbsdir);&lt;br /&gt;    &lt;br /&gt;  filepath = os.path.join(path, filename)&lt;br /&gt;  image = Image.open(filepath)&lt;br /&gt;  image.thumbnail((max_width, max_height), Image.ANTIALIAS)&lt;br /&gt;  image.save(thumbfile)&lt;br /&gt;&lt;br /&gt;def index(req):&lt;br /&gt;  return_value = header&lt;br /&gt;&lt;br /&gt;  dir_list = os.listdir(path)&lt;br /&gt;  image_list = []&lt;br /&gt;&lt;br /&gt;  search = re.compile("(.*\.[Jj][Pp][Ee]?[Gg]$)|(.*\.[Pp][Nn][Gg]$)|(.*\.[Gg][Ii][Ff]$)")&lt;br /&gt;&lt;br /&gt;  image_count = 0&lt;br /&gt;  for file in dir_list:&lt;br /&gt;    if search.match(file):&lt;br /&gt;      image_count += 1&lt;br /&gt;      image_list.append(file)&lt;br /&gt;&lt;br /&gt;  if image_count &gt; 0:&lt;br /&gt;    image_list.sort()&lt;br /&gt;    num_rows = image_count / images_per_row&lt;br /&gt;&lt;br /&gt;    image_num = 0&lt;br /&gt;    return_value += '&lt;table width="100%" border="0"&gt;&lt;tr&gt;\n'&lt;br /&gt;&lt;br /&gt;    for image in image_list:&lt;br /&gt;      thumbnail(image)&lt;br /&gt;      image_num += 1&lt;br /&gt;      return_value += '&lt;td&gt;&lt;a href="' + image + '"&gt;&lt;img src=".thumbnails/' + image + '" alt="' + image + '"&gt;&lt;/a&gt;'&lt;br /&gt;&lt;br /&gt;      if image_num % images_per_row:&lt;br /&gt;        return_value += '&lt;/td&gt;\n'&lt;br /&gt;&lt;br /&gt;      else:&lt;br /&gt;        return_value += '&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;\n'&lt;br /&gt;&lt;br /&gt;  return_value += "&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;"&lt;br /&gt;  return return_value&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here is small test script.&lt;br /&gt;It generates thumbnails with Dorrin's script, then grabs HTML output and writes it to index.html file, so you get nice index page.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import thumbnail&lt;br /&gt;import os&lt;br /&gt;&lt;br /&gt;html = thumbnail.index(0)&lt;br /&gt;&lt;br /&gt;htmlfile = os.path.join(os.getcwd(), "index.html")&lt;br /&gt;f = open(htmlfile, "w")&lt;br /&gt;f.write(html);&lt;br /&gt;f.close()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 25 Jan 2006 18:21:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1297</guid>
      <author>mloskot (Mateusz &#197;?oskot)</author>
    </item>
  </channel>
</rss>
