In [105]:
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
In [2]:
!du -sh *
336K	24nov21-1.PNG
4.6M	file.svg
22M	file2.svg
1.3M	images
6.2M	images2
452K	main-category.ipynb
452K	main.ipynb
In [3]:
#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
In [4]:
cols = 5
padding = 100
images = [
   *[f"./images2/{filename}" for filename in os.listdir("./images2/")]
]
In [5]:
images
Out[5]:
['./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/download.jfif',
 './images2/.ipynb_checkpoints',
 './images2/f7aee8753832af613b63e51d5f07011a.jpg',
 './images2/Uma-versao-Android-do-Wallpaper-Engine-estara-disponivel-em-breve.jpg',
 './images2/c3b56294437947.5e811d4b7d65e.jpg',
 './images2/wallpapersden.com_cool-4k-pattern_3840x2160.jpg']
In [16]:
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'
    ]
}
In [126]:
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
./images2/photo-1602498456745-e9503b30470b.jfif 1000 1500 100 0
./images2/Liam_Wong_Tokyo_Nights_Phone_Wallpapers_Cyberpunk_Blade_Runner_TOKYOO_TO_KY_OO_Japan_BookMinutes+To+Midnight.jpg 1000 2165 1200 0
./images2/1069078.jpg 3840 2160 2300 0
./images2/8cb5ee0a8fc8dd21497f0c2d0ebe1238.jpg 720 1440 6240 0
./images2/f7aee8753832af613b63e51d5f07011a.jpg 736 1308 7060 0
./images2/Uma-versao-Android-do-Wallpaper-Engine-estara-disponivel-em-breve.jpg 1920 1080 100 2265
./images2/f7aee8753832af613b63e51d5f07011a.jpg 736 1308 2120 2265
./images2/Uma-versao-Android-do-Wallpaper-Engine-estara-disponivel-em-breve.jpg 1920 1080 2956 2265
./images2/download.jfif 1920 1080 100 0
./images2/f7aee8753832af613b63e51d5f07011a.jpg 736 1308 100 0
./images2/Uma-versao-Android-do-Wallpaper-Engine-estara-disponivel-em-breve.jpg 1920 1080 936 0
./images2/c3b56294437947.5e811d4b7d65e.jpg 900 900 100 0
./images2/wallpapersden.com_cool-4k-pattern_3840x2160.jpg 3840 2160 1100 0
In [127]:
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>
    """)
In [ ]:
 
In [3]:
img = Image.open("./images/Slide1.JPG")
In [5]:
img.width
Out[5]:
1280
In [6]:
img.height
Out[6]:
720
In [13]:
b64 = img_to_base64_str(img)
In [17]:
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>
    """)
In [107]:
Image2("./30nov21-1.png")
Out[107]:
In [8]:
base64.encode(img)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-42d321904993> in <module>
----> 1 base64.encode(img)

TypeError: encode() missing 1 required positional argument: 'output'
In [ ]: