How to create a collage from Image urls in Python - python

I have a list of image urls (.jpg). I need to create a collage of all these images and I am looking for a python library that can do it.
I have explored the following libraries in depth but with no results:
1) https://pypi.python.org/pypi/collage/ - The best of the lot. Unfortunately, it takes a generic argument( for example, "The Avengers", "Mark Twain"), Google searches for images(need a token. Has limitations), and then outputs an Image collage. Will take a couple of days to fork the app into a library
2) https://github.com/fwenzel/collage - Creates an unattractive horizontal collage. Again need some forking
3) https://github.com/adrienverge/PhotoCollage - Application. No good.
Are there any libraries that directly take urls, download them and make a collage, or make a collage out of PIL image objects? I have checked out pretty much every library that Google search and SO has to show. I am desperately hoping there is some obscure library out there that can help my cause.
Thanks.

There is a Django package for this, very similar to your first option but with Django integration:
Django-Collage
If you have a set of images in your template, you can use it's filter to renderize them with:
{% load collage %}
{% load cache %}
{% cache 31536000 images view.id %}
{% get_collage for view.get_images as images %}
<div>
{% for image in images %}
<a href="{{ image.src }}" style="{{ image.css }}">
<img src="{{ image.url }}">
</a>
{% endfor %}
</div>
{% endcache %}
This code is taken from the tutorial provided in the package. I recommend you to check the official tutorial to learn how to use it.

Related

I cannot get images to work on my django project(this is my first one and im having trouble)

I have an img folder in the same directory as my html files. I want to put images onto my site but I am doing something wrong.
this is the code:
{% block a %}
<div class="row">
<div class="col-sm-4 center">
<img src="img/img01.jpeg">
</div>
</div>
{% endblock %}
this is what happens when I run the local server.
this is my file directory
Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”.
Those files need to be handled another way as your templates. The documentation can be found here.
It comes basically down to defining a seperate static directory inside your app. It is really good explained in the documentation.

django-wiki: how to list all articles under root article

I am a newbie in Django and am currently building a site. I have downloaded django-wiki ver 0.4a3 to have a wiki app for my site. Everything works fine, out-of-the-box. But instead of showing the root article as the main page, how can I show a list of all articles under the root article, so users can just click on any child article to open up that article? At the moment, users have to click a menu option to browse up one level or to browse at the current level to have a listing of all articles at that given level. I find this rather tedious. I rather have an "Windows Explorer tree-and-branch-like" navigation, e.g.,
root
|_child 1
| |_child 1.1
| |_child 1.2
|
|_child 2
| |_child 2.1
| |_child 2.1.1
|
|_child 3
Please note I am asking how to get a list all of articles under the root article, not how to create a template to implement the tree-and-branch articles navigator. Once I know how to get a listing of all articles, I think I can implement the necessary HTML and CSS to have that kind of navigator. I appreciate any pointers.
P.S. I have previously tried the official django-wiki google support group, but I think the support there is dead and buried. My two questions there are neither answered, let alone read (I only get 1 view -- which is actually my view count).
Chris.
[Solved.]
Make a copy of article.html originally located at wiki/templates/wiki/ and place this copy in your own project's templates/wiki/ directory. At the place where you want to place the tree directory, add something like the following lines:
<!-- article.html -->
...
<h4>List of articles</h4>
<ul>
<!-- Need to get the first article (root) -->
{% url 'wiki:get' path=urlpath.root.path as rootpath %}
<li>
<a href="{{ rootpath }}">
{{ urlpath.root.article.current_revision.title }}
</a>
</li>
<!-- Now get all the descendent articles of the root -->
{% for child in urlpath.root.get_descendants %}
<!-- Don't list articles marked for deletion -->
{% if not child.is_deleted %}
{% with ''|center:child.level as range %}
{% for _ in range %}<ul>{% endfor %}
{% url 'wiki:get' path=child.path as childpath %}
<li>
<a href="{{ childpath }}">
{{ child.article.current_revision.title }}
</a>
</li>
{% for _ in range %}</ul>{% endfor %}
{% endwith %}
{% endif %}
{% endfor %}
</ul>
You can fancy up the code above. I am new in Django (and to using django-wiki), so this might not be the cleanest or most efficient way, but the above works as expected for me.
Advantage:
The list of articles are always updated, so any deleted articles are removed and inserted new articles are shown.
The tree-like hierarchy visually shows all the available articles and their relationships with one another. This visual depiction is far, far better than the default's which is hard to even know how many articles exist or how far deep articles are nested.
Disadvantage:
According to the django-wiki's source code, get_descendents method is an expensive call, but for my small site, I have not noticed any penalty hits (so far).
Assuming your structure is no more than 10 deep:
[article_list depth:10]
Put this in your root article.

