2

Using the one of the answers provided here: [https://stackoverflow.com/questions/46107348/how-to-display-image-stored-in-pandas-dataframe][1]

I am able to call image_formatter function in IPython.display HTML function to display thumbnail pngs in the dataframe.

I now want to export this HTML formatted table to a pdf file but haven't really found any helpful links to this effect. Any help will be appreciated!


import glob
import random
import base64
import pandas as pd
import os

from PIL import Image
from io import BytesIO
from IPython.display import HTML


#display inline images in pandas DataFrame

#source: https://www.kaggle.com/stassl/displaying-inline-images-in-pandas-dataframe

pd.set_option('display.max_colwidth', None)

def get_thumbnail(path):
    i = Image.open(path)
    return i

def image_base64(im):
    if isinstance(im, str):
        im = get_thumbnail(im)
    with BytesIO() as buffer:
        im.save(buffer, 'png')
        return base64.b64encode(buffer.getvalue()).decode()

def image_formatter(im):
    return f'<img src="data:image/jpeg;base64,{image_base64(im)}">'

To implement the above functions:

data = *path to csv containing a series of png paths & other details*
df=pd.DataFrame(data)
cols_2_keep= ['patient_id', "exam_id", "file_path"]
df = df[cols_2_keep]

images= HTML(df[['patient_id', "exam_id","file"]].to_html(formatters={'file': image_formatter}, escape=False))

#call images to look at HTML display
images

This, as expected, displays the images within the dataframe. What I'm now trying to do is export this dataframe (with images in place) into a pdf file.

Subomi
  • 21
  • 4

0 Answers0