In [81]:
from PIL import Image
import base64
import io
import os
from IPython.display import IFrame, SVG, display#, Image
In [97]:
!du -sh *
336K	24nov21-1.PNG
4.6M	file.svg
22M	file2.svg
1.3M	images
6.2M	images2
452K	main.ipynb
In [9]:
#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 [100]:
cols = 5
padding = 100
images = [
   *[f"./images2/{filename}" for filename in os.listdir("./images2/")]
]
size = 0
rowheight = {-1:0}
x = padding
y = padding
svgimages = []
for image in images:
    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)
        svgimages.append(f"""
          <image x="{x}" y="{y}" href="{b64}"/>
        """)
    #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/download.jfif 1920 1080 7060 0
./images2/f7aee8753832af613b63e51d5f07011a.jpg 736 1308 100 2265
./images2/Uma-versao-Android-do-Wallpaper-Engine-estara-disponivel-em-breve.jpg 1920 1080 936 2265
./images2/c3b56294437947.5e811d4b7d65e.jpg 900 900 2956 2265
./images2/wallpapersden.com_cool-4k-pattern_3840x2160.jpg 3840 2160 3956 2265
In [101]:
with open('file2.svg', 'w') as f:
    imgs = "\n".join(svgimages)
    f.write(f"""
    <svg 
      xmlns="http://www.w3.org/2000/svg">
      {imgs}
    </svg>
    """)
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 [78]:
Image("./24nov21-1.PNG")
Out[78]:
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 [ ]: