Skip to content

Instantly share code, notes, and snippets.

@stas-sl
Last active June 12, 2024 07:55
Show Gist options
  • Select an option

  • Save stas-sl/3f8e14de912bb738d808d1aa0838df5c to your computer and use it in GitHub Desktop.

Select an option

Save stas-sl/3f8e14de912bb738d808d1aa0838df5c to your computer and use it in GitHub Desktop.

Revisions

  1. stas-sl revised this gist Nov 9, 2017. 2 changed files with 473 additions and 37 deletions.
    473 changes: 473 additions & 0 deletions pandas_image.ipynb
    473 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    37 changes: 0 additions & 37 deletions pandas_image.py
    Original file line number Diff line number Diff line change
    @@ -1,37 +0,0 @@
    import glob
    import random
    import base64
    import pandas as pd

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

    pd.set_option('display.max_colwidth', -1)

    def get_thumbnail(path):
    i = Image.open(path)
    i.thumbnail((150, 150), Image.LANCZOS)
    return i

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

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

    dogs = pd.read_csv('../input/labels.csv')
    dogs = dogs.sample(20)
    dogs['file'] = dogs.id.map(lambda id: f'../input/train/{id}.jpg')
    dogs['image'] = dogs.file.map(lambda f: get_thumbnail(f))
    dogs.head()

    # displaying PIL.Image objects embedded in dataframe
    HTML(dogs[['breed', 'image']].to_html(formatters={'image': image_formatter}, escape=False))

    # display images specified by path
    HTML(dogs[['breed', 'file']].to_html(formatters={'file': image_formatter}, escape=False))
  2. stas-sl created this gist Nov 9, 2017.
    37 changes: 37 additions & 0 deletions pandas_image.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    import glob
    import random
    import base64
    import pandas as pd

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

    pd.set_option('display.max_colwidth', -1)

    def get_thumbnail(path):
    i = Image.open(path)
    i.thumbnail((150, 150), Image.LANCZOS)
    return i

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

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

    dogs = pd.read_csv('../input/labels.csv')
    dogs = dogs.sample(20)
    dogs['file'] = dogs.id.map(lambda id: f'../input/train/{id}.jpg')
    dogs['image'] = dogs.file.map(lambda f: get_thumbnail(f))
    dogs.head()

    # displaying PIL.Image objects embedded in dataframe
    HTML(dogs[['breed', 'image']].to_html(formatters={'image': image_formatter}, escape=False))

    # display images specified by path
    HTML(dogs[['breed', 'file']].to_html(formatters={'file': image_formatter}, escape=False))