sticker-generator 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,216 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+ #poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ #pdm.lock
116
+ #pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ #pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # SageMath parsed files
135
+ *.sage.py
136
+
137
+ # Environments
138
+ .env
139
+ .envrc
140
+ .venv
141
+ env/
142
+ venv/
143
+ ENV/
144
+ env.bak/
145
+ venv.bak/
146
+
147
+ # Spyder project settings
148
+ .spyderproject
149
+ .spyproject
150
+
151
+ # Rope project settings
152
+ .ropeproject
153
+
154
+ # mkdocs documentation
155
+ /site
156
+
157
+ # mypy
158
+ .mypy_cache/
159
+ .dmypy.json
160
+ dmypy.json
161
+
162
+ # Pyre type checker
163
+ .pyre/
164
+
165
+ # pytype static type analyzer
166
+ .pytype/
167
+
168
+ # Cython debug symbols
169
+ cython_debug/
170
+
171
+ # PyCharm
172
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
173
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
174
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
175
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
176
+ #.idea/
177
+
178
+ # Abstra
179
+ # Abstra is an AI-powered process automation framework.
180
+ # Ignore directories containing user credentials, local state, and settings.
181
+ # Learn more at https://abstra.io/docs
182
+ .abstra/
183
+
184
+ # Visual Studio Code
185
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
186
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
187
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
188
+ # you could uncomment the following to ignore the entire vscode folder
189
+ # .vscode/
190
+
191
+ # Ruff stuff:
192
+ .ruff_cache/
193
+
194
+ # PyPI configuration file
195
+ .pypirc
196
+
197
+ # Cursor
198
+ # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
199
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
200
+ # refer to https://docs.cursor.com/context/ignore-files
201
+ .cursorignore
202
+ .cursorindexingignore
203
+
204
+ # Marimo
205
+ marimo/_static/
206
+ marimo/_lsp/
207
+ __marimo__/
208
+
209
+ # Generated stickers (for testing)
210
+ *.png
211
+ !tests/fixtures/*.png
212
+
213
+ # OS
214
+ .DS_Store
215
+ Thumbs.db
216
+ CLAUDE.md
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,129 @@
1
+ Metadata-Version: 2.4
2
+ Name: sticker-generator
3
+ Version: 0.1.0
4
+ Summary: Generate stickers with transparent backgrounds using Gemini AI
5
+ Project-URL: Homepage, https://github.com/ali/sticker-generator
6
+ Project-URL: Repository, https://github.com/ali/sticker-generator
7
+ Project-URL: Issues, https://github.com/ali/sticker-generator/issues
8
+ Author: Ali
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: ai,gemini,image-generation,sticker,transparent-background
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Multimedia :: Graphics
22
+ Requires-Python: >=3.10
23
+ Requires-Dist: google-genai>=1.0.0
24
+ Requires-Dist: numpy>=1.24.0
25
+ Requires-Dist: pillow>=10.0.0
26
+ Requires-Dist: scipy>=1.10.0
27
+ Provides-Extra: dev
28
+ Requires-Dist: mypy>=1.0.0; extra == 'dev'
29
+ Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
30
+ Requires-Dist: pytest>=7.0.0; extra == 'dev'
31
+ Requires-Dist: ruff>=0.1.0; extra == 'dev'
32
+ Description-Content-Type: text/markdown
33
+
34
+ # Sticker Generator
35
+
36
+ Generate stickers with transparent backgrounds using Google's Gemini AI.
37
+
38
+ ## Installation
39
+
40
+ ```bash
41
+ pip install sticker-generator
42
+ ```
43
+
44
+ ## Setup
45
+
46
+ Set your Gemini API key as an environment variable:
47
+
48
+ ```bash
49
+ export GEMINI_API_KEY="your-api-key"
50
+ ```
51
+
52
+ Or pass it directly to the functions/CLI.
53
+
54
+ ## Usage
55
+
56
+ ### Command Line
57
+
58
+ ```bash
59
+ # Basic usage
60
+ sticker-generator "a cute happy cat with big eyes"
61
+
62
+ # Specify output file
63
+ sticker-generator "a rocket ship" -o rocket.png
64
+
65
+ # Use reference images
66
+ sticker-generator "similar style illustration" -i reference1.png -i reference2.png
67
+
68
+ # Custom aspect ratio
69
+ sticker-generator "a wide banner" --aspect-ratio 16:9
70
+
71
+ # Save raw image before processing
72
+ sticker-generator "a dog" --save-raw
73
+ ```
74
+
75
+ ### Python API
76
+
77
+ ```python
78
+ from sticker_generator import create_sticker
79
+
80
+ # Basic usage
81
+ sticker = create_sticker(
82
+ prompt="a cute happy cat with big eyes",
83
+ output="cat.png"
84
+ )
85
+
86
+ # With reference images
87
+ sticker = create_sticker(
88
+ prompt="similar style illustration",
89
+ output="custom.png",
90
+ input_images=["reference1.png", "reference2.png"]
91
+ )
92
+
93
+ # Just get the image without saving
94
+ sticker = create_sticker(
95
+ prompt="a rocket ship",
96
+ output=None # Returns PIL Image
97
+ )
98
+ ```
99
+
100
+ ### Image Processing Only
101
+
102
+ If you have your own green-screen images:
103
+
104
+ ```python
105
+ from PIL import Image
106
+ from sticker_generator import remove_green_screen_hsv, cleanup_edges
107
+
108
+ # Load your image
109
+ img = Image.open("green_background.png")
110
+
111
+ # Remove green background
112
+ transparent = remove_green_screen_hsv(img)
113
+
114
+ # Clean up edges
115
+ clean = cleanup_edges(transparent, threshold=64)
116
+
117
+ # Save
118
+ clean.save("transparent.png")
119
+ ```
120
+
121
+ ## How It Works
122
+
123
+ 1. **Generation**: Uses Gemini AI to generate an image with a chromakey green (#00FF00) background
124
+ 2. **Green Removal**: Converts to HSV color space and removes pixels matching green hue
125
+ 3. **Edge Cleanup**: Removes semi-transparent edge artifacts for clean results
126
+
127
+ ## License
128
+
129
+ MIT
@@ -0,0 +1,96 @@
1
+ # Sticker Generator
2
+
3
+ Generate stickers with transparent backgrounds using Google's Gemini AI.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install sticker-generator
9
+ ```
10
+
11
+ ## Setup
12
+
13
+ Set your Gemini API key as an environment variable:
14
+
15
+ ```bash
16
+ export GEMINI_API_KEY="your-api-key"
17
+ ```
18
+
19
+ Or pass it directly to the functions/CLI.
20
+
21
+ ## Usage
22
+
23
+ ### Command Line
24
+
25
+ ```bash
26
+ # Basic usage
27
+ sticker-generator "a cute happy cat with big eyes"
28
+
29
+ # Specify output file
30
+ sticker-generator "a rocket ship" -o rocket.png
31
+
32
+ # Use reference images
33
+ sticker-generator "similar style illustration" -i reference1.png -i reference2.png
34
+
35
+ # Custom aspect ratio
36
+ sticker-generator "a wide banner" --aspect-ratio 16:9
37
+
38
+ # Save raw image before processing
39
+ sticker-generator "a dog" --save-raw
40
+ ```
41
+
42
+ ### Python API
43
+
44
+ ```python
45
+ from sticker_generator import create_sticker
46
+
47
+ # Basic usage
48
+ sticker = create_sticker(
49
+ prompt="a cute happy cat with big eyes",
50
+ output="cat.png"
51
+ )
52
+
53
+ # With reference images
54
+ sticker = create_sticker(
55
+ prompt="similar style illustration",
56
+ output="custom.png",
57
+ input_images=["reference1.png", "reference2.png"]
58
+ )
59
+
60
+ # Just get the image without saving
61
+ sticker = create_sticker(
62
+ prompt="a rocket ship",
63
+ output=None # Returns PIL Image
64
+ )
65
+ ```
66
+
67
+ ### Image Processing Only
68
+
69
+ If you have your own green-screen images:
70
+
71
+ ```python
72
+ from PIL import Image
73
+ from sticker_generator import remove_green_screen_hsv, cleanup_edges
74
+
75
+ # Load your image
76
+ img = Image.open("green_background.png")
77
+
78
+ # Remove green background
79
+ transparent = remove_green_screen_hsv(img)
80
+
81
+ # Clean up edges
82
+ clean = cleanup_edges(transparent, threshold=64)
83
+
84
+ # Save
85
+ clean.save("transparent.png")
86
+ ```
87
+
88
+ ## How It Works
89
+
90
+ 1. **Generation**: Uses Gemini AI to generate an image with a chromakey green (#00FF00) background
91
+ 2. **Green Removal**: Converts to HSV color space and removes pixels matching green hue
92
+ 3. **Edge Cleanup**: Removes semi-transparent edge artifacts for clean results
93
+
94
+ ## License
95
+
96
+ MIT
@@ -0,0 +1,96 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "sticker-generator"
7
+ version = "0.1.0"
8
+ description = "Generate stickers with transparent backgrounds using Gemini AI"
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ requires-python = ">=3.10"
12
+ authors = [
13
+ { name = "Ali" }
14
+ ]
15
+ keywords = ["sticker", "gemini", "ai", "image-generation", "transparent-background"]
16
+ classifiers = [
17
+ "Development Status :: 4 - Beta",
18
+ "Intended Audience :: Developers",
19
+ "License :: OSI Approved :: MIT License",
20
+ "Operating System :: OS Independent",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.10",
23
+ "Programming Language :: Python :: 3.11",
24
+ "Programming Language :: Python :: 3.12",
25
+ "Programming Language :: Python :: 3.13",
26
+ "Topic :: Multimedia :: Graphics",
27
+ ]
28
+ dependencies = [
29
+ "google-genai>=1.0.0",
30
+ "Pillow>=10.0.0",
31
+ "numpy>=1.24.0",
32
+ "scipy>=1.10.0",
33
+ ]
34
+
35
+ [project.optional-dependencies]
36
+ dev = [
37
+ "pytest>=7.0.0",
38
+ "pytest-cov>=4.0.0",
39
+ "ruff>=0.1.0",
40
+ "mypy>=1.0.0",
41
+ ]
42
+
43
+ [project.scripts]
44
+ sticker-generator = "sticker_generator.cli:main"
45
+
46
+ [project.urls]
47
+ Homepage = "https://github.com/ali/sticker-generator"
48
+ Repository = "https://github.com/ali/sticker-generator"
49
+ Issues = "https://github.com/ali/sticker-generator/issues"
50
+
51
+ [tool.hatch.build.targets.sdist]
52
+ include = [
53
+ "/src",
54
+ ]
55
+
56
+ [tool.hatch.build.targets.wheel]
57
+ packages = ["src/sticker_generator"]
58
+
59
+ [tool.ruff]
60
+ target-version = "py310"
61
+ line-length = 88
62
+
63
+ [tool.ruff.lint]
64
+ select = [
65
+ "E", # pycodestyle errors
66
+ "W", # pycodestyle warnings
67
+ "F", # Pyflakes
68
+ "I", # isort
69
+ "B", # flake8-bugbear
70
+ "UP", # pyupgrade
71
+ ]
72
+ ignore = []
73
+
74
+ [tool.ruff.lint.isort]
75
+ known-first-party = ["sticker_generator"]
76
+
77
+ [tool.mypy]
78
+ python_version = "3.10"
79
+ warn_return_any = true
80
+ warn_unused_configs = true
81
+ ignore_missing_imports = true
82
+
83
+ [tool.pytest.ini_options]
84
+ testpaths = ["tests"]
85
+ pythonpath = ["src"]
86
+
87
+ [tool.coverage.run]
88
+ source = ["src/sticker_generator"]
89
+ branch = true
90
+
91
+ [tool.coverage.report]
92
+ exclude_lines = [
93
+ "pragma: no cover",
94
+ "if TYPE_CHECKING:",
95
+ "if __name__ == .__main__.:",
96
+ ]
@@ -0,0 +1,17 @@
1
+ """Sticker Generator - Create stickers with transparent backgrounds using Gemini AI."""
2
+
3
+ from sticker_generator.core import create_sticker, generate_sticker
4
+ from sticker_generator.image_processing import (
5
+ cleanup_edges,
6
+ remove_green_screen_aggressive,
7
+ remove_green_screen_hsv,
8
+ )
9
+
10
+ __version__ = "0.1.0"
11
+ __all__ = [
12
+ "create_sticker",
13
+ "generate_sticker",
14
+ "remove_green_screen_hsv",
15
+ "remove_green_screen_aggressive",
16
+ "cleanup_edges",
17
+ ]
@@ -0,0 +1,89 @@
1
+ """Command-line interface for sticker generation."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import sys
7
+ from pathlib import Path
8
+
9
+ from sticker_generator.core import create_sticker
10
+
11
+
12
+ def main() -> int:
13
+ """Main entry point for the CLI."""
14
+ parser = argparse.ArgumentParser(
15
+ prog="sticker-generator",
16
+ description="Generate stickers with transparent backgrounds using Gemini AI",
17
+ )
18
+ parser.add_argument(
19
+ "prompt",
20
+ help="Description of the sticker to generate",
21
+ )
22
+ parser.add_argument(
23
+ "-o",
24
+ "--output",
25
+ default="sticker.png",
26
+ help="Output filename (default: sticker.png)",
27
+ )
28
+ parser.add_argument(
29
+ "-i",
30
+ "--image",
31
+ action="append",
32
+ dest="images",
33
+ help="Reference image path (can be specified multiple times)",
34
+ )
35
+ parser.add_argument(
36
+ "--aspect-ratio",
37
+ default="1:1",
38
+ help="Image aspect ratio (default: 1:1)",
39
+ )
40
+ parser.add_argument(
41
+ "--save-raw",
42
+ action="store_true",
43
+ help="Save the raw image before green screen removal",
44
+ )
45
+ parser.add_argument(
46
+ "--edge-threshold",
47
+ type=int,
48
+ default=64,
49
+ help="Alpha threshold for edge cleanup, 0-255 (default: 64)",
50
+ )
51
+ parser.add_argument(
52
+ "--api-key",
53
+ help="Gemini API key (or set GEMINI_API_KEY environment variable)",
54
+ )
55
+
56
+ args = parser.parse_args()
57
+
58
+ # Validate reference images exist
59
+ if args.images:
60
+ for img_path in args.images:
61
+ if not Path(img_path).exists():
62
+ print(f"Error: Reference image not found: {img_path}", file=sys.stderr)
63
+ return 1
64
+
65
+ try:
66
+ print(f"Generating sticker: {args.prompt}")
67
+ if args.images:
68
+ print(f"Using {len(args.images)} reference image(s)")
69
+
70
+ create_sticker(
71
+ prompt=args.prompt,
72
+ output=args.output,
73
+ aspect_ratio=args.aspect_ratio,
74
+ save_raw=args.save_raw,
75
+ input_images=args.images,
76
+ api_key=args.api_key,
77
+ edge_threshold=args.edge_threshold,
78
+ )
79
+
80
+ print(f"Sticker saved to: {args.output}")
81
+ return 0
82
+
83
+ except Exception as e:
84
+ print(f"Error: {e}", file=sys.stderr)
85
+ return 1
86
+
87
+
88
+ if __name__ == "__main__":
89
+ sys.exit(main())
@@ -0,0 +1,199 @@
1
+ """Core sticker generation functionality using Gemini AI."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import base64
6
+ import io
7
+ import mimetypes
8
+ from pathlib import Path
9
+ from typing import TYPE_CHECKING
10
+
11
+ from google import genai
12
+ from PIL import Image
13
+
14
+ from sticker_generator.image_processing import (
15
+ cleanup_edges,
16
+ remove_green_screen_aggressive,
17
+ remove_green_screen_hsv,
18
+ save_transparent_png,
19
+ )
20
+
21
+ if TYPE_CHECKING:
22
+ from os import PathLike
23
+
24
+ MODEL_ID = "gemini-2.5-flash-image"
25
+
26
+ # fmt: off
27
+ # ruff: noqa: E501
28
+ CHROMAKEY_PROMPT_TEMPLATE = """Create a sticker illustration of: {prompt}
29
+
30
+ CRITICAL CHROMAKEY REQUIREMENTS:
31
+ 1. BACKGROUND: Solid, flat, uniform chromakey green color. Use EXACTLY hex color #00FF00 (RGB 0, 255, 0).
32
+ The entire background must be this single pure green color with NO variation, NO gradients, NO shadows, NO lighting effects.
33
+
34
+ 2. WHITE OUTLINE: The subject MUST have a clean white outline/border (2-3 pixels wide) separating it from the green background.
35
+ This white border prevents color bleeding between the subject and background.
36
+
37
+ 3. NO GREEN ON SUBJECT: The subject itself should NOT contain any green colors to avoid confusion with the chromakey.
38
+ If the subject needs green (like leaves), use a distinctly different shade like dark forest green or teal.
39
+
40
+ 4. SHARP EDGES: The subject should have crisp, sharp, well-defined edges - no soft or blurry boundaries.
41
+
42
+ 5. CENTERED: Subject should be centered with padding around all sides.
43
+
44
+ 6. STYLE: Vibrant, clean, cartoon/illustration sticker style with bold colors.
45
+
46
+ This is for chromakey extraction - the green background will be removed programmatically."""
47
+ # fmt: on
48
+
49
+
50
+ def decode_image(data: str | bytes) -> Image.Image:
51
+ """Decode image data to PIL Image.
52
+
53
+ Args:
54
+ data: Base64-encoded string or raw bytes.
55
+
56
+ Returns:
57
+ PIL Image object.
58
+ """
59
+ if isinstance(data, str):
60
+ image_bytes = base64.b64decode(data)
61
+ else:
62
+ image_bytes = data
63
+ return Image.open(io.BytesIO(image_bytes))
64
+
65
+
66
+ def load_image_as_content(image_path: str | PathLike) -> genai.types.Part:
67
+ """Load an image file and return as API content block.
68
+
69
+ Args:
70
+ image_path: Path to the image file.
71
+
72
+ Returns:
73
+ Part formatted for Gemini API content.
74
+ """
75
+ path = Path(image_path)
76
+ mime_type, _ = mimetypes.guess_type(str(path))
77
+ if mime_type is None:
78
+ mime_type = "image/jpeg"
79
+
80
+ with open(path, "rb") as f:
81
+ image_data = f.read()
82
+
83
+ return genai.types.Part.from_bytes(data=image_data, mime_type=mime_type)
84
+
85
+
86
+ def generate_sticker(
87
+ prompt: str,
88
+ aspect_ratio: str = "1:1",
89
+ input_images: list[str | PathLike] | None = None,
90
+ api_key: str | None = None,
91
+ ) -> Image.Image:
92
+ """Generate a sticker image with chromakey green background.
93
+
94
+ Args:
95
+ prompt: Description of the sticker to generate.
96
+ aspect_ratio: Image aspect ratio (default "1:1").
97
+ input_images: Optional list of reference image paths.
98
+ api_key: Optional Gemini API key (uses GEMINI_API_KEY env var if not provided).
99
+
100
+ Returns:
101
+ PIL Image with green background (before processing).
102
+
103
+ Raises:
104
+ ValueError: If no image was generated.
105
+ """
106
+ client = genai.Client(api_key=api_key) if api_key else genai.Client()
107
+
108
+ enhanced_prompt = CHROMAKEY_PROMPT_TEMPLATE.format(prompt=prompt)
109
+
110
+ input_content: str | list = enhanced_prompt
111
+ if input_images:
112
+ content_list: list = []
113
+ for img_path in input_images:
114
+ content_list.append(load_image_as_content(img_path))
115
+ content_list.append(enhanced_prompt)
116
+ input_content = content_list
117
+
118
+ response = client.models.generate_content(
119
+ model=MODEL_ID,
120
+ contents=input_content,
121
+ config=genai.types.GenerateContentConfig(
122
+ response_modalities=["TEXT", "IMAGE"],
123
+ ),
124
+ )
125
+
126
+ if response.candidates:
127
+ for part in response.candidates[0].content.parts: # type: ignore[union-attr]
128
+ if part.inline_data is not None:
129
+ print(f"Found image: mime_type={part.inline_data.mime_type}")
130
+ return decode_image(part.inline_data.data) # type: ignore[arg-type]
131
+ elif part.text:
132
+ print(f"Text response: {part.text[:200]}...")
133
+
134
+ raise ValueError("No image was generated")
135
+
136
+
137
+ def create_sticker(
138
+ prompt: str,
139
+ output: str | PathLike | None = None,
140
+ aspect_ratio: str = "1:1",
141
+ save_raw: bool = False,
142
+ input_images: list[str | PathLike] | None = None,
143
+ api_key: str | None = None,
144
+ edge_threshold: int = 64,
145
+ ) -> Image.Image:
146
+ """Generate a sticker with transparent background.
147
+
148
+ Complete workflow: generates image with green background, removes green,
149
+ cleans edges, and optionally saves to file.
150
+
151
+ Args:
152
+ prompt: Description of the sticker to generate.
153
+ output: Optional output filename (PNG recommended).
154
+ aspect_ratio: Image aspect ratio (default "1:1").
155
+ save_raw: If True, save the raw image before processing.
156
+ input_images: Optional list of reference image paths.
157
+ api_key: Optional Gemini API key (uses GEMINI_API_KEY env var if not provided).
158
+ edge_threshold: Alpha threshold for edge cleanup (0-255).
159
+
160
+ Returns:
161
+ PIL Image with transparent background.
162
+ """
163
+ raw_image = generate_sticker(
164
+ prompt=prompt,
165
+ aspect_ratio=aspect_ratio,
166
+ input_images=input_images,
167
+ api_key=api_key,
168
+ )
169
+
170
+ if save_raw and output:
171
+ output_path = Path(output)
172
+ raw_filename = output_path.with_stem(output_path.stem + "_raw")
173
+ raw_image.save(raw_filename)
174
+
175
+ # HSV-based green removal (permissive settings for various green shades)
176
+ transparent_image = remove_green_screen_hsv(
177
+ raw_image,
178
+ hue_center=115,
179
+ hue_range=35,
180
+ min_saturation=25,
181
+ min_value=40,
182
+ dilation_iterations=2,
183
+ erosion_iterations=0,
184
+ )
185
+
186
+ # Second pass: aggressive removal for remaining greens
187
+ transparent_image = remove_green_screen_aggressive(
188
+ transparent_image,
189
+ green_threshold=1.1,
190
+ edge_pixels=1,
191
+ )
192
+
193
+ # Edge cleanup
194
+ transparent_image = cleanup_edges(transparent_image, threshold=edge_threshold)
195
+
196
+ if output:
197
+ save_transparent_png(transparent_image, str(output))
198
+
199
+ return transparent_image
@@ -0,0 +1,171 @@
1
+ """Image processing utilities for green screen removal and edge cleanup."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import numpy as np
6
+ from PIL import Image
7
+
8
+
9
+ def rgb_to_hsv_array(rgb_array: np.ndarray) -> np.ndarray:
10
+ """Convert RGB array to HSV array efficiently.
11
+
12
+ Args:
13
+ rgb_array: NumPy array of RGB values (H, W, 3).
14
+
15
+ Returns:
16
+ NumPy array of HSV values (H, W, 3) with H in [0, 360], S and V in [0, 100].
17
+ """
18
+ rgb_normalized = rgb_array.astype(np.float32) / 255.0
19
+ r, g, b = rgb_normalized[:, :, 0], rgb_normalized[:, :, 1], rgb_normalized[:, :, 2]
20
+
21
+ max_c = np.maximum(np.maximum(r, g), b)
22
+ min_c = np.minimum(np.minimum(r, g), b)
23
+ delta = max_c - min_c
24
+
25
+ h = np.zeros_like(max_c)
26
+ mask_r = (max_c == r) & (delta != 0)
27
+ h[mask_r] = (60 * ((g[mask_r] - b[mask_r]) / delta[mask_r]) + 360) % 360
28
+
29
+ mask_g = (max_c == g) & (delta != 0)
30
+ h[mask_g] = 60 * ((b[mask_g] - r[mask_g]) / delta[mask_g]) + 120
31
+
32
+ mask_b = (max_c == b) & (delta != 0)
33
+ h[mask_b] = 60 * ((r[mask_b] - g[mask_b]) / delta[mask_b]) + 240
34
+
35
+ s = np.zeros_like(max_c)
36
+ s[max_c != 0] = delta[max_c != 0] / max_c[max_c != 0]
37
+
38
+ v = max_c
39
+ return np.stack([h, s * 100, v * 100], axis=-1)
40
+
41
+
42
+ def remove_green_screen_hsv(
43
+ image: Image.Image,
44
+ hue_center: float = 120,
45
+ hue_range: float = 25,
46
+ min_saturation: float = 75,
47
+ min_value: float = 70,
48
+ dilation_iterations: int = 2,
49
+ erosion_iterations: int = 0,
50
+ ) -> Image.Image:
51
+ """Remove green screen using HSV color space detection.
52
+
53
+ Args:
54
+ image: PIL Image to process.
55
+ hue_center: Center hue value for green (default 120 degrees).
56
+ hue_range: Tolerance around hue center in degrees.
57
+ min_saturation: Minimum saturation percentage to consider as green.
58
+ min_value: Minimum value/brightness percentage to consider as green.
59
+ dilation_iterations: Number of dilation passes to catch anti-aliased edges.
60
+ erosion_iterations: Number of erosion passes.
61
+
62
+ Returns:
63
+ PIL Image with green background removed (RGBA with transparency).
64
+ """
65
+ if image.mode != "RGBA":
66
+ image = image.convert("RGBA")
67
+
68
+ data = np.array(image)
69
+ rgb = data[:, :, :3]
70
+ hsv = rgb_to_hsv_array(rgb)
71
+ h, s, v = hsv[:, :, 0], hsv[:, :, 1], hsv[:, :, 2]
72
+
73
+ hue_diff = np.abs(h - hue_center)
74
+ hue_diff = np.minimum(hue_diff, 360 - hue_diff)
75
+
76
+ green_mask = (hue_diff < hue_range) & (s > min_saturation) & (v > min_value)
77
+
78
+ if dilation_iterations > 0 or erosion_iterations > 0:
79
+ from scipy import ndimage
80
+
81
+ if dilation_iterations > 0:
82
+ green_mask = ndimage.binary_dilation(
83
+ green_mask, iterations=dilation_iterations
84
+ )
85
+ if erosion_iterations > 0:
86
+ green_mask = ndimage.binary_erosion(
87
+ green_mask, iterations=erosion_iterations
88
+ )
89
+
90
+ alpha = data[:, :, 3].copy()
91
+ alpha[green_mask] = 0
92
+ data[:, :, 3] = alpha
93
+
94
+ return Image.fromarray(data)
95
+
96
+
97
+ def remove_green_screen_aggressive(
98
+ image: Image.Image,
99
+ green_threshold: float = 1.2,
100
+ edge_pixels: int = 0,
101
+ ) -> Image.Image:
102
+ """Aggressive green removal detecting dominant green pixels.
103
+
104
+ This method catches darker greens and tinted shadows that HSV might miss.
105
+
106
+ Args:
107
+ image: PIL Image to process.
108
+ green_threshold: Ratio threshold for green channel dominance.
109
+ edge_pixels: Number of dilation iterations for edge expansion.
110
+
111
+ Returns:
112
+ PIL Image with green background removed (RGBA with transparency).
113
+ """
114
+ if image.mode != "RGBA":
115
+ image = image.convert("RGBA")
116
+
117
+ data = np.array(image)
118
+ r = data[:, :, 0].astype(float)
119
+ g = data[:, :, 1].astype(float)
120
+ b = data[:, :, 2].astype(float)
121
+
122
+ rb_max = np.maximum(r, b) + 1
123
+ green_ratio = g / rb_max
124
+ green_dominant = (g > r) & (g > b)
125
+ green_mask = (green_ratio > green_threshold) & green_dominant
126
+
127
+ if edge_pixels > 0:
128
+ from scipy import ndimage
129
+
130
+ green_mask = ndimage.binary_dilation(green_mask, iterations=edge_pixels)
131
+
132
+ alpha = data[:, :, 3].copy()
133
+ alpha[green_mask] = 0
134
+ data[:, :, 3] = alpha
135
+
136
+ return Image.fromarray(data)
137
+
138
+
139
+ def cleanup_edges(image: Image.Image, threshold: int = 128) -> Image.Image:
140
+ """Clean up semi-transparent edge pixels by thresholding alpha.
141
+
142
+ Args:
143
+ image: PIL Image to process.
144
+ threshold: Alpha values below this become fully transparent,
145
+ values at or above become fully opaque.
146
+
147
+ Returns:
148
+ PIL Image with cleaned edges.
149
+ """
150
+ if image.mode != "RGBA":
151
+ return image
152
+
153
+ data = np.array(image)
154
+ alpha = data[:, :, 3]
155
+ alpha[alpha < threshold] = 0
156
+ alpha[alpha >= threshold] = 255
157
+
158
+ data[:, :, 3] = alpha
159
+ return Image.fromarray(data)
160
+
161
+
162
+ def save_transparent_png(image: Image.Image, filename: str) -> None:
163
+ """Save image as PNG with transparency preserved.
164
+
165
+ Args:
166
+ image: PIL Image to save.
167
+ filename: Output filename.
168
+ """
169
+ if image.mode != "RGBA":
170
+ image = image.convert("RGBA")
171
+ image.save(filename, "PNG")