[WIP] random table segments: Table via tabulate and text -> image

This commit is contained in:
Matthias Bisping 2023-01-17 13:23:53 +01:00
parent ca190721d6
commit 54484d9ad0

View File

@ -15,6 +15,7 @@ from PIL import Image, ImageOps, ImageFont, ImageDraw
from PIL.Image import Transpose
from faker import Faker
from matplotlib import pyplot as plt
from tabulate import tabulate
from cv_analysis.utils import star, rconj
from cv_analysis.utils.merging import merge_related_rectangles
@ -451,14 +452,16 @@ class RandomTable(RandomContentRectangle):
import dataframe_image as dfi
df = self.generate_random_dataframe(rectangle, 11)
table_lines = self.generate_random_ascii_table(rectangle, 11)
print(df)
df_styled = df.style.background_gradient()
image = write_lines_to_image(table_lines, rectangle)
buf = io.BytesIO()
dfi.export(df_styled, buf)
image = Image.open(buf)
image = image.resize((rectangle.width, rectangle.height))
# df_styled = df.style.background_gradient()
# buf = io.BytesIO()
# dfi.export(df_styled, buf)
# image = Image.open(buf)
# image = image.resize((rectangle.width, rectangle.height))
self.content = image
@ -478,6 +481,12 @@ class RandomTable(RandomContentRectangle):
return df
def generate_random_ascii_table(self, rectangle: Rectangle, font_size: int):
df = self.generate_random_dataframe(rectangle, font_size)
text_table = tabulate(df, headers="keys", tablefmt="psql")
lines = text_table.split("\n")
return lines
def dump_plt_to_image(rectangle):
buf = io.BytesIO()