def mainSquare(): turtle.delay(0) t.speed(0) t.fillcolor('#F6D02F') t.begin_fill() noTrace_goto(-700, 0) # cnt = input() result = 0 # for i in range(int(cnt)): # n = input() # array.append(int(n)) pt = t.position() ccx = 0 cnt = 0 for i in array: result = result + i pt = t.position() ccx = pt[0] if (i > 0): noTrace_goto(pt[0] + width, 50) if (cnt != 0): t.write('+', font=("Arial", 16, "normal")) pt = t.position() noTrace_goto(pt[0] + 30, 0) drawNumber(i) cnt = cnt + 1 else: noTrace_goto(pt[0] + width, 50) t.write('-', font=("Arial", 16, "normal")) pt = t.position() noTrace_goto(pt[0] + 30, 0) drawNumber(abs(i)) cnt = cnt + 1 pt = t.position() noTrace_goto(pt[0] + width, 50) t.write('=', font=("Arial", 16, "normal")) pt = t.position() noTrace_goto(pt[0] + 30, 0) drawNumber(abs(result)) t.hideturtle() nameSav = "2" + '.png' tmpdir = tempfile.mkdtemp() tmpfile = os.path.join(tmpdir, 'tmpp.svg') ts = turtle.getcanvas() canvasvg.saveall(tmpfile, ts) with open(tmpfile) as svg_input, open(nameSav, 'wb') as png_output: cairosvg.svg2png(bytestring=svg_input.read(), write_to=png_output) shutil.rmtree(tmpdir) sys.exit()
def convert_to_svg_string(canvas): canvas.update() with tempfile.NamedTemporaryFile(delete=False) as temp_output: temp_name = temp_output.name saveall(temp_name, canvas) # The temp file must be reopened because Windows does not support reading AND writing NamedTemporaryFile. with open(temp_name, 'rb') as temp_output: content = temp_output.read() os.remove(temp_name) return content
def mainLine(): turtle.delay(0) t.speed(10) mi = 0 ma = 0 cur = 0 scale = 1 ran = 20 t.hideturtle() i = 0 for n in array: cur = cur + int(n) i = i + 1 if (mi > cur): mi = cur if (ma < cur): ma = cur noTrace_goto(-400, 0) t.fillcolor('#F6D02F') t.begin_fill() t.forward(1000) step = 20 scale = 600 / (ma - mi) if (scale > 1): scale = int(scale) width = int(ma - mi) if ((ma < 5000) and (ma >= 1000)): step = 100 ran = 500 if ((ma < 1000) and (ma >= 500)): step = 40 ran = 200 if ((ma < 500) and (ma >= 150)): step = 20 ran = 100 if ((ma < 150) and (ma >= 90)): step = 10 ran = 50 if ((ma < 90) and (ma >= 0)): step = 5 ran = 20 if (ma >= 5000): step = 500 ran = 2000 flag = 0 mm = 0 for ii in array: if (abs(ii) >= int(step / 5)): flag = flag + 1 mm = ii if (flag == 1): i = 1 ma = mm if (i == 1): if ((ma < 1000) and (ma > 500)): step = 40 ran = 200 if ((ma < 500) and (ma > 150)): step = 20 ran = 100 if ((ma < 150) and (ma > 90)): step = 10 ran = 50 if ((ma < 90) and (ma > 0)): step = 5 ran = 20 if (ma > 1000): step = 100 ran = 500 xx = 0 nn = ma scale = 1 offset = ma % step xx = xx - offset * scale dx = 100 nn = nn - offset while (xx > -380): xx = xx - dx * scale nn = nn - step start = nn while (xx < 350): drawStepp(xx, nn) xx = xx + dx * scale nn = nn + step end = nn t.pencolor('#000000') noTrace_goto(xx, 5) t.seth(270) t.forward(10) noTrace_goto(xx, -20) t.write(str(end), font=("Arial", 12, "normal")) noTrace_goto(0, 20) t.dot(10, '#0000FF') t.pencolor("#FF0000") noTrace_goto(-300, -120) t.write("start : %d, end: %d , range: %d[step: %d]" % (start, end, ran, step), font=("Arial", 20, "normal")) noTrace_goto(-300, -150) t.write("Single Dot", font=("Arial", 20, "normal")) else: # scale = int(math.ceil(600/(ma-mi))) zero = int(int(0 - mi) * scale + (-300)) noTrace_goto(zero, 0) width = int(ma - mi) if ((width < 5000) and (width > 1000)): step = 100 ran = 500 if ((width < 1000) and (width > 500)): step = 40 ran = 200 if ((width < 500) and (width > 150)): step = 20 ran = 100 if ((width < 150) and (width > 90)): step = 10 ran = 50 if ((width < 90) and (width > 0)): step = 5 ran = 20 if (width > 5000): step = 500 ran = 2000 xx = zero - int(step * scale) nn = -step noTrace_goto(xx, 0) while (xx <= int(ma * scale + zero)): if (nn % ran == 0): drawStepp(xx, nn) else: drawStep(xx) nn = nn + step xx = xx + int(step * scale) start = mi end = nn + step t.pencolor('#000000') noTrace_goto(xx, 5) t.seth(270) t.forward(10) if (xx > 600): noTrace_goto(600, 0) t.seth(0) t.forward(xx - 600 + 100) noTrace_goto(xx, -20) t.write(str(end), font=("Arial", 12, "normal")) noTrace_goto(zero, 0) for i in array: if (i > 0): plus(i, scale) else: minus(-i, scale) drawDot(2) t.pencolor("#FF0000") noTrace_goto(-300, -150) t.write("start : %d, end: %d , range: %d[step: %d]" % (start, end, ran, step), font=("Arial", 20, "normal")) t.hideturtle() nameSav = "1" + '.png' tmpdir = tempfile.mkdtemp() tmpfile = os.path.join(tmpdir, 'tmp.svg') ts = turtle.getcanvas() canvasvg.saveall(tmpfile, ts) with open(tmpfile) as svg_input, open(nameSav, 'wb') as png_output: cairosvg.svg2png(bytestring=svg_input.read(), write_to=png_output) shutil.rmtree(tmpdir) sys.exit()
def saveImg(name): name = name + ".svg" ts = t.getscreen().getcanvas() canvasvg.saveall(name, ts)
def convert_to_svg_string(canvas): canvas.update() with tempfile.NamedTemporaryFile() as temp_output: saveall(temp_output.name, canvas) return open(temp_output.name, "rb").read()