-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerator.py
165 lines (139 loc) · 7.99 KB
/
generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import discord
from discord.ext import commands
import random
import hashlib
import os
"""
* Generate cog
* Has commands: gen, rand, name, mess, trunc, mix
"""
class Generate(commands.Cog):
def __init__(self, generator, delete_images = False):
self.generator = generator
self.generator.generate_one_image(1)
self.generator.style_mix(100, 200)
self.delete_images = delete_images
#----------------------------------------------------------------------------
@commands.command(help="Generates an image from a given seed. Params: seed", aliases=["generate"])
async def gen(self, ctx, seed:int):
img_path = self.generator.generate_one_image(seed)
await ctx.send('Here is your generated anime girl from seed %s :)' % seed, file=discord.File(img_path, 'moe.png'))
await self.cleanup(img_path)
@gen.error
async def gen_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("No seed provided")
elif isinstance(error, commands.BadArgument):
await ctx.send("Enter a number for the seed")
elif isinstance(error.original, ValueError):
await ctx.send("Enter a number between 0 and 2^32 -1")
else:
print(error, error.original)
await ctx.send("Uh oh something bad happened and idk what it was")
#----------------------------------------------------------------------------
@commands.command(help="Generates the opposite image from a given seed. Params: seed", aliases=["opp"])
async def opposite(self, ctx, seed:int):
img_path = self.generator.generate_one_image(seed, -0.55)
await ctx.send('Here is your generated anime girl opposite of seed %s :)' % seed, file=discord.File(img_path, 'moe.png'))
await self.cleanup(img_path)
@opposite.error
async def opposite_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("No seed provided")
elif isinstance(error, commands.BadArgument):
await ctx.send("Enter a number for the seed")
elif isinstance(error.original, ValueError):
await ctx.send("Enter a number between 0 and 2^32 -1")
else:
print(error, error.original)
await ctx.send("Uh oh something bad happened and idk what it was")
#----------------------------------------------------------------------------
@commands.command(help="Generates an image from a random seed. Params: None", aliases=["random"])
async def rand(self, ctx):
trunc = round(random.uniform(0.5, 1),3)
seed = random.randint(0, 4294967295,)
img_path = self.generator.generate_one_image(seed, trunc)
await ctx.send('Here is your randomly generated anime girl :) seed: %s truncation: %.3f' % (seed, trunc), file=discord.File(img_path, 'moe.png'))
await self.cleanup(img_path)
@rand.error
async def rand_error(self, ctx, error):
print(error, error.original)
await ctx.send("Uh oh something bad happened and idk what it was")
#----------------------------------------------------------------------------
@commands.command(help="Generates an image from a random seed with truncation turned off. Params: None", aliases=["messy"])
async def mess(self, ctx):
seed = random.randint(0, 4294967295)
img_path = self.generator.generate_one_image(seed, 1)
await ctx.send('Here is your randomly generated anime girl seed: %s :)\nShe may look kind of messed up' % seed, file=discord.File(img_path, 'moe.png'))
await self.cleanup(img_path)
@mess.error
async def mess_error(self, ctx, error):
print(error, error.original)
await ctx.send("Uh oh something bad happened and idk what it was")
#----------------------------------------------------------------------------
@commands.command(help="Generates an image from a given seed with truncation value. Params: seed, truncation", aliases=["truncation"])
async def trunc(self, ctx, seed:int, truncation:float):
if truncation < -1.0 or truncation > 1.0:
raise ValueError
img_path = self.generator.generate_one_image(seed, truncation)
await ctx.send('Here is your generated anime girl with seed %s and truncation %s :)' % (seed, truncation), file=discord.File(img_path, 'moe.png'))
await self.cleanup(img_path)
@trunc.error
async def trunc_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("No seed or truncation value provided")
elif isinstance(error, commands.BadArgument):
await ctx.send("Enter a number for the seed and truncation")
elif isinstance(error.original, ValueError):
await ctx.send("Enter a number between 0 and 2^32 -1 for the seed and -1 -> 1 for truncation value")
else:
print(error, error.original)
await ctx.send("Uh oh something bad happened and idk what it was")
#----------------------------------------------------------------------------
@commands.command(help="Generates an image from a seed based on a string. Params: string(s)")
async def name(self, ctx, *, input_string):
seed = self.convertToSeed(input_string)
img_path = self.generator.generate_one_image(seed)
await ctx.send('Here is your generated anime girl from name %s seed: %s' % (input_string, seed), file=discord.File(img_path, 'moe.png'))
await self.cleanup(img_path)
@name.error
async def name_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("No name provided")
elif isinstance(error, commands.BadArgument):
await ctx.send("Enter a string for the name")
else:
print(error, error.original)
await ctx.send("Uh oh something bad happened and idk what it was")
#----------------------------------------------------------------------------
@commands.command(help="Applies the style a of input A onto input B, split with | if they're more than 1 word use -t to set the truncation. -t must come after all inputs and separated by whitespace.\nExamples:\nmoemix moetron moe\nmoemix moetron is awesome! | I love moetron!\nmoemix 100 500\nmoemix moetron is awesome! | I love moetron! -t 0.9")
async def mix(self, ctx, *, args):
trunc = 0.55
if " -t " in args:
args, temp_trunc = args.split(" -t ")
trunc = float(temp_trunc)
if trunc < -1 or trunc > 1:
raise ValueError
split_args = args.split(" | ") if " | " in args else args.split()
arg1, arg2 = split_args[0], split_args[1] # This will throw for 1 arg that's fine
img_path = self.generator.style_mix(self.convertToSeed(arg1), self.convertToSeed(arg2), truncation_psi=trunc)
await ctx.send('Here is your generated anime girl from %s and %s :)' % (arg1, arg2), file=discord.File(img_path, 'moe.png'))
await self.cleanup(img_path)
@mix.error
async def mix_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Not enough inputs provided")
elif isinstance(error.original, IndexError):
await ctx.send("Not enough inputs provided")
elif isinstance(error.original, ValueError):
await ctx.send("Enter a number between 1 and -1 for trunc")
else:
print(error, error.original)
await ctx.send("Uh oh something bad happened and idk what it was")
#----------------------------------------------------------------------------
## Helper Methods
def convertToSeed(self, arg:str):
return int.from_bytes(hashlib.md5(arg.encode('utf-8')).digest(), byteorder='big', signed=False) % 1000000000 if not arg.isdigit() else int(arg)
async def cleanup(self, image_path):
if self.delete_images and os.path.isfile(image_path):
os.remove(image_path)