In [ ]:
from glob import glob
from io import BytesIO
from PIL import Image
from PIL.ImageDraw import Draw
from IPython.core import display
def overlay(img="vis_1404051745G13I01.png"):
overlay = Image.open("fulldisk_1006011745_G13I04_coasts.png")
overlay = overlay.convert("RGBA")
datas = overlay.getdata()
newData = []
for item in datas:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
newData.append((255, 255, 255, 0))
else:
newData.append(item)
overlay.putdata(newData)
background = Image.open(img)
background = background.convert("RGBA")
background.paste(overlay, (0, 0), overlay)
return background
def html_fig_table(images, ncols=2, nrows=2):
if ncols * nrows != len(images):
raise ValueError("ncols x nrows must be equal the number of images!")
unit = ["<th><img src='%s'/></th>"]
row = ['<tr>\n%s\n</tr>' % '\n'.join(unit*ncols)]
table = """<table>%s</table>""" % '\n'.join(row*nrows)
return table % tuple(images)
def display_pil_image(im):
"""Displayhook function for PIL Images, rendered as PNG."""
b = BytesIO()
im.save(b, format='png')
data = b.getvalue()
ip_img = display.Image(data=data, format='png', embed=True)
return ip_img._repr_png_()
# Register display func with PNG formatter.
png_formatter = get_ipython().display_formatter.formatters['image/png']
dpi = png_formatter.for_type(Image.Image, display_pil_image)
In [ ]:
images = glob('./images/*.png')
for img in images:
tmp = overlay(img)
tmp.save('./overlays/' + img.split('.')[0] + '_overlay.png')
In [ ]:
import base64
from IPython.display import display, HTML
images = []
for img in glob("./overlays/vis*.png"):
with open(img, "rb") as f:
base64_data = base64.b64encode(f.read()).decode("ascii")
images.append('data:image/png;base64,{0}'.format(base64_data))
table = html_fig_table(images, ncols=2, nrows=2)
In [ ]:
display(HTML(table))