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.

2
  • Please provide enough code so others can better understand or reproduce the problem. Commented Sep 6, 2021 at 15:53
  • Thank you. Please see the updated question with code. Commented Sep 10, 2021 at 14:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.