From 40d97b5f6b6880cdf6ba7698504ce4c702a516ae Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 4 Jan 2011 13:02:32 +0530 Subject: [PATCH] icon --- pandora/item/models.py | 27 ++++++++++++++++++++++++ pandora/item/urls.py | 2 ++ pandora/item/views.py | 43 +++++++++++++++++++++++++++++---------- pandora/scripts/item_icon | 4 ++-- 4 files changed, 63 insertions(+), 13 deletions(-) diff --git a/pandora/item/models.py b/pandora/item/models.py index 0acdc0397..fe1e18b00 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -236,6 +236,9 @@ class Item(models.Model): poster_height = models.IntegerField(default=0) poster_width = models.IntegerField(default=0) poster_frame = models.FloatField(default=-1) + + icon = models.ImageField(default=None, blank=True, upload_to=lambda i, x: i.path("icon.jpg")) + #stream related fields stream_aspect = models.FloatField(default=4/3) @@ -871,6 +874,30 @@ class Item(models.Model): p.wait() return posters.keys() + def make_icon(self): + #FIXME: should take middle or selected frame or som + frames = [] + for f in self.main_videos(): + for ff in f.frames.all(): + frames.append(ff.frame.path) + if frames: + icon = self.path('icon.jpg') + self.icon.name = icon + frame = frames[int(len(frames)/2)] + timeline = self.path('timeline.64.png') + timeline = os.path.abspath(os.path.join(settings.MEDIA_ROOT, timeline)) + if os.path.exists(timeline): + cmd = [settings.ITEM_ICON, + '-f', frame, + '-l', timeline, + '-i', self.icon.path + ] + p = subprocess.Popen(cmd) + p.wait() + self.save() + return icon + return None + Item.facet_keys = [] Item.person_keys = [] for key in site_config['findKeys']: diff --git a/pandora/item/urls.py b/pandora/item/urls.py index 458d29e0c..bd8276866 100644 --- a/pandora/item/urls.py +++ b/pandora/item/urls.py @@ -11,5 +11,7 @@ urlpatterns = patterns("item.views", (r'^(?P[A-Z0-9].*)/poster\.(?P\d+)\.jpg$', 'poster'), (r'^(?P[A-Z0-9].*)/poster\.(?Plarge)\.jpg$', 'poster'), (r'^(?P[A-Z0-9].*)/poster\.jpg$', 'poster'), + (r'^(?P[A-Z0-9].*)/icon\.(?P\d+)\.jpg$', 'icon'), + (r'^(?P[A-Z0-9].*)/icon\.jpg$', 'icon'), (r'^(?P[A-Z0-9].*)/timelines/(?P.+)\.(?P\d+)\.(?P\d+)\.png$', 'timeline'), ) diff --git a/pandora/item/views.py b/pandora/item/views.py index 1cc98b245..29521fbfc 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -57,6 +57,7 @@ def _order_query(qs, sort, prefix='sort__'): qs = qs.order_by(*order_by) return qs + def _parse_query(data, user): query = {} query['range'] = [0, 100] @@ -68,6 +69,7 @@ def _parse_query(data, user): #group by only allows sorting by name or number of itmes return query + def _get_positions(ids, get_ids): positions = {} for i in get_ids: @@ -77,6 +79,7 @@ def _get_positions(ids, get_ids): pass return positions + def find(request): ''' param data { @@ -222,6 +225,7 @@ Positions actions.register(find) + def autocomplete(request): ''' param data @@ -265,6 +269,7 @@ def autocomplete(request): return render_to_json_response(response) actions.register(autocomplete) + def getItem(request): ''' param data @@ -283,6 +288,7 @@ def getItem(request): return render_to_json_response(response) actions.register(getItem) + @login_required_json def editItem(request): ''' @@ -434,21 +440,27 @@ def frame(request, id, position, size): raise Http404 return HttpFileResponse(frame, content_type='image/jpeg') + +def image_to_response(item, image, size=None): + if size: + size = int(size) + path = image.path.replace('.jpg', '.%d.jpg'%size) + if not os.path.exists(path): + image_size = max(image.width, image.height) + if size > image_size: + return redirect('/%s/icon.jpg' % item.itemId) + extract.resize_image(image.path, path, size=size) + else: + path = image.path + return HttpFileResponse(path, content_type='image/jpeg') + + def poster(request, id, size=None): item = get_object_or_404(models.Item, itemId=id) if size == 'large': size = None if item.poster: - if size: - size = int(size) - poster_path = item.poster.path.replace('.jpg', '.%d.jpg'%size) - if not os.path.exists(poster_path): - poster_size = max(item.poster.width, item.poster.height) - if size > poster_size: - return redirect('/%s/poster.jpg' % item.itemId) - extract.resize_image(item.poster.path, poster_path, size=size) - else: - poster_path = item.poster.path + return image_to_response(item, item.poster, size) else: if not size: size='large' @@ -456,6 +468,15 @@ def poster(request, id, size=None): poster_path = os.path.join(settings.STATIC_ROOT, 'png/posterDark.48.png') return HttpFileResponse(poster_path, content_type='image/jpeg') + +def icon(request, id, size=None): + item = get_object_or_404(models.Item, itemId=id) + if item.icon: + return image_to_response(item, item.icon, size) + else: + raise Http404 + + def timeline(request, id, timeline, size, position): item = get_object_or_404(models.Item, itemId=id) if timeline == 'strip': @@ -464,6 +485,7 @@ def timeline(request, id, timeline, size, position): timeline = '%s.%s.%04d.png' %(item.timeline_prefix, size, int(position)) return HttpFileResponse(timeline, content_type='image/png') + def video(request, id, profile): item = get_object_or_404(models.Item, itemId=id) stream = get_object_or_404(item.streams, profile=profile) @@ -497,4 +519,3 @@ def video(request, id, profile): response['Content-Disposition'] = 'attachment; filename="%s"' % filename return response return HttpFileResponse(path, content_type=content_type) - diff --git a/pandora/scripts/item_icon b/pandora/scripts/item_icon index e3a8387bc..49aaf7f3a 100755 --- a/pandora/scripts/item_icon +++ b/pandora/scripts/item_icon @@ -40,7 +40,7 @@ def render_icon(frame, timeline, icon): timeline_image = Image.open(timeline) timeline_image = timeline_image.resize((icon_width, 64), Image.ANTIALIAS) icon_image.paste(timeline_image, (0, icon_height - 64)) - mask_image = Image.open(ox.path.join(static_root, 'png', 'icon.mask.png')) + mask_image = Image.open(os.path.join(static_root, 'png', 'icon.mask.png')) mask_image = mask_image.resize((icon_width, icon_height)) icon_image.putalpha(mask_image) icon_image.save(icon) @@ -54,7 +54,7 @@ def main(): if options.icon == None: parser.print_help() sys.exit() - render_icon(opt.frame, opt.timeline, opt.icon) + render_icon(options.frame, options.timeline, options.icon) if __name__ == "__main__": main()