Pelican Archives Not Displaying

How do you enable an archives tab on a pelican powered blog?
I see from the docs that it is a direct template by default, but it isn't showing up on my blog. Is there some additional field to enable it? I couldn't find any mention of it in the docs or tutorials, so I'm assuming I've missed something obvious.
Not sure if you ever resolved this; I was having the same issue as you with pelican's bootstrap3 theme. For some reason, setting a value for YEAR_ARCHIVE_SAVE_AS was not working.
After looking at the theme's base.html file, I got it working by adding the following to pelicanconf.py:
ARCHIVES_SAVE_AS = 'archives.html'
Lines 156-158 from my base.html file:
{% if ARCHIVES_SAVE_AS %}
<li><i class="fa fa-th-list"></i><span class="icon-label">{{ _('Archives') }}</span></li>
{% endif %}

Flask and Jinja for loop skip if file contains errors

I'm having a issue where I am trying to display some thumbs. The problem is that when I run the jinja2 with the variable it displays the alt text, I would rather have it skip or pass if the thumb contains errors instead of displaying alt text.
Here is code
{% block content %}
{% if games %}
{% for g in games if g.game_thumb %}
<img src="static{{ g.game_thumb }}" class="img-rounded" alt="{{ g.game_name }}" width="150" height="150">
{%endfor%}
{% endif %}
{% endblock %}
I ended up solving this via the flask route via.
from PIL import Image
try:
Image.open(thumb).verify()
print "image"
except:
print "failed"
continue
Another way and propably more efficient, would be to use image preloading. Pass the src list to javascript through a data attribute or render to a javascript variable. Using javascript you can preload the images and discard the ones that do not load.
You can also simply handle onerror on img which is called when it fails to load, and hide/remove the images.

load static file with variable name in django

I'm trying to do load the following static file
img file
where image is in a for loop of images derived from a database.
But I simply got an error Could not parse the remainder: '{{' from ''static/matrices/'{{'
What should I do to fix this? I can't use relative paths because this will be used by subsections as well, with the same html template.
You should pass a full string to the static tag from staticfiles. This is so it can use your staticstorages to find your file.
{% load staticfiles %}
{% with 'images/'|add:image.title|add:'.png' as image_static %}
{% static image_static %}
{% endwith %}
But in your use case it might be better if you just store the path of the images on the image model itself.
I got this to work by using an empty string for the static path and then using my variables in their own section, like this:
<a href= "{% static "" %}{{obj.a}}/{{obj.b}}/{{obj.c}}.gz" >Name</a>
You can use get_static_prefix template tag. get_static_prefix is a variable that contains the path specified in your STATIC_URL. Your code would be:
{% load static %}
img file
or
{% load static %}
{% get_static_prefix as STATIC_PREFIX %}
img file
Reference: get_static_prefix
You should avoid nesting tags.
What are you trying to solve? Isn't the image part of dynamic content? The static tag is for static content not uploaded media files.
If you must use the static tag the correct way would be something in the order of;
{% static image %} or {% static image.file %}
Depending on the object layout. If you're using an ImageField (inherits FileField) the image object already holds the path, so there's no need to manually add extensions.
What I ended up doing was to just split the path from the name itself. Those images represents tabs, are static and in a sequence. They are not connected to the database imagewise.
I didn´t want to repeat the html for every run so I ended up doing a forloop like this, a bit simplified.
{% for option in options_obj %}
<img class="highlight" src="{% static "store/img/editor/tab-" %}
{{ option.name.lower }}-selected.png">
{% endfor %}
EDIT:
Even though this does work in most situations it can really mess stuff up as well. For me this occured when having different images for different language settings and at the same time using tools like CachedStaticFilesStorage. If you need to add language code or something else to your image this is a more solid solution
{% for option in options_obj %}
<img class="highlight" src="{% static "store/img/editor/tab-"
|add:LANGUAGE_CODE|add:"-selected.png" %}">
{% endfor %}
Too many quotes!
Just
img file
and you don't have to call the static in the link because you already load the static

Categories

Resources