用清晰的文本生成低res图像

发布于 2025-01-24 17:15:41 字数 2350 浏览 1 评论 0 原文

我正在尝试创建一个表格(宽度= 190px,高度= 128px),该表装有一些文本并将其保存为图像。我设法使用matplotlib和简单地生成HTML表来做到这一点,但是,通过这两种方法,我都将文本“压缩”并且模糊的问题运行。 模糊字母

我想生成一个像这样的图片 32x16 pick in Paint 。实现这一目标的最佳方法是什么。目前,我正在使用Python生成HTML表,然后使用HTML2Image库保存它。该表需要以编程方式生成。此外,我尝试仅对阈值以上的某些像素进行采样。图像可以保存为.pgn,.jpg或.bmp,任何建议都将不胜感激!下面的代码是我如何生成和保存表atm的方式。

def printTable(data):
    htmlTable = ""
    htmlTable += '<table>'
    htmlTable += '<tr>'
    htmlTable += '<td>%s</td>'% "NAME"
    htmlTable += '<td>%s</td>'% "LAST NAME"
    htmlTable += '<td>%s</td>'% "PASSWORD"
    htmlTable += '<td>%s</td>'% "USERNAME" 
    htmlTable+= '</tr>'
    
    for element in data.itertuples():
        htmlTable += '<tr>'
        htmlTable += ('<td>%s</td>'% element.NAME)
        htmlTable += ('<td>%s</td>'% element.LAST NAME)
        htmlTable += ('<td>%s</td>'% element.PASSWORD) 
        htmlTable += ('<td>%s</td>'% element.USERNAME) 
        htmlTable += '</tr>'
    htmlTable += '</table>'
    return htmlTable

tableString = printTable(df1)

print(tableString)

from html2image import Html2Image
hti = Html2Image(size=(190, 128))
#hti = Html2Image()

html = tableString
css = "\
      @font-face {\
          font-family: myFirstFont;\
          src: url(Dogica Pixel Regular.ttf);\
      }\
        table, th, td{ \
        border:1px solid red; \
        border-collapse: collapse; \
        background-color: black; \
        color:red \
      }\
      table{ \
        widht: 190px;\
        font-size: 8px;\
        font-family:myFirstFont\
      }\
      "

hti.screenshot(html_str=html, css_str=css, save_as='htmlTable.png')

原始图像 缩放图像

PS我真的希望手绘制画布上的每个像素不是唯一的解决方案XD。

I'm trying to create a table (width = 190px, height = 128px) that is filled with some text and save that as an image. I managed to do that both with matplotlib and by simply generating a HTML table however with both methods I run to the same problem the text gets "compressed" and is blurry. Blurry letters

I would like to generate a pic like this 32x16 pic hand drawn in paint. What would be the best way to achieve this. At the moment I am using python to generate the HTML table and then I am saving it using the html2image library. The table needs to be generated programmatically though. Furthermore I tried to sample only some of the pixels above a threshold but then the text is even worse. The image can be saved as .pgn, .jpg or .bmp Any advice is appreciated! The code below is how I am generating and saving the table ATM.

def printTable(data):
    htmlTable = ""
    htmlTable += '<table>'
    htmlTable += '<tr>'
    htmlTable += '<td>%s</td>'% "NAME"
    htmlTable += '<td>%s</td>'% "LAST NAME"
    htmlTable += '<td>%s</td>'% "PASSWORD"
    htmlTable += '<td>%s</td>'% "USERNAME" 
    htmlTable+= '</tr>'
    
    for element in data.itertuples():
        htmlTable += '<tr>'
        htmlTable += ('<td>%s</td>'% element.NAME)
        htmlTable += ('<td>%s</td>'% element.LAST NAME)
        htmlTable += ('<td>%s</td>'% element.PASSWORD) 
        htmlTable += ('<td>%s</td>'% element.USERNAME) 
        htmlTable += '</tr>'
    htmlTable += '</table>'
    return htmlTable

tableString = printTable(df1)

print(tableString)

from html2image import Html2Image
hti = Html2Image(size=(190, 128))
#hti = Html2Image()

html = tableString
css = "\
      @font-face {\
          font-family: myFirstFont;\
          src: url(Dogica Pixel Regular.ttf);\
      }\
        table, th, td{ \
        border:1px solid red; \
        border-collapse: collapse; \
        background-color: black; \
        color:red \
      }\
      table{ \
        widht: 190px;\
        font-size: 8px;\
        font-family:myFirstFont\
      }\
      "

hti.screenshot(html_str=html, css_str=css, save_as='htmlTable.png')

Original image not scaled
Scaled image
enter image description here

P.S. I really hope hand drawing every pixel on a canvas isn't the only solution xd.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文