m3g4p0p commited on
Commit
16c06bf
·
1 Parent(s): 7a8f70c

add image param type

Browse files
Files changed (4) hide show
  1. myapp/__main__.py +4 -0
  2. myapp/app.py +1 -1
  3. myapp/cli.py +6 -2
  4. myapp/types.py +19 -0
myapp/__main__.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from myapp.cli import cli
2
+
3
+ if __name__ == "__main__":
4
+ cli()
myapp/app.py CHANGED
@@ -65,7 +65,7 @@ with gr.Blocks() as demo:
65
  matches = re.findall(r"\d+(?:\.\d+)?", value)
66
  r, g, b = map(int, map(float, matches[:3]))
67
 
68
- return f"#{r:x}{g:x}{b:x}".upper()
69
 
70
  image = Image.open(data[background])
71
  qr_code = segno.make(data[text], error="h")
 
65
  matches = re.findall(r"\d+(?:\.\d+)?", value)
66
  r, g, b = map(int, map(float, matches[:3]))
67
 
68
+ return f"#{r:02X}{g:02X}{b:02X}"
69
 
70
  image = Image.open(data[background])
71
  qr_code = segno.make(data[text], error="h")
myapp/cli.py CHANGED
@@ -2,6 +2,8 @@ import click
2
  import dotenv
3
  from huggingface_hub import InferenceClient
4
 
 
 
5
  dotenv.load_dotenv()
6
  client = InferenceClient()
7
 
@@ -28,5 +30,7 @@ def generate_image(prompt, target, model, width, height):
28
  image.save(target)
29
 
30
 
31
- if __name__ == "__main__":
32
- cli()
 
 
 
2
  import dotenv
3
  from huggingface_hub import InferenceClient
4
 
5
+ from myapp.types import ImageParamType
6
+
7
  dotenv.load_dotenv()
8
  client = InferenceClient()
9
 
 
30
  image.save(target)
31
 
32
 
33
+ @cli.command()
34
+ @click.option("--image", type=ImageParamType(), required=True)
35
+ def generate_palette(image):
36
+ pass
myapp/types.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any
2
+
3
+ import click
4
+ from PIL import Image
5
+
6
+
7
+ class ImageParamType(click.ParamType):
8
+ name = "image"
9
+
10
+ def convert(
11
+ self, value: Any, param: click.Parameter | None, ctx: click.Context | None
12
+ ) -> Any:
13
+ if value is None or isinstance(value, Image.Image):
14
+ return value
15
+
16
+ try:
17
+ return Image.open(value)
18
+ except OSError as e:
19
+ self.fail(str(e))