def _get_drawing(self, palette_index=0): box = self.get_box() if box is None: return None else: width = box[2] - box[0] height = box[3] - box[1] d = document(width+2, height+2, "pt") p = d.addpage() # _path = shape.path(shape(), []) for i in range(len(self.layers)): layer = self.font.rfont[self.layers[i]] colorindex = self.colors[i] layer_color = self.font._get_fcolor(palette_index, colorindex) pen = FlatPen(self.font.rfont) layer.draw(pen) g = p.place(shape().nostroke().fill(layer_color).path(pen.path)) g.position(1 - box[0], 1 - box[1]) return p
def make_chromachip_png(colour_list, width=600, height=600): """ Use flat to generate and return png data with the colours specified in the colour_list. """ doc_height = height doc_width = width d = document(doc_width, doc_height, 'pt') p = d.addpage() chip_height = doc_height / len(colour_list) for i, row in enumerate(colour_list): chip_width = doc_width / len(row) chip_y = i * chip_height for j, colour in enumerate(row): chip_rgb = rgb(*hex_to_rgb(colour)) colour_thing = shape().nostroke().fill(chip_rgb) p.place(colour_thing.rect(j * chip_width, chip_y, chip_width, chip_height)) return p.image(kind='rgb').png()
def _get_drawing(self, palette_index=0): box = self.get_box() if box is None: return None else: width = box[2] - box[0] height = box[3] - box[1] d = document(width + 2, height + 2, "pt") p = d.addpage() # _path = shape.path(shape(), []) for i in range(len(self.layers)): layer = self.font.rfont[self.layers[i]] colorindex = self.colors[i] layer_color = self.font._get_fcolor(palette_index, colorindex) pen = FlatPen(self.font.rfont) layer.draw(pen) g = p.place(shape().nostroke().fill(layer_color).path( pen.path)) g.position(1 - box[0], 1 - box[1]) return p
import random import wand from flat import rgb, font, shape, strike, document, command r = random red = rgb(255, 0, 0) #lato = font.open('Lato-Reg.otf') figure = shape().stroke(red).width(0.5) #headline = strike(lato).color(red).size(20, 24) d = document(100, 100, 'mm') p = d.addpage() p.place(figure.circle(50, 50, 20)) p.place(figure.circle(50, 50, 20)).position(random.randint(50, 50), random.randint(50, 50)) d.pdf('hello.pdf') from wand.image import Image as wi pdf = wi(filename="hello.pdf", resolution=300) pdfimage = pdf.convert("jpeg") i = 1 for img in pdfimage.sequence: page = wi(image=img) page.save(filename=str(i) + ".jpg") i += 1
# Only runs under Flat from flat import rgb, font, shape, strike, document #c = rgb(255, 0, 0) c = rgb(0, 0, 0) #lato = font.open('/Library/Fonts/Verdana.ttf') lato = font.open('/Library/Fonts/Upgrade-Middle.ttf') figure = shape().stroke(c).width(2.5) headline = strike(lato).color(c).size(80, 96) d = document(400, 200, 'mm') p = d.addpage() p.place(figure.circle(50, 50, 20)) t = headline.text(u'Hello world! AVT.TeYAYeYé') p.place(t).frame(10, 10, 380, 80) im = p.image(kind='rgb') im.png('_export/hello.png') im.jpeg('_export/hello.jpg') p.svg('_export/hello.svg') d.pdf('_export/hello.pdf')
import dask import numpy as np from flat import document, shape, rgba from lineart.shapes import Octahedron from lineart import draw # Colors and Styles sand = rgba(254, 250, 219, 50) sandstyle = shape().fill(sand).nostroke() blue = rgba(33, 80, 98, 255) background = shape().fill(blue).nostroke() @dask.delayed def rotating_sampled_octagon(step, total_steps=20): image_size = 100 # page setup d = document(image_size, image_size, "mm") page = d.addpage() page.place(background.rectangle(0, 0, image_size, image_size)) o = Octahedron(np.array([50, 50, 40]), 40) o.rotate_unison( np.array([[50, 50, 40]]), np.array([1, 1, 1]), np.pi * 2 / total_steps * step ) image = draw.draw_zsampled_edges(o.edges, n=1000, scatter=0.01) return image
from random import random from flat import document, shape, gray d = document(100, 100, 'mm') p = d.addpage() figure = shape().nostroke().fill(gray(0)) for i in range(10000): x = random() * 96.0 + 2.0 y = random() * 96.0 + 2.0 r = random()**2 p.place(figure.circle(x, y, r)) d.pdf('random-dots.pdf')
beziers = beziers_from_catmull(vertices, tightness) polygon = beziers_tangent_offset_polygon(beziers, thicknesses, samples_per, interp) return polygon def flatten(t): "Convenience function to flatten lists of pts to format required for flat" from itertools import chain return list(chain(*t)) if __name__ == '__main__': from random import randrange from flat import document, rgba, shape width, height = (500, 500) pts = [[100,100], [100,100], [100,400], [400,400], [400,100], [400,100]] bez = beziers_from_catmull(pts) poly = fancy_curve(pts, [1, 10, 50, 25, 20, 15, 10], samples_per=24, interp='cubic') bg_shape = shape().nostroke().fill(rgba(255, 255, 255, 255)) pts_shape = shape().stroke(rgba(255, 0, 0, 240)).width(2) catmull_shape = shape().stroke(rgba(0, 255, 0, 240)).width(2) poly_shape = shape().nostroke().fill(rgba(0, 0, 0, 220)).width(4) doc = document(width, height, 'mm') page = doc.addpage() page.place(bg_shape.rectangle(0, 0, width, height)) page.place(poly_shape.polygon(flatten(poly))) page.place(catmull_shape.path(smooth_point_path(pts))) page.place(pts_shape.polyline(flatten(pts))) page.image(kind='rgba').png('test.png')
from flat import rgba, shape # colors canvas = rgba(254, 250, 239, 255) sand = rgba(254, 250, 219, 50) blue = rgba(33, 80, 98, 255) blue_low_alpha = rgba(33, 80, 98, 50) red = rgba(255, 0, 0, 255) # styles blue_fill = shape().fill(blue).nostroke() canvas_fill = shape().fill(canvas).nostroke() canvas_edge = shape().stroke(canvas).width(1) blue_edge = shape().stroke(blue).width(1) blue_sand = shape().fill(blue_low_alpha).nostroke() sand = shape().fill(sand).nostroke() debug = shape().stroke(red).width(0.5)
def testFlat(): context = getContext('Flat') pagebotFont = findFont(FONTNAME) flatFont = font.open(pagebotFont.path) flatFill = rgb(180, 0, 125) pagebotFill = color((180.0 / 255, 0, 125.0 / 255)) flatStroke = rgb(100, 180, 0) pagebotStroke = color(100.0 / 255, 180.0 / 255, 0) strokeWidth = 1 ''' Creates a document. ''' # Flat. doc = document(WIDTH, HEIGHT, 'pt') p = doc.addpage() # Pagebot. context.newDocument(WIDTH, HEIGHT) context.newPage() ''' Draws a figure. ''' # Flat. figure = shape().fill(flatFill).stroke(flatStroke).width(strokeWidth) r = figure.rectangle(50, 50, 20, 20) p.place(r) # Pagebot. context.fill(pagebotFill) context.stroke(pagebotStroke) context.strokeWidth(strokeWidth) context.rect(50, 50, 20, 20) ''' print(p.items[0].item.style.width) print(context.pages[0].items[0].item.style.width) ''' s = context.pages[0].items[0] ''' print(s.item.style.fill) print(s.item.style.stroke) print(s.item.style.join) print(s.item.style.limit) ''' ''' Draws text. ''' msg = 'Hello world!' # Flat. headline = strike(flatFont).color(flatStroke).size(FONTSIZE, LEADING, units='pt') t = headline.text(msg) entity = p.place(t) entity.frame(100, 100, 380, 80) # Pagebot. style = dict(font=pagebotFont, fontSize=FONTSIZE, color=pagebotStroke, leading=LEADING) bs = context.newString(msg, style=style) context.text('bla', (50, 100)) # TODO: also for native flat. context.text(bs, (100, 100)) ''' print(headline.style.size) print(headline.style.leading) print(headline.style.color.r) print(headline.style.color.g) print(headline.style.color.b) ''' ''' Exports file. ''' im = p.image(kind='rgb') #print(p.items) # TODO: #imagePath = getResourcesPath() + '/images/peppertom_lowres_398x530.png' #size = context.imageSize(imagePath) #print(size) if not os.path.exists('_export'): os.mkdir('_export') #print('Exporting native') doc.pdf('_export/native-flat.pdf') ''' im.png('_export/native-flat.png') im.jpeg('_export/native-flat.jpg') p.svg('_export/native-flat.svg') ''' print(context.doc) context.saveDocument('_export/pagebot-flat.pdf')
explication_txt = "La mise en page de ce poster a été réalisée selon les principes exposés dans l'article au recto, \ en utilisant trois bibliothèques du langage Python pour la typographie et le traitement des images : \ \nLa première bibliothèque est une version alpha de Drawbot, adaptée à la librairie graphique Skia \ pour une utilisation sur différents systèmes d'exploitation par Just Van Rossum (pypi.org/project/drawbot-skia). \ \nLa deuxième bibliothèque, Flat (xxyxyz.org) de Juraj Sukop, ne semble toutefois plus maintenue depuis 2018. \ \nLa troisième bibliothèque, Pillow, est très utilisée (pillow.readthedocs.io) dans le domaine du traitement d'image. \ \nLe code qui a permis la réalisation de ce poster est disponible à l'adresse : github.com/jeremien/maj.poster." regular = font.open('font/PlantinMTProRg.TTF') bold = font.open('font/Erbarre-Bold.otf') body = strike(regular).size(10) titre = strike(bold).size(13) d = document(doc_width, doc_height, 'pt') p = d.addpage() s = shape() def setImages(folder: str) -> list: images = [] path = base_path + folder + '/' with os.scandir(path) as entries: for entry in entries: try: if entry.is_file(): name = path + entry.name try: im = Image.open(name).convert('CMYK') print(im.mode) im.save( name, resolution=300.0,
tn = truncnorm((a - mu) / sigma, (b - mu) / sigma, loc=mu, scale=sigma) return tn.rvs(1)[0] def make_scribble(width, height, steps, stddev=0): pts = [] for i in range(steps): x = ((width / steps) * i) + normal(0, stddev) y = normal(0, height) pts.append([x, y]) return Polyline(pts) size = 200 d = document(size, size, 'mm') page = d.addpage() curve_figure = shape().stroke(rgba(0, 0, 0, 255)).width(2) row_n = 16 for i in range(row_n): scribble_poly = make_scribble(width=size - 20, height=(size / row_n) * 0.25, steps=int(uniform(25, 250)), stddev=uniform(2)) scribble_poly_tr = scribble_poly.translate(10, 10 + (i * size / (row_n + 1))) curve = curve_figure.path(scribble_poly_tr.smooth_path(tightness=-0.5)) page.place(curve) with open("scribble.svg", "wb") as fh: fh.write(page.svg())
def testFlat(): context = getContext('Flat') f = findFont(FONTNAME) ff = font.open(f.path) c1 = rgb(180, 0, 125) c2 = rgb(100, 180, 0) style = dict(font=f, fontSize=FONTSIZE, color=c2, leading=LEADING) # Creates the document. d = document(WIDTH, HEIGHT, 'mm') #print(d.width) #print(d.height) p = d.addpage() #print(p.width) context.newDocument(WIDTH, HEIGHT) #print(context.doc.width) #print(context.doc.height) context.newPage() print(context.pages[0].width) print(context.pages[0].height) figure = shape().fill(c1).stroke(c2).width(2.5) r = figure.rectangle(50, 50, 20, 20) print(r) p.place(r) context.fill((180, 0, 125)) context.stroke((100, 180, 0)) context.strokeWidth(2.5) context.rect(50, 50, 20, 20) #print(p.items[0].item.style.width) #print(context.pages[0].items[0].item.style.width) s = context.pages[0].items[0] #print(s.item.style.fill) #print(s.item.style.stroke) #print(s.item.style.join) #print(s.item.style.limit) headline = strike(ff).color(c2).size(FONTSIZE, LEADING) t = headline.text('Hello world!') entity = p.place(t) entity.frame(10, 10, 380, 80) bs = context.newString('Hello world!', style=style) #print(bs.__class__.__name__) context.text(bs, (10, 10)) ''' print(headline.style.size) print(headline.style.leading) print(headline.style.color.r) print(headline.style.color.g) print(headline.style.color.b) ''' im = p.image(kind='rgb') #print(p.items) # TODO: #imagePath = getResourcesPath() + '/images/peppertom_lowres_398x530.png' #size = context.imageSize(imagePath) #print(size) if not os.path.exists('_export'): os.mkdir('_export') print('Exporting native') d.pdf('_export/native-flat.pdf') im.png('_export/native-flat.png') im.jpeg('_export/native-flat.jpg') p.svg('_export/native-flat.svg') print('Exporting pagebot') context.saveDocument('_export/pagebot-flat.pdf') context.saveDocument('_export/pagebot-flat.png') context.saveDocument('_export/pagebot-flat.jpg') context.saveDocument('_export/pagebot-flat.svg')