Skip to content

Instantly share code, notes, and snippets.

@dogtopus
Created January 22, 2019 18:29
Show Gist options
  • Save dogtopus/afae263293c17bb84c2e0e9cfbd13905 to your computer and use it in GitHub Desktop.
Save dogtopus/afae263293c17bb84c2e0e9cfbd13905 to your computer and use it in GitHub Desktop.
Vuforia Frame Marker generator (legacy code)
from PIL import Image, ImageDraw
DEFAULT_NESW = (292, 246, 177, 472)
OFFSETS_NESW = ((1, 0), (19, 1), (2, 19), (0, 2))
class common(object):
def __init__(self, fmid = 0):
self.fmid = fmid
def get_nesw(self):
return [(self.fmid ^ i) for i in DEFAULT_NESW]
def calc_all(self):
result = [
self.calc_dots(num, OFFSETS_NESW[index],
(index >> 1) & 1, index & 1
) for index, num in enumerate(self.get_nesw())
]
return result
def calc_dots(self, num = 0, offset = (0, 0), reverse = False, vertical = False):
bits = [(num & (1 << shifts)) >> shifts for shifts in range(8, -1, -1)]
if reverse: bits.reverse()
if vertical:
coords = [(offset[0], y) for y in range(offset[1], 2 * len(bits) + offset[1], 2)]
else:
coords = [(x, offset[1]) for x in range(offset[0], 2 * len(bits) + offset[0], 2)]
return zip(coords, bits)
# ---------------------------------------------->8
class bitmap(common):
def __init__(self, fmid = 0, scale = 1):
super(bitmap, self).__init__(fmid)
self.scale = scale
self.reset_image()
def reset_image(self):
self.image = Image.new('RGBA', (20, 20))
def plot(self):
for data in self.calc_all():
self.plot_dots(data)
# add an extra white frame (otherwise libqcar won't recongnize the generated image)
result = Image.new('RGBA', (24, 24), 'white')
result.paste(Image.new('RGBA', (22, 22), 'black'), (1, 1))
result.paste(self.image, (2, 2))
if self.scale != 1:
return result.resize([24 * self.scale] * 2)
else:
return result
def plot_dots(self, data):
colors = ['white', 'black']
draw = ImageDraw.Draw(self.image)
for xy, b in data:
draw.point(xy, colors[b])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment