summaryrefslogtreecommitdiff
path: root/bin/blogdims
blob: a60be0b6c25aa47f713a30992c8fac5b4a7aee36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python

# blog image aspect ratio calculator

from __future__ import division # so that dividing two integers returns a float
import sys
import os
from PIL import Image           # apt-get install python-imaging

def main():
    if len(sys.argv) < 2:
        sys.exit('%s: usage: %s IMAGE' % sys.argv[0])
    if not os.path.exists(sys.argv[1]):
        sys.exit('%s: file %s not found' % (sys.argv[0], sys.argv[1]))

    img = Image.open(sys.argv[1])
    img_width, img_height = img.size

    if img_width > img_height:
        new_width = "500"
        new_height = (500 / img_width) * img_height
        new_height = str(round(new_height, 0))[:-2]
    else:
        new_height = "500"
        new_width = (500 / img_height) * img_width
        new_width = str(round(new_width, 0))[:-2]

    print "[[!img blog/img/" + os.path.basename(sys.argv[1]) + " size=" + new_width + "x" + new_height + "]]"

if __name__ == "__main__":
    main()