Merge branch 'master' of git@stigma:platforma
authorŁukasz Rekucki <lrekucki@gmail.com>
Mon, 31 Aug 2009 14:23:50 +0000 (16:23 +0200)
committerŁukasz Rekucki <lrekucki@gmail.com>
Mon, 31 Aug 2009 14:23:50 +0000 (16:23 +0200)
README.txt
apps/explorer/forms.py
imgconv.py [new file with mode: 0755]
project/templates/explorer/panels/gallery.html

index cb42e71..8caa441 100644 (file)
@@ -4,7 +4,8 @@ Zależności
  * [Django 1.1](http://djangoproject.com/)
  * [Mercurial 1.3.1](http://www.selenic.com/mercurial/)
  * [librarian 1.1](http://redmine.nowoczesnapolska.org.pl/projects/show/librarian)
-
+ * [PIL 1.1.6](http://www.pythonware.com/products/pil/) (wymagane przez skrypt imgconv.py)
+ * [pngnq](http://pngnq.sourceforge.net/) (wymagane przez skrypt imgconv.py)
 
 Moduły
 ======
index 81e9f26..aa6b48a 100644 (file)
@@ -64,5 +64,5 @@ class DublinCoreForm(forms.Form):
         old_rdf = doc.getroottree().find('//{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF')
         old_rdf.getparent().remove(old_rdf)
         doc.insert(0, rdf)
-        repository.add_file(path, unicode(etree.tostring(doc), 'utf-8'))
+        repository.add_file(path, etree.tostring(doc, pretty_print=True, encoding=unicode))
 
diff --git a/imgconv.py b/imgconv.py
new file mode 100755 (executable)
index 0000000..e00b9c8
--- /dev/null
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+import sys
+import os
+from os.path import splitext, dirname
+from PIL import Image, ImageFilter, ImageEnhance, ImageOps
+
+
+def resize(image, max_width, max_height):
+    """Resize image so it's not wider than max_width and not higher than max_height."""
+    width, height = image.size
+    ratio = max(1.0, float(width) / max_width, float(height) / max_height)
+    new_width, new_height = int(width / ratio), int(height / ratio)
+    return image.resize((new_width, new_height), Image.ANTIALIAS)
+
+
+def crop(image, ratio, from_right=False):
+    """Crop image to ratio of current width."""
+    width, height = image.size
+    new_width = width * ratio
+    if from_right:
+        bounds = (int(width - new_width), 0, int(width), int(height))
+    else:
+        bounds = (0, 0, int(new_width), int(height))
+    image = image.crop(bounds)
+    image.load()
+    return image
+
+
+def ratio(image):
+    """Return width to height ratio of image."""
+    width, height = image.size
+    return float(width) / height
+    
+    
+for file_name in sys.argv[1:]:
+    try:
+        os.mkdir('output')
+    except:
+        pass
+    base_name, ext = splitext(file_name)
+    try:
+        image = Image.open(file_name)
+    except IOError, e:
+        sys.stderr.write('\nerror:%s:%s\n' % (file_name, e.message))
+        continue
+    
+    # Check ratio
+    if ratio(image) > 1:
+        images = [crop(image, 0.5), crop(image, 0.5, True)]
+    else:
+        images = [image]
+    
+    for i, image in enumerate(images):
+        image = image.filter(ImageFilter.SHARPEN)
+        
+        image = image.filter(ImageFilter.MinFilter)
+        def convert(i):
+            if i > 48:
+                return 255
+            return i
+        image = image.point(convert)
+        image = ImageOps.autocontrast(image, cutoff=95)
+        image = image.convert('L')
+        image = image.filter(ImageFilter.SHARPEN)
+        
+        # Save files
+        small_image = resize(image, 480, 720)
+        small_image_file_name = '%s.small.%d.png' % (base_name, i)
+        small_image.save(small_image_file_name, optimize=True, bits=6)
+        os.system('pngnq -n 8 -e .png -d output -f "%s"' % small_image_file_name)
+        os.remove(small_image_file_name)
+        
+        big_image = resize(image, 960, 1440)
+        big_image_file_name = '%s.big.%d.png' % (base_name, i)
+        big_image.save(big_image_file_name, optimize=True, bits=6)
+        os.system('pngnq -n 8 -e .png -d output -f "%s"' % big_image_file_name)
+        os.remove(big_image_file_name)
+        
+    sys.stderr.write('.')
index 30c1c83..af3a5cb 100644 (file)
@@ -1,30 +1,28 @@
 <div class="toolbar" style="height: 24px">
-       <select name="folders" class="id_folders">
-               <option value="" selected="selected">-- Wybierz folder z obrazkami --</option>
-        <option value="andersen_basnie">andersen_basnie</option>
-       </select>
+    {{ form.folders }}
 </div>
 
 <div class="images-wrap" style="position: absolute; top: 24px; left:0px; right:0px; bottom: 0px;">
-       <div class="images"><!-- To jest div na obrazki, które są wstawiane później --></div>
+    <div class="images"><!-- To jest div na obrazki, które są wstawiane później --></div>
 </div>
 <script type="text/javascript" charset="utf-8">
 
 panel_hooks = {
-       load: function() {
-               var contentDiv = this.contentDiv;
-               $('.id_folders', contentDiv).change(function() {
-               $('.images', contentDiv).fadeOut('slow', function() { 
-                   $(this).html('').load('{% url folder_image_ajax %}' + $('.id_folders', contentDiv).val() + '/',  function() {
+    load: function() {
+        var contentDiv = this.contentDiv;
+        $('select[name=folders]', contentDiv).change(function() {
+            var select = this;
+            $('.images', contentDiv).fadeOut('slow', function() { 
+                $(this).html('').load('{% url folder_image_ajax %}' + $(select).val() + '/',  function() {
                     $('.images-wrap', contentDiv).data('lazyload:lastCheckedScrollTop', -10000);
                 });
-                   })
-               });
-               
-               $('.images-wrap', contentDiv).lazyload('.image-box', 
-                       {threshold: 640 * 10, scrollTreshold: 640 * 5}
-               );
-       },
+            })
+        });
+        
+        $('.images-wrap', contentDiv).lazyload('.image-box', 
+            {threshold: 640 * 10, scrollTreshold: 640 * 5}
+        );
+    },
     refresh: function() {
         return true; // gallery is always fresh
     },