mirror of
https://github.com/vladmandic/sdnext.git
synced 2026-01-27 15:02:48 +03:00
33 lines
783 B
Python
Executable File
33 lines
783 B
Python
Executable File
#!/usr/bin/env python
|
|
import io
|
|
import os
|
|
import sys
|
|
import base64
|
|
from PIL import Image
|
|
from rich import print # pylint: disable=redefined-builtin
|
|
|
|
|
|
def encode(file: str):
|
|
image = Image.open(file) if os.path.exists(file) else None
|
|
print(f'Input: file={file} image={image}')
|
|
if image is None:
|
|
return None
|
|
if image.mode != 'RGB':
|
|
image = image.convert('RGB')
|
|
with io.BytesIO() as stream:
|
|
image.save(stream, 'JPEG')
|
|
image.close()
|
|
values = stream.getvalue()
|
|
encoded = base64.b64encode(values).decode()
|
|
return encoded
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.argv.pop(0)
|
|
fn = sys.argv[0] if len(sys.argv) > 0 else ''
|
|
b64 = encode(fn)
|
|
print('=== BEGIN ===')
|
|
print(f'{b64}')
|
|
print('=== END ===')
|
|
|