Created
May 5, 2017 01:52
-
-
Save frostidaho/1391da4427e54bafe1896fa485c4656e to your computer and use it in GitHub Desktop.
using_libqtile_images.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aa_svg = b""" | |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 112 90"> | |
<path d="M68,87h-13l20-56h14l20,56h-13l-4-12h-20l-4,12zM75,65h14l-7-22l-7,22z | |
" fill="#2a376e"/> | |
<path d="M15,87h-13l20-56h14l19,56h-12l-4-12h-21l-3,12zM22,65h14l-7-22l-7,22z" fill="#bf2426"/> | |
<path d="M705,169c5,18,5,27-2,48l-58,172l-62-175c0,0,7-19,11-30c3,0,9,7,9,7c0,0-7-20-4-24l9-24l10,6c-2-6-5-19-5-19l9-28l10,5c0,0-7-19-5-22l24-63c20,47,36,98,54,147zM659,485c1,5,0,11-5,19c1,10,1,13-2,17c-3,3-6,4-10,4c1-3,1-7,0-8c-13-17-42-50-42-50h-15l-14,36l6,10c0,0,13,0,19,0c1,0,2,1,3,3v6c-1-1-3-2-4-3c-14,0-24,0-37,1l-4,2c-2-5,2-9,5-9c3,0,6,0,5-2c-1-3-2-4-5-7h-16c-16-46-33-92-47-139c6,0,14-3,14-3c0,0-16-6-20-15c-3-9-14-40-14-40c0,0,9-1,13-3c-5-4-18-8-20-14c-7-18-11-35-11-35c0,0,16-1,14-2c-9-4-21-16-21-16c0,0-11-31-11-31c1-1,16,0,16,0c-4-8-16-12-13-23c4-13,9-26,12-34l9,5c-1-5-4-23-4-23l12-35l9,7l-3-24l20-51l52,147l109,310zM475,476l-5-8l-6,8h-22l48-74l27,74h-14l-5-8l-5,8h-18z" fill="#2a376e" transform="scale(0.1)"/> | |
</svg> | |
""" | |
from libqtile.images import Img | |
import cairocffi | |
from tempfile import TemporaryDirectory | |
from os import path, chdir | |
def create_montage_image(*imgs): | |
from subprocess import Popen, PIPE | |
cmd = ['montage'] | |
for img in imgs: | |
cmd.extend(['-label', img.label, img.path]) | |
# cmd.extend(['-font', 'Ubuntu', '-tile', '1x{}'.format(len(imgs)), '-geometry', '+16x16+1+1', '/tmp/out.png']) | |
cmd.extend(['-font', 'Ubuntu', '-frame', '5', '-geometry', '200x200>+10+10', '/tmp/out.png']) | |
print(cmd) | |
p = Popen(cmd, stdout=PIPE, stderr=PIPE) | |
stdout, stderr = p.communicate() | |
print('stdout', stdout) | |
print('stderr', stderr) | |
print('retcoe', p.returncode) | |
# run(cmd).check_returncode() | |
def save_images(*imgs): | |
with TemporaryDirectory() as tdir: | |
chdir(tdir) | |
for idx, img in enumerate(imgs): | |
base_path = path.join(tdir, img.name) | |
# surf = cairocffi.SVGSurface(base_path + '.svg', img.width, img.height) | |
surf = cairocffi.SVGSurface(base_path + '.svg', 200, 200) | |
ctx = cairocffi.Context(surf) | |
ctx.save() | |
ctx.set_source(img.pattern) | |
ctx.paint() | |
ctx.restore() | |
png_path = base_path + '.png' | |
surf.write_to_png(png_path) | |
surf.finish() | |
img.path = png_path | |
create_montage_image(*imgs) | |
nimgs = 0 | |
def get_img(name='aa'): | |
global nimgs | |
img = Img(aa_svg, name=name+str(nimgs)) | |
nimgs += 1 | |
return img | |
img = get_img() | |
img.label = 'base ({}x{})'.format(*img.default_size) | |
img2 = get_img() | |
img2.scale(1.5, 0.5) | |
img2.label = 'img2.scale(1.5, 0.5)\n-> ({}x{})'.format(img2.width, img2.height) | |
img3 = get_img() | |
img3.scale(1.5, lock_aspect_ratio=True) | |
img3.label = 'img3.scale(1.5,\nlock_aspect_ratio=True)\n-> ({}x{})'.format(img3.width, img3.height) | |
img6 = get_img() | |
img6.scale(height_factor=1.5, lock_aspect_ratio=True) | |
img6.label = 'img6.scale(height_factor=1.5,\nlock_aspect_ratio=True)\n-> ({}x{})'.format(img6.width, img6.height) | |
img4 = get_img() | |
img4.resize(width=200) | |
img4.label = 'img4.resize(width=200)\n-> ({}x{})'.format(img4.width, img4.height) | |
img5 = get_img() | |
img5.resize(width=112, height=45) | |
img5.label = 'img5.resize(width=112, height=45)\n-> ({}x{})'.format(img5.width, img5.height) | |
img7 = get_img() | |
img7.resize(height=45) | |
img7.label = 'img7.resize(height=45)\n-> ({}x{})'.format(img7.width, img7.height) | |
# WIP | |
# img8 = get_img() | |
# img8.theta = 30.0 | |
# img8.label = 'img8.theta = 30.0\n-> ({}x{})'.format(img8.width, img8.height) | |
save_images(img, img2, img3, img6, img4, img5, img7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment