First commit of the library
This commit is contained in:
commit
dd89e74d87
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
*~
|
||||
*.pyc
|
||||
/.ropeproject
|
||||
.\#*
|
||||
/output/*
|
90
apyx/__init__.py
Normal file
90
apyx/__init__.py
Normal file
@ -0,0 +1,90 @@
|
||||
"""
|
||||
Antipixels generator script
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import copy
|
||||
|
||||
from PIL import Image, ImageFont, ImageDraw
|
||||
|
||||
|
||||
def create_antipixel(cfg):
|
||||
font = ImageFont.truetype(cfg['base']['font_path'], size=8)
|
||||
width, height = cfg['base']['size']
|
||||
avail_space = width - 4 # padding: 2px on each side
|
||||
image = Image.new('RGB', cfg['base']['size'])
|
||||
draw = ImageDraw.Draw(image)
|
||||
parts = copy.deepcopy(cfg['parts']) # so we can safely alter
|
||||
|
||||
##--------------------------------------------------
|
||||
## Prepare sizing
|
||||
|
||||
even_parts = []
|
||||
for part in parts:
|
||||
part['text_width'] = draw.textsize(part['text'], font=font)[0]
|
||||
part_sizing = part.get('sizing', 'even')
|
||||
if part_sizing == 'even':
|
||||
## Keep track as we'll need later..
|
||||
even_parts.append(part)
|
||||
elif part_sizing == 'fixed':
|
||||
avail_space -= part['width']
|
||||
else:
|
||||
raise ValueError("Invalid sizing: " + part_sizing)
|
||||
avail_space -= len(parts) # spacing
|
||||
all_text_space = sum(x['text_width'] for x in even_parts)
|
||||
if all_text_space > avail_space:
|
||||
raise ValueError("Not enough space to fit text!")
|
||||
for part in even_parts:
|
||||
# Give space proportional
|
||||
part['sizing'] = 'fixed'
|
||||
part['width'] = int(round(1.0 * part['text_width'] *
|
||||
avail_space / all_text_space))
|
||||
|
||||
##--------------------------------------------------
|
||||
## Draw background
|
||||
|
||||
draw.rectangle(
|
||||
[(0, 0), (image.size[0] - 1, image.size[1] - 1)],
|
||||
outline=cfg['base']['outline'],
|
||||
fill=cfg['base']['background'])
|
||||
|
||||
##--------------------------------------------------
|
||||
## Draw rectangles, with text
|
||||
|
||||
# Vertical bounds
|
||||
vmin, vmax = 2, image.size[1] - 3
|
||||
# Horizontal offset
|
||||
offset = 2
|
||||
for part in parts:
|
||||
draw.rectangle(
|
||||
[(offset, vmin), (offset + part['width'], vmax)],
|
||||
fill=part['background'])
|
||||
|
||||
text_align = part.get('align', 'center')
|
||||
if text_align == 'left':
|
||||
text_offset = 1
|
||||
elif text_align == 'right':
|
||||
text_offset = part['width'] - part['text_width']
|
||||
elif text_align == 'center':
|
||||
text_offset = round((part['width'] - part['text_width']) / 2.0)
|
||||
else:
|
||||
raise ValueError("Invalid alignment: " + text_align)
|
||||
|
||||
draw.text((offset + text_offset, vmin + 1),
|
||||
part['text'], fill=part['color'], font=font)
|
||||
|
||||
offset += part['width']
|
||||
offset += 1 # space between rectangles
|
||||
|
||||
##--------------------------------------------------
|
||||
## Draw outline to make sure we clear overflows..
|
||||
|
||||
draw.rectangle(
|
||||
[(0, 0), (image.size[0] - 1, image.size[1] - 1)],
|
||||
outline=cfg['base']['outline'])
|
||||
draw.rectangle(
|
||||
[(1, 1), (image.size[0] - 2, image.size[1] - 2)],
|
||||
outline=cfg['base']['background'])
|
||||
|
||||
return image
|
34
apyx/templates.py
Normal file
34
apyx/templates.py
Normal file
@ -0,0 +1,34 @@
|
||||
"""
|
||||
Some templates for common antipixel patterns
|
||||
"""
|
||||
|
||||
|
||||
def twoparts_even(text1, fg1, bg1, text2, fg2, bg2, a1='center', a2='center'):
|
||||
return {
|
||||
'parts': [
|
||||
dict(text=text1, background=bg1, color=fg1, sizing='even',
|
||||
align=a1),
|
||||
dict(text=text2, background=bg2, color=fg2, sizing='even',
|
||||
align=a2),
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def twoparts_even_centered(text1, fg1, bg1, text2, fg2, bg2):
|
||||
return twoparts_even(text1, fg1, bg1, text2, fg2, bg2, 'right', 'left')
|
||||
|
||||
|
||||
def twoparts_even_altcolor(text1, text2, col1, col2):
|
||||
return twoparts_even(text1, col2, col1, text2, col1, col2)
|
||||
|
||||
|
||||
def twoparts_even_centered_altcolor(text1, text2, col1, col2):
|
||||
return twoparts_even_centered(text1, col2, col1, text2, col1, col2)
|
||||
|
||||
|
||||
def onepart(text, fg, bg):
|
||||
return {
|
||||
'parts': [
|
||||
dict(text=text, background=bg, color=fg, sizing='even'),
|
||||
],
|
||||
}
|
BIN
fonts/slkscr.ttf
Normal file
BIN
fonts/slkscr.ttf
Normal file
Binary file not shown.
BIN
fonts/slkscrb.ttf
Normal file
BIN
fonts/slkscrb.ttf
Normal file
Binary file not shown.
BIN
fonts/slkscre.ttf
Normal file
BIN
fonts/slkscre.ttf
Normal file
Binary file not shown.
BIN
fonts/slkscreb.ttf
Normal file
BIN
fonts/slkscreb.ttf
Normal file
Binary file not shown.
26
setup.py
Normal file
26
setup.py
Normal file
@ -0,0 +1,26 @@
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
|
||||
version = '0.1'
|
||||
install_requires = [
|
||||
'pillow',
|
||||
]
|
||||
|
||||
setup(
|
||||
name='APyX',
|
||||
version=version,
|
||||
packages=find_packages(),
|
||||
url='http://rshk.github.io/apyx',
|
||||
license='Apache License 2.0',
|
||||
author='Samuele Santi',
|
||||
author_email='samuele@samuelesanti.com',
|
||||
description='Simple antipixel generation utility',
|
||||
long_description='Simple antipixel generation utility',
|
||||
install_requires=install_requires,
|
||||
classifiers=[
|
||||
"License :: OSI Approved :: Apache Software License",
|
||||
"Development Status :: 3 - Alpha",
|
||||
"Programming Language :: Python :: 2",
|
||||
"Programming Language :: Python :: 2.7",
|
||||
],
|
||||
package_data={'': ['README.md', 'LICENSE']})
|
Loading…
Reference in New Issue
Block a user