2018-05-16 13:31:06 +02:00
|
|
|
# pylint: disable=too-many-locals,too-many-arguments,too-many-function-args
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
2019-10-11 13:14:08 +02:00
|
|
|
Usage
|
|
|
|
-----
|
2013-08-27 11:47:14 +02:00
|
|
|
|
2013-09-16 21:08:55 +02:00
|
|
|
>>> import qtidenticon
|
|
|
|
>>> qtidenticon.render_identicon(code, size)
|
2013-08-27 11:47:14 +02:00
|
|
|
|
|
|
|
Return a PIL Image class instance which have generated identicon image.
|
2019-10-11 13:14:08 +02:00
|
|
|
``size`` specifies `patch size`. Generated image size is 3 * ``size``.
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
|
|
|
|
2018-05-16 13:31:06 +02:00
|
|
|
from PyQt4 import QtGui
|
2020-01-24 15:03:13 +01:00
|
|
|
from PyQt4.QtCore import QPointF, QSize, Qt
|
|
|
|
from PyQt4.QtGui import QPainter, QPixmap, QPolygonF
|
2013-08-27 11:47:14 +02:00
|
|
|
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
class IdenticonRendererBase(object):
|
2018-05-16 13:31:06 +02:00
|
|
|
"""Encapsulate methods around rendering identicons"""
|
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
PATH_SET = []
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
def __init__(self, code):
|
|
|
|
"""
|
2019-10-11 13:14:08 +02:00
|
|
|
:param code: code for icon
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
|
|
|
if not isinstance(code, int):
|
|
|
|
code = int(code)
|
|
|
|
self.code = code
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-18 17:39:45 +02:00
|
|
|
def render(self, size, twoColor, opacity, penwidth):
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
2013-09-18 17:39:45 +02:00
|
|
|
render identicon to QPicture
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2019-10-11 13:14:08 +02:00
|
|
|
:param size: identicon patchsize. (image size is 3 * [size])
|
|
|
|
:returns: :class:`QPicture`
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
# decode the code
|
2013-09-17 10:55:26 +02:00
|
|
|
middle, corner, side, foreColor, secondColor, swap_cross = self.decode(self.code, twoColor)
|
2013-08-27 11:47:14 +02:00
|
|
|
|
2013-09-16 21:08:55 +02:00
|
|
|
# make image
|
2018-05-16 13:31:06 +02:00
|
|
|
image = QPixmap(QSize(size * 3 + penwidth, size * 3 + penwidth))
|
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
# fill background
|
2018-05-16 13:31:06 +02:00
|
|
|
backColor = QtGui.QColor(255, 255, 255, opacity)
|
2013-09-17 10:55:26 +02:00
|
|
|
image.fill(backColor)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
kwds = {
|
2013-09-16 21:08:55 +02:00
|
|
|
'image': image,
|
2013-08-27 11:47:14 +02:00
|
|
|
'size': size,
|
2013-09-17 10:55:26 +02:00
|
|
|
'foreColor': foreColor if swap_cross else secondColor,
|
|
|
|
'penwidth': penwidth,
|
2013-08-27 11:47:14 +02:00
|
|
|
'backColor': backColor}
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
# middle patch
|
2013-09-16 21:08:55 +02:00
|
|
|
image = self.drawPatchQt((1, 1), middle[2], middle[1], middle[0], **kwds)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
# side patch
|
2013-09-17 10:55:26 +02:00
|
|
|
kwds['foreColor'] = foreColor
|
2018-05-16 13:31:06 +02:00
|
|
|
kwds['patch_type'] = side[0]
|
2013-08-27 11:47:14 +02:00
|
|
|
for i in xrange(4):
|
|
|
|
pos = [(1, 0), (2, 1), (1, 2), (0, 1)][i]
|
2013-09-16 21:08:55 +02:00
|
|
|
image = self.drawPatchQt(pos, side[2] + 1 + i, side[1], **kwds)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
# corner patch
|
2013-09-17 10:55:26 +02:00
|
|
|
kwds['foreColor'] = secondColor
|
2018-05-16 13:31:06 +02:00
|
|
|
kwds['patch_type'] = corner[0]
|
2013-08-27 11:47:14 +02:00
|
|
|
for i in xrange(4):
|
|
|
|
pos = [(0, 0), (2, 0), (2, 2), (0, 2)][i]
|
2013-09-16 21:08:55 +02:00
|
|
|
image = self.drawPatchQt(pos, corner[2] + 1 + i, corner[1], **kwds)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
return image
|
2013-09-16 21:08:55 +02:00
|
|
|
|
2018-05-16 13:31:06 +02:00
|
|
|
def drawPatchQt(self, pos, turn, invert, patch_type, image, size, foreColor,
|
|
|
|
backColor, penwidth): # pylint: disable=unused-argument
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
2019-10-11 13:14:08 +02:00
|
|
|
:param size: patch size
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
2018-05-16 13:31:06 +02:00
|
|
|
path = self.PATH_SET[patch_type]
|
2013-08-27 11:47:14 +02:00
|
|
|
if not path:
|
|
|
|
# blank patch
|
|
|
|
invert = not invert
|
|
|
|
path = [(0., 0.), (1., 0.), (1., 1.), (0., 1.), (0., 0.)]
|
2013-09-16 21:08:55 +02:00
|
|
|
|
2018-05-16 13:31:06 +02:00
|
|
|
polygon = QPolygonF([QPointF(x * size, y * size) for x, y in path])
|
|
|
|
|
2013-09-16 21:08:55 +02:00
|
|
|
rot = turn % 4
|
2018-05-16 13:31:06 +02:00
|
|
|
rect = [QPointF(0., 0.), QPointF(size, 0.), QPointF(size, size), QPointF(0., size)]
|
|
|
|
rotation = [0, 90, 180, 270]
|
|
|
|
|
2013-09-17 10:55:26 +02:00
|
|
|
nopen = QtGui.QPen(foreColor, Qt.NoPen)
|
2013-09-16 21:08:55 +02:00
|
|
|
foreBrush = QtGui.QBrush(foreColor, Qt.SolidPattern)
|
2013-09-17 10:55:26 +02:00
|
|
|
if penwidth > 0:
|
2013-09-18 17:39:45 +02:00
|
|
|
pen_color = QtGui.QColor(255, 255, 255)
|
2013-09-17 10:55:26 +02:00
|
|
|
pen = QtGui.QPen(pen_color, Qt.SolidPattern)
|
|
|
|
pen.setWidth(penwidth)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-16 21:08:55 +02:00
|
|
|
painter = QPainter()
|
|
|
|
painter.begin(image)
|
2013-09-17 10:55:26 +02:00
|
|
|
painter.setPen(nopen)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
|
|
|
painter.translate(pos[0] * size + penwidth / 2, pos[1] * size + penwidth / 2)
|
2013-09-17 10:55:26 +02:00
|
|
|
painter.translate(rect[rot])
|
2013-09-16 21:08:55 +02:00
|
|
|
painter.rotate(rotation[rot])
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-17 10:55:26 +02:00
|
|
|
if invert:
|
|
|
|
# subtract the actual polygon from a rectangle to invert it
|
2013-09-18 17:39:45 +02:00
|
|
|
poly_rect = QPolygonF(rect)
|
|
|
|
polygon = poly_rect.subtracted(polygon)
|
2013-09-16 21:08:55 +02:00
|
|
|
painter.setBrush(foreBrush)
|
2013-09-17 10:55:26 +02:00
|
|
|
if penwidth > 0:
|
|
|
|
# draw the borders
|
|
|
|
painter.setPen(pen)
|
|
|
|
painter.drawPolygon(polygon, Qt.WindingFill)
|
|
|
|
# draw the fill
|
|
|
|
painter.setPen(nopen)
|
2013-09-16 21:08:55 +02:00
|
|
|
painter.drawPolygon(polygon, Qt.WindingFill)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-16 21:08:55 +02:00
|
|
|
painter.end()
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-16 21:08:55 +02:00
|
|
|
return image
|
2013-08-27 11:47:14 +02:00
|
|
|
|
2018-05-16 13:31:06 +02:00
|
|
|
def decode(self, code, twoColor):
|
|
|
|
"""virtual functions"""
|
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
raise NotImplementedError
|
2018-05-16 13:31:06 +02:00
|
|
|
|
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
class DonRenderer(IdenticonRendererBase):
|
|
|
|
"""
|
|
|
|
Don Park's implementation of identicon
|
2019-10-11 13:14:08 +02:00
|
|
|
see: http://www.docuverse.com/blog/donpark/2007/01/19/identicon-updated-and-source-released
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
PATH_SET = [
|
2018-05-16 13:31:06 +02:00
|
|
|
# [0] full square:
|
2013-09-18 17:39:45 +02:00
|
|
|
[(0, 0), (4, 0), (4, 4), (0, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [1] right-angled triangle pointing top-left:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 0), (4, 0), (0, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [2] upwardy triangle:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(2, 0), (4, 4), (0, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [3] left half of square, standing rectangle:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 0), (2, 0), (2, 4), (0, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [4] square standing on diagonale:
|
2013-09-18 17:39:45 +02:00
|
|
|
[(2, 0), (4, 2), (2, 4), (0, 2)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [5] kite pointing topleft:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 0), (4, 2), (4, 4), (2, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [6] Sierpinski triangle, fractal triangles:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(2, 0), (4, 4), (2, 4), (3, 2), (1, 2), (2, 4), (0, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [7] sharp angled lefttop pointing triangle:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 0), (4, 2), (2, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [8] small centered square:
|
2013-09-18 17:39:45 +02:00
|
|
|
[(1, 1), (3, 1), (3, 3), (1, 3)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [9] two small triangles:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(2, 0), (4, 0), (0, 4), (0, 2), (2, 2)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [10] small topleft square:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 0), (2, 0), (2, 2), (0, 2)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [11] downpointing right-angled triangle on bottom:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 2), (4, 2), (2, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [12] uppointing right-angled triangle on bottom:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(2, 2), (4, 4), (0, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [13] small rightbottom pointing right-angled triangle on topleft:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(2, 0), (2, 2), (0, 2)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [14] small lefttop pointing right-angled triangle on topleft:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 0), (2, 0), (0, 2)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [15] empty:
|
2013-09-18 17:39:45 +02:00
|
|
|
[]]
|
|
|
|
# get the [0] full square, [4] square standing on diagonale, [8] small centered square, or [15] empty tile:
|
2013-08-27 11:47:14 +02:00
|
|
|
MIDDLE_PATCH_SET = [0, 4, 8, 15]
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
# modify path set
|
|
|
|
for idx in xrange(len(PATH_SET)):
|
|
|
|
if PATH_SET[idx]:
|
2018-05-16 13:31:06 +02:00
|
|
|
p = [(vec[0] / 4.0, vec[1] / 4.0) for vec in PATH_SET[idx]]
|
2013-08-27 11:47:14 +02:00
|
|
|
PATH_SET[idx] = p + p[:1]
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-17 10:55:26 +02:00
|
|
|
def decode(self, code, twoColor):
|
2018-05-16 13:31:06 +02:00
|
|
|
"""decode the code"""
|
|
|
|
|
|
|
|
shift = 0
|
|
|
|
middleType = (code >> shift) & 0x03
|
|
|
|
shift += 2
|
|
|
|
middleInvert = (code >> shift) & 0x01
|
|
|
|
shift += 1
|
|
|
|
cornerType = (code >> shift) & 0x0F
|
|
|
|
shift += 4
|
|
|
|
cornerInvert = (code >> shift) & 0x01
|
|
|
|
shift += 1
|
|
|
|
cornerTurn = (code >> shift) & 0x03
|
|
|
|
shift += 2
|
|
|
|
sideType = (code >> shift) & 0x0F
|
|
|
|
shift += 4
|
|
|
|
sideInvert = (code >> shift) & 0x01
|
|
|
|
shift += 1
|
|
|
|
sideTurn = (code >> shift) & 0x03
|
|
|
|
shift += 2
|
|
|
|
blue = (code >> shift) & 0x1F
|
|
|
|
shift += 5
|
|
|
|
green = (code >> shift) & 0x1F
|
|
|
|
shift += 5
|
|
|
|
red = (code >> shift) & 0x1F
|
|
|
|
shift += 5
|
|
|
|
second_blue = (code >> shift) & 0x1F
|
|
|
|
shift += 5
|
|
|
|
second_green = (code >> shift) & 0x1F
|
|
|
|
shift += 5
|
|
|
|
second_red = (code >> shift) & 0x1F
|
|
|
|
shift += 1
|
|
|
|
swap_cross = (code >> shift) & 0x01
|
|
|
|
|
2013-09-17 10:55:26 +02:00
|
|
|
middleType = self.MIDDLE_PATCH_SET[middleType]
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
foreColor = (red << 3, green << 3, blue << 3)
|
2013-09-16 21:08:55 +02:00
|
|
|
foreColor = QtGui.QColor(*foreColor)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-17 10:55:26 +02:00
|
|
|
if twoColor:
|
|
|
|
secondColor = (second_blue << 3, second_green << 3, second_red << 3)
|
|
|
|
secondColor = QtGui.QColor(*secondColor)
|
|
|
|
else:
|
|
|
|
secondColor = foreColor
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
return (middleType, middleInvert, 0),\
|
|
|
|
(cornerType, cornerInvert, cornerTurn),\
|
|
|
|
(sideType, sideInvert, sideTurn),\
|
2018-05-16 13:31:06 +02:00
|
|
|
foreColor, secondColor, swap_cross
|
2013-08-27 11:47:14 +02:00
|
|
|
|
|
|
|
|
2013-09-18 17:39:45 +02:00
|
|
|
def render_identicon(code, size, twoColor=False, opacity=255, penwidth=0, renderer=None):
|
2018-05-16 13:31:06 +02:00
|
|
|
"""Render an image"""
|
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
if not renderer:
|
|
|
|
renderer = DonRenderer
|
2018-05-16 13:31:06 +02:00
|
|
|
return renderer(code).render(size, twoColor, opacity, penwidth)
|