Comment utiliser ces 2 scripts Python : Vidéo d’explication
Comprendre en 10 minutes les mathématiques cachées derrière : Les courbes de Bézier - Elles sont partout !
def conv(s): try: return int(float(s)) except: return s def nw(): img = gimp.image_list()[0] svg = pdb.gimp_vectors_export_to_string(img, img.vectors[0]) svg = svg.split(" d=\"")[1].split("\"")[0].replace(" ",",").replace("\n",",").replace("M",",M") svg = [conv(v) for v in svg.split(",") if len(v)>0] return svg
Ce script permet de récupérer dans une chaine de caractères nommée svg
les coordonnées des points et des poignées. Un petit nettoyage est fait sur les espace et les retours à la ligne et on ne prend que des coordonnées entières afin de gagner en espace mémoire.
svg = ['M',166,119,'C',166,119,149,71,149,71,148,68,146,65,142,64,138,63,134,62,130,63,130,63,111,68,111,68,108,68,107,70,107,72,108,74,111,75,114,74,114,74,133,70,133,70,134,69,136,70,137,70,138,71,139,71,139,72,139,72,152,109,152,109,150,109,148,109,146,109,140,109,134,111,130,114,126,111,121,109,115,109,109,109,103,110,99,113,97,115,95,117,94,120,94,120,87,120,87,120,86,117,84,115,82,113,78,110,73,109,67,109,60,109,55,111,51,114,47,111,41,109,35,109,33,109,31,109,29,109,29,109,41,72,41,72,42,71,43,70,44,70,45,70,47,69,48,70,48,70,67,74,67,74,70,75,73,74,74,72,74,70,73,68,70,68,70,68,51,63,51,63,47,62,43,63,39,64,35,65,33,68,32,71,32,71,15,120,15,120,15,121,15,123,15,124,15,132,24,137,37,145,40,147,44,149,48,151,49,151,50,152,51,152,52,152,53,151,54,151,58,149,62,147,65,145,76,138,85,134,87,127,87,127,94,127,94,127,96,134,105,138,116,145,120,147,123,149,127,151,128,151,129,152,130,152,131,152,133,151,133,151,137,149,141,147,144,145,157,137,167,132,167,124,167,122,166,121,166,119,'Z','M',59,139,'C',56,141,54,142,51,144,48,142,46,141,43,1
from turtle import * from random import * NB = 10 svg = ['M',31,49,'C',31,49,31,23,31,23,31,23,49,49,49,49,54,44,50,24,50,24,'M',64,23,'C',64,23,62,45,65,48,67,50,73,51,73,51,73,51,79,51,81,47,83,43,82,23,82,23,'M',96,49,'C',96,49,96,22,96,22,96,22,108,49,108,49,108,49,121,23,121,23,121,23,122,48,122,48,'M',136,23,'C',136,23,140,49,142,49,144,49,148,23,150,23,152,22,155,49,157,49,158,50,164,25,165,24,'M',184,22,'C',188,22,196,27,196,35,196,43,190,51,185,50,180,50,173,46,173,37,174,27,180,22,184,22,'Z','M',207,50,'C',207,50,207,23,207,23,207,23,220,22,220,22,220,22,224,24,224,29,224,34,222,35,219,35,216,36,209,36,209,36,'M',221,39,'C',221,39,222,46,224,47,225,48,227,50,228,49,'M',239,23,'C',239,23,240,49,240,49,'M',240,38,'C',240,38,257,24,257,24,'M',248,34,'C',248,34,256,47,259,48,'M',287,25,'C',287,25,279,18,272,23,268,28,270,30,271,33,271,36,285,36,286,39,287,42,288,46,285,48,283,51,273,51,270,48,'M',59,101,'C',59,101,76,101,76,101,'M',67,104,'C',67,104,66,131,66,131,66,131,65,138,56,138,48,138,46,138,44,133,42,128,43,124,48,121,'M',94,102,'C',94,102,94,139,94,139,'M',97,102,'C',97,102,110,102,115,101,'M',96,119,'C',96,119,106,119,106,119,'M',97,138,'C',97,138,114,140,116,138,'M',158,100,'C',158,100,158,133,158,133,'M',144,99,'C',145,97,174,97,174,98,'M',193,83,'C',193,83,188,92,187,94,'M',230,147,'C',225,147,210,122,207,115,205,109,205,97,217,97,228,98,231,109,231,111,231,113,235,97,244,97,253,97,255,104,254,113,253,122,234,147,230,147,'Z','M',201,144,'C',201,144,226,127,226,127,'M',258,102,'C',258,102,270,94,270,94,'M',268,87,'C',268,87,277,86,280,87,282,89,275,97,275,97,'M',194,194,'C',201,197,207,184,198,183,189,182,190,191,189,197,189,203,193,205,200,204,'M',209,175,'C',208,177,208,181,209,181,'M',211,166,'C',209,166,207,168,209,169,'M',222,174,'C',222,178,221,182,221,182,'M',230,174,'C',230,174,230,181,230,181,'M',218,173,'C',221,171,221,170,225,171,229,172,232,172,232,171,'M',244,192,'C',244,192,256,192,256,192,'M',244,199,'C',244,199,255,199,255,199,'M',266,195,'C',266,195,273,195,273,195,'M',283,189,'C',283,189,289,183,289,183,289,183,289,205,289,205] def move(x,y): pensize(randint(3,6)) goto(x-160,110-y) def coord(a,b,c,d,t): return a*(1-t)**3+3*b*t*(1-t)**2+3*c*t*t*(1-t)+d*t**3 def bezier(l): pendown() t = 0 for i in range(NB+1): x = coord(l[0],l[2],l[4],l[6],t) y = coord(l[1],l[3],l[5],l[7],t) move(x,y) t += 1/NB speed(2) i = 0 while i<len(svg): color(randint(0,255),randint(0,255),randint(0,255)) # version rose : # color(255,randint(0,140),255) if svg[i] == "M": l = [svg[i+1],svg[i+2]] penup() move(l[0], l[1]) i += 3 elif svg[i] == "Z" or svg[i] == "C": i += 1 else: for j in range(6): l.append(svg[i+j]) bezier(l) l = [svg[i+4],svg[i+5]] i += 6 hideturtle()