# HG changeset patch # User souliane # Date 1403202822 -7200 # Node ID 5d89fecdf6674cb97b2ca421ec280f3743f58b50 # Parent f25ec9fd7cc4dc697af8c08de53dd3653296ec0f plugin XEP-0054: crop uploaded avatar to get a square diff -r f25ec9fd7cc4 -r 5d89fecdf667 src/plugins/plugin_xep_0054.py --- a/src/plugins/plugin_xep_0054.py Thu Jun 19 19:20:52 2014 +0200 +++ b/src/plugins/plugin_xep_0054.py Thu Jun 19 20:33:42 2014 +0200 @@ -46,6 +46,7 @@ from wokkel.subprotocols import XMPPHandler AVATAR_PATH = "avatars" +AVATAR_DIM = (64, 64) IQ_GET = '/iq[@type="get"]' NS_VCARD = 'vcard-temp' @@ -242,8 +243,19 @@ except IOError: return Failure(exceptions.DataError("Can't open image")) - if img.size != (64, 64): - img = img.resize((64, 64)) + if img.size != AVATAR_DIM: + img.thumbnail(AVATAR_DIM, Image.ANTIALIAS) + if img.size[0] != img.size[1]: # we need to crop first + left, upper = (0, 0) + right, lower = img.size + offset = abs(right - lower) / 2 + if right == min(img.size): + upper += offset + lower -= offset + else: + left += offset + right -= offset + img = img.crop((left, upper, right, lower)) img_buf = StringIO() img.save(img_buf, 'PNG')