+
+ @classmethod
+ def crop_to_frame(cls, wlpic, image_file):
+ img = Image.open(image_file)
+ if wlpic.frame is None:
+ return img
+ img = img.crop(itertools.chain(*wlpic.frame))
+ return img
+
+ @staticmethod
+ def add_source_note(wlpic, img):
+ from PIL import ImageDraw, ImageFont
+ from librarian import get_resource
+
+ annotated = Image.new(img.mode,
+ (img.size[0], img.size[1] + 40),
+ (255, 255, 255)
+ )
+ annotated.paste(img, (0, 0))
+ annotation = Image.new(img.mode, (3000, 120), (255, 255, 255))
+ ImageDraw.Draw(annotation).text(
+ (30, 15),
+ wlpic.picture_info.source_name,
+ (0, 0, 0),
+ font=ImageFont.truetype(get_resource("fonts/DejaVuSerif.ttf"), 75)
+ )
+ annotated.paste(annotation.resize((1000, 40), Image.ANTIALIAS), (0, img.size[1]))
+ return annotated
+
+ @classmethod
+ def picture_list(cls, filter=None):
+ """Generates a hierarchical listing of all pictures
+ Pictures are optionally filtered with a test function.
+ """
+
+ pics = cls.objects.all().order_by('sort_key')\
+ .only('title', 'slug', 'image_file')
+
+ if filter:
+ pics = pics.filter(filter).distinct()
+
+ pics_by_author = SortedDict()
+ orphans = []
+ for tag in catalogue.models.Tag.objects.filter(category='author'):
+ pics_by_author[tag] = []
+
+ for pic in pics.iterator():
+ authors = list(pic.tags.filter(category='author'))
+ if authors:
+ for author in authors:
+ pics_by_author[author].append(pic)
+ else:
+ orphans.append(pic)
+
+ return pics_by_author, orphans
+
+ @property
+ def info(self):
+ if not hasattr(self, '_info'):
+ from librarian import dcparser
+ from librarian import picture
+ info = dcparser.parse(self.xml_file.path, picture.PictureInfo)
+ self._info = info
+ return self._info
+
+ def pretty_title(self, html_links=False):
+ picture = self
+ names = [(tag.name, tag.get_absolute_url())
+ for tag in self.tags.filter(category='author')]
+ names.append((self.title, self.get_absolute_url()))
+
+ if html_links:
+ names = ['<a href="%s">%s</a>' % (tag[1], tag[0]) for tag in names]
+ else:
+ names = [tag[0] for tag in names]
+ return ', '.join(names)
+
+ def related_themes(self):
+ return catalogue.models.Tag.objects.usage_for_queryset(
+ self.areas.all(), counts=True).filter(category__in=('theme', 'thing'))
+
+ def flush_includes(self, languages=True):
+ if not languages:
+ return
+ if languages is True:
+ languages = [lc for (lc, _ln) in settings.LANGUAGES]
+ flush_ssi_includes([
+ template % (self.pk, lang)
+ for template in [
+ '/katalog/p/%d/short.%s.html',
+ ]
+ for lang in languages
+ ])