In [1]:
from PIL import Image
import base64
import io
In [ ]:
 
In [ ]:
 
In [2]:
with open("img.png", "rb") as f:
    b64str = base64.b64encode(f.read())
In [3]:
data = f"data:image/png;base64,{b64str.decode()}"
data2 = b64str.decode()
In [ ]:
 
In [4]:
imagedata = base64.b64decode(data2)
buf = io.BytesIO(imagedata)
img = Image.open(buf)
In [12]:
print(img)
#https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=506x525 at 0x7F6BE2E60DA0>
In [5]:
img.size
Out[5]:
(506, 525)
In [6]:
img.width
Out[6]:
506
In [7]:
img.height
Out[7]:
525
In [8]:
size = 128, 128
In [9]:
display(img)
In [10]:
img.thumbnail(size, Image.ANTIALIAS)
In [11]:
display(img)
In [19]:
img.save('img2.png', 'PNG')
In [ ]:
 
In [12]:
img2 = Image.open("imgW.png")
In [13]:
display(img2)
In [14]:
img2.thumbnail(size, Image.ANTIALIAS)
In [15]:
display(img2)
In [ ]: