from PIL import Image
import base64
import io
import os
from IPython.display import IFrame, SVG, display#, Image
from IPython.display import Image as Image2
!du -sh *
#https://stackoverflow.com/questions/48229318/how-to-convert-image-pil-into-base64-without-saving/48229407 #by @Taha Mahjoubi
def img_to_base64_str(img):
buffered = io.BytesIO()
img.save(buffered, format="PNG")
buffered.seek(0)
img_byte = buffered.getvalue()
img_str = "data:image/png;base64," + base64.b64encode(img_byte).decode()
return img_str
def img_from_base64_str(msg):
msg = msg.replace("data:image/png;base64,", "")
msg = base64.b64decode(msg)
buf = io.BytesIO(msg)
img = Image.open(buf)
return img
cols = 5
padding = 100
images = [
*[f"./images2/{filename}" for filename in os.listdir("./images2/")]
]
images
images_cat = {
'Category1': [
'./images2/photo-1602498456745-e9503b30470b.jfif',
'./images2/Liam_Wong_Tokyo_Nights_Phone_Wallpapers_Cyberpunk_Blade_Runner_TOKYOO_TO_KY_OO_Japan_BookMinutes+To+Midnight.jpg',
'./images2/1069078.jpg',
'./images2/8cb5ee0a8fc8dd21497f0c2d0ebe1238.jpg',
'./images2/f7aee8753832af613b63e51d5f07011a.jpg',
'./images2/Uma-versao-Android-do-Wallpaper-Engine-estara-disponivel-em-breve.jpg',
'./images2/f7aee8753832af613b63e51d5f07011a.jpg',
'./images2/Uma-versao-Android-do-Wallpaper-Engine-estara-disponivel-em-breve.jpg',
],
'Category2': [
'./images2/download.jfif',
],
'Category3': [
'./images2/f7aee8753832af613b63e51d5f07011a.jpg',
'./images2/Uma-versao-Android-do-Wallpaper-Engine-estara-disponivel-em-breve.jpg',
],
'Category4': [
'./images2/c3b56294437947.5e811d4b7d65e.jpg',
'./images2/wallpapersden.com_cool-4k-pattern_3840x2160.jpg'
]
}
rowheight = {-1:0}
x = padding
y = padding
yprev = y
svgimages = []
for gidy, gallery in enumerate(images_cat):
size = 0
yprev += 2*padding+sum(rowheight.values())
svgimages.append(f"""
<text x="{padding}" y="{yprev}" font-size="200px">{gallery}</text>
""")
yprev += padding
rowheight = {-1:0}
x = padding
for image in images_cat[gallery]:
if '.ipynb_checkpoints' in image: continue
img = Image.open(image)
b64 = img_to_base64_str(img)
rowid = int(size/cols)
#print(rowid, size%cols)
y = sum([rowheight[i] for i in range(rowid)])
if size%cols == 0:
x = padding
rowheight[rowid] = max(rowheight.get(rowid, 0), img.height+padding)
if 1:
print(image, img.width, img.height, x, y)
b64html = f"""
<span style='color:black; font-weight:bold;'>{image}</span>
"""
b64html = base64.b64encode(b64html.encode('utf8')).decode('utf8')
link = 'https://pt.aliexpress.com/campaign/wow/gcp/superdeal-g/index?spm=a2g0o.home.superdeal.4.31c81c911IUU7s&productIds=4000994012585&anchor=2'
b64link = base64.b64encode(link.encode('utf8')).decode('utf8')
svgimages.append(f"""
<image x="{x}" y="{yprev+y}" href="{b64}" style="outline: 1px solid black;"
onclick="window.open(atob('{b64link}'), '_blank')"
onmousemove="
console.log(d3);
console.log(event, d3, window.d3);
d3.select('#note')
.style('top', event.pageY+20)
.style('left', event.pageX+20)
.style('opacity', 0.8)
.html(atob('{b64html}'))
"
onmouseout="d3.select('#note').style('opacity', 0)"
/>
""")
#print(x,y)
x += img.width + padding
size += 1
with open('file-dash.svg', 'w') as f:
imgs = "\n".join(svgimages)
f.write(f"""
<svg
xmlns="http://www.w3.org/2000/svg">
{imgs}
</svg>
""")
img = Image.open("./images/Slide1.JPG")
img.width
img.height
b64 = img_to_base64_str(img)
with open('file.svg', 'w') as f:
f.write(f"""
<svg
xmlns="http://www.w3.org/2000/svg">
<image x="200" y="200" href="{b64}"/>
</svg>
""")
Image2("./30nov21-1.png")
base64.encode(img)