def plot_dist_lengths(lengths, length_limit, x_step, max_x, y_step, max_y, outimg): """ Plot a length distribution over sentences Inputs: - lengths: list of numerical lenghts - length_limit: maximum allowed length - x_step: step in the horizontal axis - max_x: maximum value in the horizontal axis - y_step: step in the vertical axis - max_y: maximum value in the vertical axis - outimg: output image """ # count number of occurrences for each length value c = Counter(lengths) max_val = min(max_x, max(max_x, max(c.keys()))) # output in svg format xdata = list(range(1, max_val + x_step + 1)) ydata_used = [c[k] if k in c and k <= length_limit else 0 for k in xdata] ydata_unused = [c[k] if k in c and k > length_limit else 0 for k in xdata] line_chart = pygal.StackedBar(width=1300, height=800, x_label_rotation=-45, x_title='Number of words', y_title='Number of sentences', show_minor_x_labels=False) line_chart.x_labels = xdata line_chart.x_labels_major = list(set([x // x_step * x_step for x in xdata])) line_chart.y_labels = range(0, max_y + 1, y_step) line_chart.add('Truncated data', ydata_unused) line_chart.add('Unchanged data', ydata_used) # circumvent potential svg styling problems line_chart.render_to_file(outimg) cairosvg.svg2svg(url=outimg, write_to=outimg)
def fix_file(file): try: # acommodating for all of the broken symlinks fileobject = open(file, "r+") fileobject.close() except FileNotFoundError: return None else: try: svg2svg(url=file) except: fileobject = open(file, 'r') filedsmnld = fileobject.readlines() fileobject.close() filedsmnld = ''.join(filedsmnld) filedsmnld = splitter(filedsmnld, [" ", "\n"]) newfile = open(file, "w") in_svgtag = False for i in range(len(filedsmnld)): if filedsmnld[i] == "<svg": in_svgtag = True if filedsmnld[i][-1] == '>' and in_svgtag: filedsmnld.insert( i, 'xmlns:osb="http://www.openswatchbook.org/uri/2009/osb" ' ) break # just in case filedsmnld = ''.join(filedsmnld) newfile.write(filedsmnld) newfile.close()
def main(): create_output_dir(svg_target_dir) for dirpath, dirnames, filenames in os.walk(svg_source_dir): target_dir = os.path.join(svg_target_dir, os.path.relpath(dirpath, svg_source_dir)) if not os.path.isdir(target_dir): os.mkdir(target_dir) for image in filenames: print(f'Processing {image}') cairosvg.svg2svg(url=os.path.join(dirpath, image), write_to=os.path.join(target_dir, image), scale=5) print('Images resized')
def convert_chart_svg(self, request: Request): vert_bar_chart_max_label = int( request.args.get('vertBarChartMaxLabel', '10')) chart_type = request.args.get('chartType') if chart_type in ('bar', 'time', 'timescatter'): svg_src = normalize_bar_chart_svg(request.get_data(), vert_bar_chart_max_label) else: svg_src = normalize_wcloud_svg(request.get_data()) if request.args.get('outFormat', '') == 'png': self._response.set_header('Content-Type', 'image/png') return svg2png(bytestring=svg_src, output_width=1200, background_color='#FFFFFF') elif request.args.get('outFormat', '') == 'png-print': self._response.set_header('Content-Type', 'image/png') return svg2png(bytestring=svg_src, output_width=4961, background_color='#FFFFFF') elif request.args.get('outFormat', '') == 'svg': self._response.set_header('Content-Type', 'image/svg+xml') return svg2svg(bytestring=svg_src, scale=5, background_color='#FFFFFF') elif request.args.get('outFormat', '') == 'pdf': self._response.set_header('Content-Type', 'application/pdf') return svg2pdf(bytestring=svg_src, scale=5, background_color='#FFFFFF') else: raise UserActionException('Invalid data format', code=422)
def save_all(request): if not request.POST and not request.is_ajax: return HttpResponseRedirect(reverse(index)) in_memory = StringIO() zip = ZipFile(in_memory, "a") for name in request.POST: if name == 'csrfmiddlewaretoken': continue try: svg_data = request.POST[name] zip.writestr("%s.svg" % (name), cairosvg.svg2svg(svg_data)) zip.writestr("%s.png" % (name), cairosvg.svg2png(svg_data)) except: print name + " broke while running" pass # fix for Linux zip files read in Windows (I dont know what this actually does) for file in zip.filelist: file.create_system = 0 zip.close() response = HttpResponse(mimetype="application/zip") response["Content-Disposition"] = "attachment; filename=charts.zip" in_memory.seek(0) response.write(in_memory.read()) return response
def __load_svg(filename, _width): _svg = cairosvg.svg2svg( url=filename, dpi=(96 / (_width / 64) )) # Convert svg to svg changing DPI to resize the image _bytes = cairosvg.svg2png(_svg) byte_io = io.BytesIO(_bytes) return pygame.image.load(byte_io)
def plot_dist_tags(sents, vocab, outimg, outfile, padwords=[]): """ Plot the amount of IV and OOV words per tag in a dataset Inputs: - sents: list of sentences represented as a list of (word, tag, sym) items - vocab: set containing words in the vocabulary - outimg: output image - outfile: output text file - padwords: set of words to be ignored """ # compute the number of IV and OOV words per tag count = {} for sent in sents: if sent: for word, tag, sym in sent: if word not in padwords: if tag not in count: count[tag] = [0,0] count[tag][0] += 1 if sym not in vocab: count[tag][1] += 1 # output in svg format xdata = sorted(count.keys(), key=lambda x: count[x][0], reverse=True) ydata_oov = list([count[x][1] for x in xdata]) ydata_rest = list([count[x][0] - count[x][1] for x in xdata]) line_chart = pygal.StackedBar(width=1300, height=800, x_label_rotation=-45, y_title='Number of words') line_chart.x_labels = xdata line_chart.add('OOVs', ydata_oov) line_chart.add('IVs + OOVs', ydata_rest) # circumvent potential svg styling problems line_chart.render_to_file(outimg) cairosvg.svg2svg(url=outimg, write_to=outimg) # output in text format with open(outfile, 'w') as tfile: tfile.write("tag\t#OOVs\t#IVs\t#words\tratio\n") for i in range(len(xdata)): tfile.write(str(xdata[i]) + '\t') tfile.write(str(ydata_oov[i]) + '\t') tfile.write(str(ydata_rest[i]) + '\t') tfile.write(str(ydata_oov[i] + ydata_rest[i]) + '\t') tfile.write(str(ydata_oov[i] / (ydata_oov[i] + ydata_rest[i])) + '\n')
def plot_accuracy(history, keys, labels, test_acc, outimg, outfile): """ Plot the obtained accuracy scores against training epochs Inputs: - history: object obtained from calling Keras fit() function - keys: key values to access the metrics in history - labels: names of the metrics which match `keys` - test_acc: accuracy obtained on the test set - outimg: output image - outfile: output text file """ # build chart hist = pd.DataFrame(history.history) chart = pygal.Line(width=1300, height=800, x_label_rotation=0, x_title='Number of training epochs', y_title='Sem-tagging accuracy') xdata = [x + 1 for x in range(len(hist[keys[0]]))] chart.x_labels = xdata # plot all metrics for i in range(len(keys)): key = keys[i] label = labels[i] chart.add(label, hist[key], show_dots=False, stroke_style={'width': 4, 'dasharray': '3, 8', 'linecap': 'round', 'linejoin': 'round'}) # plot a horizontal line representing accuracy on the test set if test_acc > 0: ytest=[test_acc] * len(xdata) chart.add(None, ytest, show_dots=False, stroke_style={'width': 2}) # output in svg format chart.render_to_file(outimg) cairosvg.svg2svg(url=outimg, write_to=outimg) # output in text format with open(outfile, 'w') as tfile: header = str(keys[0]) + ''.join(["\t" + str(key) for key in keys[1:]]) tfile.write(header + '\n') for i in range(len(hist[keys[0]])): tfile.write(str(hist[keys[0]][i])) for key in keys[1:]: tfile.write("\t" + str(hist[key][i])) tfile.write("\n")
def saveImage(self, session_id, query_str, output_format, data): """ Serve image files for download. """ filename = ".".join((query_str, output_format)) filepath = os.path.join(cherrypy.session.storage_path, session_id, filename) if os.path.exists(filepath): os.remove(filepath) data = data.encode('utf8', 'replace') with open(filepath, 'w') as fout: # write image file if output_format == 'svg': cairosvg.svg2svg(bytestring=data, write_to=fout) elif output_format == 'png': cairosvg.svg2png(bytestring=data, write_to=fout) elif output_format == 'pdf': cairosvg.svg2pdf(bytestring=data, write_to=fout) else: pass return serve_file(filepath, 'application/x-download', 'attachment')
def route_save_image(): image_format = flask.request.form['format'] svg = flask.request.form['svg'] if image_format == 'pdf': data = cairosvg.svg2pdf(bytestring=svg) mimetype = 'application/pdf' elif image_format == 'png': data = cairosvg.svg2png(bytestring=svg) mimetype = 'image/png' elif image_format == 'ps': data = cairosvg.svg2ps(bytestring=svg) mimetype = 'application/postscript' elif image_format == 'svg': data = cairosvg.svg2svg(bytestring=svg) mimetype = 'image/svg+xml' elif image_format == 'raw-svg': data = svg mimetype = 'image/svg+xml' else: flask.abort(400) return flask.Response(data, mimetype=mimetype)
def panel(request): if len(request.POST) == 0: return redirect(prog_type+'/HitWalker2/') elif request.POST.has_key("output_format") and request.POST.has_key("data"): try: import cairosvg #and all the somewhat invisible dependencies needed to use external css styling on the svgs... import lxml import tinycss import cssselect if request.POST["plot_type"] == 'svg': use_svg = '<?xml-stylesheet type="text/css" href="' + request.session['new_css_path'] + '" ?>' + request.POST["data"] else: if request.POST['panel_context'] == 'image': width = str(config.pathway_sizes['w'] + config.pathway_sizes['legend_offset']) height = str(config.pathway_sizes['h']) transl = 'translate('+str(config.pathway_sizes['legend_offset'])+',0)' elif request.POST['panel_context'] == 'panel': if request.POST["plot_type"] == 'g1': width=str(config.network_sizes['w']+config.network_sizes['legend_offset']) transl = 'translate('+str(config.pathway_sizes['legend_offset'])+',0)' elif request.POST["plot_type"] == 'siRNA_plot': width=str((config.network_sizes['w']*2)+50) transl='translate(50,0)' else: raise Exception('Unknown plot_type') height = str(config.network_sizes['h']) else: raise Exception('Unknown value for panel_context') use_data = '<svg xmlns="http://www.w3.org/2000/svg" width="'+width+'" height="'+height+'" >' + request.POST["data"].replace('xmlns="http://www.w3.org/2000/svg"', "") + '</svg>' import xml.etree.ElementTree as ET tree = ET.fromstring(use_data) #ensure everything will fit on screen #assuming only one non-legend g for now tree.find("./*[@class=\'"+request.POST["plot_type"]+"\']").attrib['transform'] = transl sub_tree = tree.find(".//*[@class='BorderRect BorderSelected']/..") if sub_tree != None: tree.find(".//*[@class='BorderRect BorderSelected']/..").remove(tree.find(".//*[@class='BorderRect BorderSelected']")) use_svg = '<?xml-stylesheet type="text/css" href="' + request.session['new_css_path'] + '" ?>' + ET.tostring(tree) temp_file = tempfile.mktemp() #temp_file = '/Users/bottomly/Desktop/test_image.xml' temp_inp = open(temp_file, "w") temp_inp.write(use_svg) temp_inp.close() if request.POST["output_format"] == "svg": response = HttpResponse(cairosvg.svg2svg(url=temp_file), content_type='image/svg+xml') response['Content-Disposition'] = 'attachment; filename="HitWalker2.svg"' elif request.POST["output_format"] == "pdf": response = HttpResponse(cairosvg.svg2pdf(url=temp_file), content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="HitWalker2.pdf"' return response except ImportError, e: raise e response = HttpResponse('<?xml-stylesheet type="text/css" ?>' + request.POST["data"], content_type='image/svg+xml') response['Content-Disposition'] = 'attachment; filename="HitWalker2.svg"' return response
def convert_svg(svg, file): if file == 'png': svg2png(bytestring=svg, write_to='badge.png') else: svg2svg(bytestring=svg, write_to='badge.svg')
#!/usr/bin/env python # -*- coding: utf-8 -*- import cairosvg # import source svg at 20x20px original size appIcon = cairosvg.svg2svg(bytestring=open("AppIcon.svg").read().encode('utf-8')) icon_dir = "Images.xcassets/AppIcon.appiconset/" # convert for 20x20 cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"AppIcon20x20~ipad.png") cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"AppIcon20x20@2x~ipad.png", scale=2) cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"*****@*****.**", scale=2) cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"*****@*****.**", scale=3) # convert for 29x29 appIcon = cairosvg.svg2svg(bytestring=appIcon, scale=1.45) cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"*****@*****.**", scale=2) cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"*****@*****.**", scale=3) cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"AppIcon29x29~ipad.png") cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"AppIcon29x29@2x~ipad.png", scale=2) # convert for 40x40 appIcon = cairosvg.svg2svg(bytestring=appIcon, scale=1.38) cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"*****@*****.**", scale=2) cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"*****@*****.**", scale=3) cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"AppIcon40x40~ipad.png") cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"AppIcon40x40@2x~ipad.png", scale=2) # convert for 60x60 appIcon = cairosvg.svg2svg(bytestring=appIcon, scale=1.5)
def save(self, fp, **params): tree = etree.parse(self.template_path) d = self.data with open(d['recipient_image'], 'rb') as recipient_image: image_data = recipient_image.read() image_mime_type = magic.from_buffer(image_data, mime=True) image_base_64 = base64.b64encode(image_data) el_image = tree.xpath(self.el_selectors['recipient_image'], namespaces=NSMAP)[0] el_image.attrib[ '{http://www.w3.org/1999/xlink}href'] = 'data:{};base64,{}'.format( image_mime_type, image_base_64.decode('utf-8')) recipient_name_parts = d['recipient_name'].split() if len(recipient_name_parts) > 3: recipient_name_part_1 = ' '.join(recipient_name_parts[:3]) recipient_name_part_2 = ' '.join(recipient_name_parts[3:]) else: recipient_name_part_1, recipient_name_part_2 = name, '' el_r_name_part_1 = tree.xpath( self.el_selectors['recipient_name_part_1'], namespaces=NSMAP)[0] el_r_name_part_1.text = recipient_name_part_1.upper() el_r_name_part_2 = tree.xpath( self.el_selectors['recipient_name_part_2'], namespaces=NSMAP)[0] el_r_name_part_2.text = recipient_name_part_2.upper() el_r_blood_type = tree.xpath(self.el_selectors['recipient_image'], namespaces=NSMAP)[0] el_r_blood_type.text = self.data['recipient_blood_type'].upper() el_l_name = tree.xpath(self.el_selectors['location_name'], namespaces=NSMAP)[0] el_l_name.text = self.data['location_name'].upper() el_l_address_part_1 = tree.xpath( self.el_selectors['location_address_part_1'], namespaces=NSMAP)[0] el_l_address_part_1.text = '{}, {}'.format( self.data['location_address_street'].upper(), self.data['location_address_number'].upper()) el_l_address_part_2 = tree.xpath( self.el_selectors['location_address_part_2'], namespaces=NSMAP)[0] el_l_address_part_2.text = '{}, {} - {}, {}'.format( self.data['location_address_district'].upper(), self.data['location_address_locality'].upper(), self.data['location_address_region'].upper(), self.data['location_address_postal_code'].upper(), ) file_format = fp.rpartition('.')[-1].lower() if file_format == 'png': cairosvg.svg2png(bytestring=etree.tostring(tree), write_to=fp) if file_format == 'pdf': cairosvg.svg2pdf(bytestring=etree.tostring(tree), write_to=fp) if file_format == 'ps': cairosvg.svg2ps(bytestring=etree.tostring(tree), write_to=fp) if file_format == 'svg': cairosvg.svg2svg(bytestring=etree.tostring(tree), write_to=fp) _d = d.copy() (_d.pop(k) for k in 'recipient_name recipient_image'.split()) logger.info({ 'type': 'banner_generated', 'data': { 'donation': _d, '_meta': { 'file_format': file_format, } }, }) return True error_message = ''' "{}" is invalid file extension! The supported extension is "png". "pdf", "ps" and "svg". ''' raise ValueError(error_message.format(fp))
) else: fileName = sys.argv[1] buffer = readWillProtobuff(fileName + '.will') for i, n in zip(buffer, range(99)): print(n) (xData, yData, lineData) = processBuffer(i) plotXYLineData(xData, yData, lineData) svgStr = XYLineDataToSVG(xData, yData, lineData) datei_name = fileName + '_' + str(n).zfill(2) + '.svg' svg2svg(bytestring=svgStr, write_to=datei_name) run(['svgtoipe', datei_name], shell=False) # with open(fileName +'_'+str(n).zfill(2)+ '.svg','wt') as f: # f.write(svgStr) # buffer = readWillProtobuff(fileName + '.will') # # # (xData, yData, lineData) = processBuffer(buffer) # # # plotXYLineData(xData, yData, lineData) # # svgStr = XYLineDataToSVG(xData, yData, lineData) #
def save(self, uri_dest, **kargs): tree = etree.parse(self.template_path) d = self.data with open(d['recipient_image'], 'rb') as recipient_image: image_data = recipient_image.read() image_mime_type = magic.from_buffer(image_data, mime=True) image_base_64 = base64.b64encode(image_data) el_image = tree.xpath(self.el_selectors['recipient_image'], namespaces=NSMAP)[0] el_image.attrib[ '{http://www.w3.org/1999/xlink}href'] = 'data:{};base64,{}'.format( image_mime_type, image_base_64.decode('utf-8')) recipient_name_parts = d['recipient_name'].split() if len(recipient_name_parts) > 3: recipient_name_part_1 = ' '.join(recipient_name_parts[:3]) recipient_name_part_2 = ' '.join(recipient_name_parts[3:]) else: recipient_name_part_1, recipient_name_part_2 = ' '.join( recipient_name_parts), '' el_r_name_part_1 = tree.xpath( self.el_selectors['recipient_name_part_1'], namespaces=NSMAP)[0] el_r_name_part_1.text = recipient_name_part_1.upper() el_r_name_part_2 = tree.xpath( self.el_selectors['recipient_name_part_2'], namespaces=NSMAP)[0] el_r_name_part_2.text = recipient_name_part_2.upper() el_r_blood_type = tree.xpath(self.el_selectors['recipient_bloodtype'], namespaces=NSMAP)[0] el_r_blood_type.text = self.data['recipient_bloodtype'].upper() el_l_name = tree.xpath(self.el_selectors['location_name'], namespaces=NSMAP)[0] el_l_name.text = self.data['location_name'].upper() el_l_address_part_1 = tree.xpath( self.el_selectors['location_address_part_1'], namespaces=NSMAP)[0] el_l_address_part_1.text = '{}, {}'.format( self.data['location_address_street'].upper(), self.data['location_address_number'].upper()) el_l_address_part_2 = tree.xpath( self.el_selectors['location_address_part_2'], namespaces=NSMAP)[0] el_l_address_part_2.text = '{}, {} - {}, {}'.format( self.data['location_address_district'].upper(), self.data['location_address_locality'].upper(), self.data['location_address_region'].upper(), self.data['location_address_postal_code'].upper(), ) uri = urlparse(uri_dest) file_format = uri.path.rpartition('.')[-1].lower() file_args = {'uid': self.uid, 'format': file_format} if uri.scheme == 'gs': fp = '/tmp/hematopy-img-{uid}.{format}'.format(**file_args) else: fp = uri.path.format(**file_args) if file_format == 'png': cairosvg.svg2png(bytestring=etree.tostring(tree), write_to=fp) if file_format == 'pdf': cairosvg.svg2pdf(bytestring=etree.tostring(tree), write_to=fp) if file_format == 'ps': cairosvg.svg2ps(bytestring=etree.tostring(tree), write_to=fp) if file_format == 'svg': cairosvg.svg2svg(bytestring=etree.tostring(tree), write_to=fp) if uri.scheme == 'gs': client = storage.storage_gc.Client() file_path = uri.path.format(**file_args) with open(fp, 'rb') as file: object_stream = storage.GCSObjectStreamUpload( client, bucket_name=uri.netloc, blob_name=file_path, ) def file_read_chunked(file, chunk_size): return iter(lambda: file.read(chunk_size), '') with open(fp, 'rb') as file: with object_stream as obj_stream: file_chunks = file_read_chunked( file, obj_stream._chunk_size) for data in file_chunks: obj_stream.write(data) if data == b'': break os.remove(fp) _d = d.copy() (_d.pop(k) for k in 'recipient_name recipient_image'.split()) logger.info({ 'type': 'banner_generated', 'data': { 'donation': _d, '_meta': { 'file_format': file_format, } }, }) return True, error_message = ''' "{}" is invalid file extension! The supported extension is "png". "pdf", "ps" and "svg". ''' raise ValueError(error_message.format(fp))
#!/usr/bin/env python # -*- coding: utf-8 -*- import cairosvg import os, errno # import source svg at 20x20px original size appIcon = cairosvg.svg2svg(bytestring=open("AppIcon.svg").read().encode('utf-8')) # render for 48x48 icon_dir = "mdpi/" os.makedirs(icon_dir, exist_ok=True) cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"ic_launcher.png") # render for 72x72 appIcon = cairosvg.svg2svg(bytestring=appIcon, scale=1.5) icon_dir = "hdpi/" os.makedirs(icon_dir, exist_ok=True) cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"ic_launcher.png") # render for 96x96 appIcon = cairosvg.svg2svg(bytestring=appIcon, scale=1.34) icon_dir = "xhdpi/" os.makedirs(icon_dir, exist_ok=True) cairosvg.svg2png(bytestring=appIcon, write_to=icon_dir+"ic_launcher.png") # render for 144x144 appIcon = cairosvg.svg2svg(bytestring=appIcon, scale=1.5) icon_dir = "xxhdpi/" os.makedirs(icon_dir, exist_ok=True)