A
A
Artem Kondratsky2019-12-08 11:31:37
Python
Artem Kondratsky, 2019-12-08 11:31:37

How to generate an empty image?

Hello! The frontend uses an empty image (as a placeholder so that the content does not move out when loading images) for lazy load loading of images. There is a service that generates this empty image. Using its API is unwise, because. extra requests from the backend server. How can I generate this empty image using python?

empty image example

изображение 100х50:
data:image/gif;base64,R0lGODlhZAAyAIAAAP///wAAACH5BAEAAAEALAAAAABkADIAAAJNjI+py+0Po5y02ouz3rz7D4biSJbmiabqyrbuC8fyTNf2jef6zvf+DwwKh8Si8YhMKpfMpvMJjUqn1Kr1is1qt9yu9wsOi8fksvlsKwAAOw==

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Artem Kondratsky, 2019-12-08
@kondrackii

Nagovnokodil what you need. Thanks to all!

image = Image.open("[Путь до изображения]")
image.putalpha(0)
buffer = BytesIO()
image.save(buffer, format='PNG')
buffer.seek(0)
image_bytes = buffer.read()
image_base64 = base64.b64encode(image_bytes).decode('ascii')

V
Vladimir Korotenko, 2019-12-08
@firedragon

put the empty.gif file in the img folder
Or like this
https://jsfiddle.net/vkorotenko/j1bzntaw/

A
Andrey Dugin, 2019-12-08
@adugin

import numpy as np

image = np.zeros((50, 100, 3), np.uint8)  # чёрное
image = np.ones((50, 100, 3), np.uint8) * 255  # белое

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question