Created
July 29, 2024 13:53
-
-
Save arash77/df994b093023e3acb7053147df8871d3 to your computer and use it in GitHub Desktop.
This Python script generates an image of a table containing formatted text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from PIL import Image, ImageDraw, ImageFont | |
from io import BytesIO | |
def create_image_with_text(text): | |
lines = text.split("\n") | |
lines = [line for line in lines if line.strip()] | |
header = lines[0] | |
data = [line.split(": ", 1) for line in lines if ": " in line] | |
font_url = "https://fonts.gstatic.com/s/roboto/v27/KFOmCnqEu92Fr1Mu4mxP.ttf" | |
response = requests.get(font_url) | |
response.raise_for_status() | |
header_font = ImageFont.truetype(BytesIO(response.content), 40) | |
font = ImageFont.truetype(BytesIO(response.content), 30) | |
temp_image = Image.new("RGB", (1, 1), "white") | |
draw = ImageDraw.Draw(temp_image) | |
max_label_width = 0 | |
max_value_width = 0 | |
row_height = 0 | |
header_bbox = draw.textbbox((0, 0), header, font=header_font) | |
for label, value in data: | |
label_bbox = draw.textbbox((0, 0), label, font=font) | |
value_bbox = draw.textbbox((0, 0), value, font=font) | |
max_label_width = max(max_label_width, label_bbox[2] - label_bbox[0]) | |
max_value_width = max(max_value_width, value_bbox[2] - value_bbox[0]) | |
row_height = max( | |
row_height, label_bbox[3] - label_bbox[1], value_bbox[3] - value_bbox[1] | |
) | |
padding = 10 | |
border_width = 1 | |
num_rows = len(data) | |
total_width = max_label_width + max_value_width + 3 * padding + 2 * border_width | |
total_height = ( | |
(row_height + padding) * num_rows | |
+ 2 * border_width | |
+ padding | |
+ header_bbox[3] | |
+ padding | |
) | |
image = Image.new("RGB", (total_width, total_height), "white") | |
draw = ImageDraw.Draw(image) | |
draw.rectangle( | |
[0, 0, total_width - 1, total_height - 1], outline="black", width=border_width | |
) | |
x = (total_width - header_bbox[2] + header_bbox[0]) // 2 | |
y = border_width + padding | |
draw.text((x, y), header, fill="black", font=header_font) | |
draw.line( | |
[ | |
(border_width, y + header_bbox[3] + padding - 1), | |
(total_width - border_width, y + header_bbox[3] + padding - 1), | |
], | |
fill="black", | |
width=border_width, | |
) | |
y += header_bbox[3] + padding | |
draw.line( | |
[ | |
( | |
max_label_width + 2 * padding + border_width, | |
border_width + header_bbox[3] + 2 * padding, | |
), | |
(max_label_width + 2 * padding + border_width, total_height - border_width), | |
], | |
fill="black", | |
width=border_width, | |
) | |
for label, value in data: | |
draw.text((padding + border_width, y), label, fill="black", font=font) | |
draw.text( | |
(padding + border_width + max_label_width + padding * 2, y), | |
value, | |
fill="black", | |
font=font, | |
) | |
draw.line( | |
[ | |
(border_width, y + row_height + padding - 1), | |
(total_width - border_width, y + row_height + padding - 1), | |
], | |
fill="black", | |
width=border_width, | |
) | |
y += row_height + padding | |
image.save("stats.jpg") | |
text = """ | |
+++Assembly summary+++ | |
Num scaffolds: 203 | |
Total scaffold length: 833835073 | |
Average scaffold length: 4107561.94 | |
Scaffold N50: 36314571 | |
Scaffold auN: 35186726.68 | |
Scaffold L50: 11 | |
Largest scaffold: 46713506 | |
Smallest scaffold: 6737 | |
Num contigs: 674 | |
Total contig length: 833750873 | |
Average contig length: 1237019.10 | |
Contig N50: 7769413 | |
Contig auN: 9481484.12 | |
Contig L50: 32 | |
Largest contig: 28624128 | |
Smallest contig: 141 | |
Num gaps in scaffolds: 471 | |
Total gap length in scaffolds: 84200 | |
Average gap length in scaffolds: 178.77 | |
Gap N50 in scaffolds: 200 | |
Gap auN in scaffolds: 188.12 | |
Gap L50 in scaffolds: 211 | |
Largest gap in scaffolds: 200 | |
Smallest gap in scaffolds: 100 | |
Base composition (A:C:G:T): 246189749:170749526:170284260:246527338 | |
GC content %: 40.90 | |
Num soft-masked bases: 0 | |
Num segments: 674 | |
Total segment length: 833750873 | |
Average segment length: 1237019.10 | |
Num gaps: 471 | |
Num paths: 203 | |
""" | |
create_image_with_text(text) |
Author
arash77
commented
Jul 29, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment