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
def setup_tiled_page( tile_size=(100, 100), n_cols=1, n_rows=1, background=style.canvas_fill): x_dim = tile_size[0] * n_cols y_dim = tile_size[1] * n_rows d = document(x_dim, y_dim, "mm") page = d.addpage() page.place(background.rectangle( 0, 0, x_dim, y_dim, )) return page
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 layout(author, title, *paragraphs): regular = font.open("/home/macrico/FONTS/Inknut-Antiqua/TTF-OTF/InknutAntiqua-Regular.ttf") bold = font.open("/home/macrico/FONTS/Inknut-Antiqua/TTF-OTF/InknutAntiqua-Bold.ttf") body = strike(regular).size(12, 16) headline = strike(bold).size(12, 16) story = text( body.paragraph(author), headline.paragraph(title), body.paragraph(""), *[body.paragraph(p) for p in paragraphs]) doc = document(148, 210, "mm") page = doc.addpage() block = page.place(story).frame(18, 21, 114, 167) while block.overflow(): page = doc.addpage() block = page.chain(block).frame(18, 21, 114, 167) return doc
def quick_draw_edges( edges, image_size=100, v=False, background=style.canvas_fill, edge_style=style.blue_edge, ): edges = edges[:, :, :-1] # page setup d = document(image_size, image_size, "mm") page = d.addpage() page.place(background.rectangle(0, 0, image_size, image_size)) for e in edges: page.place(edge_style.line(*e.flatten())) if v: for p in edges.reshape(-1, 2): page.place(style.debug.circle(*p, 2)) return page.image(kind="rgba", ppi=60)
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
def quick_draw_zsampled_edges( edges, n=1000, scatter=0.02, image_size=100, v=False, background=style.canvas_fill, point_style=style.blue_sand, ): 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)) line_points = [z_blur_sample_line(*e, n, scatter) for e in edges] points = np.concatenate(line_points) for p in points: page.place(point_style.circle(*p[:2], 0.1)) return page.image(kind="rgba", ppi=60)
def testFlatContext(): context = getContext('Flat') # PageBot implementation, to be used for strings and styles. pagebotFont = findFont(FONTNAME) pagebotFill = color((180.0 / 255, 0, 125.0 / 255)) pagebotStroke = color(100.0 / 255, 180.0 / 255, 0) # Native flat implementation of fonts and colors. flatFont = font.open(pagebotFont.path) flatFill = rgb(180, 0, 125) flatStroke = rgb(100, 180, 0) # Stroke width is the same. strokeWidth = 1 ''' Creates a document. ''' # Creating a Flat document. flatDoc = document(WIDTH, HEIGHT, 'pt') flatPage = flatDoc.addpage() # Pagebot equivalent. context.newDrawing(WIDTH, HEIGHT) pbPage = context.newPage() print(pbPage) ''' 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. header = strike(flatFont).color(flatStroke).size(FONTSIZE, LEADING, units='pt') t = header.text(msg) placedText = flatPage.place(t).frame(100, 100, 380, 80) # Pagebot. style = dict(font=pagebotFont, fontSize=FONTSIZE, textFill=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) # Now for conditions and elements. c = (Left2Left(), Fit2Right(), Float2Top()) style = dict(fontSize=14, font=pagebotFont) msg = 'Testing textBox' print(msg) bs = context.newString(msg, style=style) print(type(bs)) newTextBox(bs, font=pagebotFont, parent=pbPage, conditions=c, fill=0.9, margin=4) #print(p.items) ''' Exports file. ''' im = flatPage.image(kind='rgb') # 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') flatDoc.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.saveDrawing('_export/pagebot-flat.pdf')
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')
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')
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')
jurg_lehni_txt = "*Hektor Draws a Landscape* & *Hektor Titles a Show*, « Lee 3 Tau Ceti Central Armory Show », Villa Arson, Nice, Villa Arson, Nice, 2003 et « Design and the Elastic Mind », MoMA New York, Jürg Lehni, 2008. (Juerglehni.com/works/hektor)\n© Jürg Lehni & Uli Franke, 2002/Jürg Lehni & Alex Rich, 2003." 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(
def t_normal(a, b, mu, sigma): 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')