min-dalle-test/image_from_text.py

63 lines
1.9 KiB
Python
Raw Normal View History

2022-06-27 19:46:04 +00:00
import argparse
import os
from PIL import Image
from min_dalle.min_dalle_torch import MinDalleTorch
2022-06-27 19:46:04 +00:00
parser = argparse.ArgumentParser()
2022-06-27 20:49:42 +00:00
parser.add_argument('--mega', action='store_true')
parser.add_argument('--no-mega', dest='mega', action='store_false')
parser.set_defaults(mega=False)
parser.add_argument('--text', type=str, default='cat')
parser.add_argument('--seed', type=int, default=0)
2022-06-27 20:49:42 +00:00
parser.add_argument('--image_path', type=str, default='generated')
parser.add_argument('--token_count', type=int, default=256) # for debugging
2022-06-27 19:46:04 +00:00
def ascii_from_image(image: Image.Image, size: int) -> str:
rgb_pixels = image.resize((size, int(0.55 * size))).convert('L').getdata()
chars = list('.,;/IOX')
chars = [chars[i * len(chars) // 256] for i in rgb_pixels]
chars = [chars[i * size: (i + 1) * size] for i in range(size // 2)]
return '\n'.join(''.join(row) for row in chars)
2022-06-27 20:49:42 +00:00
def save_image(image: Image.Image, path: str):
2022-06-27 19:46:04 +00:00
if os.path.isdir(path):
path = os.path.join(path, 'generated.png')
elif not path.endswith('.png'):
path += '.png'
print("saving image to", path)
image.save(path)
return image
def generate_image(
is_mega: bool,
text: str,
seed: int,
image_path: str,
token_count: int
):
2022-06-30 15:25:24 +00:00
is_reusable = False
model = MinDalleTorch(is_mega, is_reusable, token_count)
if token_count < 256:
image_tokens = model.generate_image_tokens(text, seed)
print('image tokens', list(image_tokens.to('cpu').detach().numpy()))
else:
image = model.generate_image(text, seed)
save_image(image, image_path)
print(ascii_from_image(image, size=128))
2022-06-27 19:46:04 +00:00
if __name__ == '__main__':
args = parser.parse_args()
2022-06-27 20:49:42 +00:00
print(args)
generate_image(
is_mega=args.mega,
text=args.text,
seed=args.seed,
image_path=args.image_path,
token_count=args.token_count
)