Django - python cut image after checking original Width
-
08-12-2019 - |
Question
I'm using sorl-thumbnail and PIL for my django site. How do I cut image when certain conditions meet? For example, create thumbnail of width 600px only when the original image width is greater than 600px.
{% thumbnail img.image "600" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" alt="{{ object.name }}" />
{% endthumbnail %}
Solution
If I understand you, you want the image to simply be 600px or less, i.e. you don't want sorl-thumbnail to stretch it to be 600px always.
If that's the case, you just need to add upscale=False
:
{% thumbnail img.image "600" upscale=False as im %}
<img src="{{ im.url }}" width="{{ im.width }}" alt="{{ object.name }}" />
{% endthumbnail %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow