Skip to content

Commit 476cdeb

Browse files
authored
feat: add examples and tests, update configuration (#5)
1 parent 57b2614 commit 476cdeb

15 files changed

Lines changed: 531 additions & 63 deletions

File tree

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
matrix:
1313
just-trigger:
1414
- "lint"
15-
# - "tests"
15+
- "tests"
1616
python-version:
1717
- "3.10"
1818
- "3.11"

README.md

Lines changed: 87 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,115 @@ Oversimplified Github-like userpic (avatar) generator.
66
[![PyPI](https://img.shields.io/pypi/v/tiny-userpic.svg)](https://pypi.python.org/pypi/tiny-userpic)
77
[![PyPI](https://img.shields.io/pypi/dm/tiny-userpic.svg)](https://pypi.python.org/pypi/tiny-userpic)
88

9-
## Installation
9+
## Features
10+
11+
- Generate unique avatars from text input (email, username, etc.)
12+
- Create both PIL Image and SVG outputs
13+
- Customizable size, colors, and padding
14+
- Deterministic output (same input always produces the same avatar)
1015

11-
Get started by installing the library via pip:
16+
## Installation
1217

1318
```bash
1419
pip install tiny-userpic
1520
```
1621

17-
## Create a PIL Image
22+
## Usage
1823

19-
```python
20-
from PIL.Image import Image
24+
The library provides several ways to generate avatars:
25+
26+
### 1. Random Generation (Non-deterministic)
27+
Generate a unique random avatar each time.
2128

22-
from userpic import make_userpic_image
29+
```python
30+
from tiny_userpic import make_userpic_image
2331

24-
# Generate a PIL Image object
25-
image: Image = make_userpic_image(
32+
# Generate random avatar
33+
random_image = make_userpic_image(
2634
size=(7, 5),
27-
padding=(20, 10),
28-
mode='RGB',
2935
image_size=(300, 300),
30-
background='white',
31-
foreground='black',
36+
background="white",
37+
foreground="black"
3238
)
33-
34-
# save as JPEG file
35-
with open('output.jpeg', 'wb') as fp:
36-
image.save(fp)
39+
random_image.save("random_avatar.png")
3740
```
3841

39-
## Create SVG Data
42+
### 2. With Custom Seed (Deterministic)
43+
Generate an avatar with a specific seed for reproducible results.
4044

4145
```python
46+
from tiny_userpic import make_userpic_image
4247

43-
from userpic import make_userpic_svg
44-
45-
# Generate SVG string data
46-
image: str = make_userpic_svg(
48+
# Generate avatar with specific seed
49+
seeded_image = make_userpic_image(
4750
size=(7, 5),
48-
padding=(20, 10),
4951
image_size=(300, 300),
50-
background='white',
51-
foreground='black',
52+
background="white",
53+
foreground="black",
54+
seed=42 # Any integer value will work as seed
55+
)
56+
seeded_image.save("seeded_avatar.png")
57+
```
58+
59+
### 3. From Text Input (Deterministic)
60+
Generate an avatar from any text input (email, username, etc.). The same input will always produce the same avatar.
61+
62+
```python
63+
from tiny_userpic import make_userpic_image_from_string, make_userpic_svg_from_string
64+
65+
# Generate avatar from email
66+
email = "user@example.com"
67+
68+
# As PNG image
69+
image = make_userpic_image_from_string(
70+
text=email, # Input text to generate avatar from
71+
size=(7, 5), # Pattern size (width, height)
72+
image_size=(300, 300), # Output image size in pixels
73+
background="white", # Background color (can be color name, hex or RGB tuple)
74+
foreground="black" # Foreground color (can be color name, hex or RGB tuple)
5275
)
76+
image.save("avatar.png")
5377

54-
# save as SVG file
55-
with open('output.svg', 'w') as fp:
56-
fp.write(image)
78+
# As SVG
79+
svg = make_userpic_svg_from_string(
80+
text=email,
81+
size=(7, 5),
82+
image_size=(300, 300),
83+
background="white",
84+
foreground="black"
85+
)
86+
with open("avatar.svg", "w") as f:
87+
f.write(svg)
5788
```
5889

59-
## Example Output
90+
### Common Parameters
91+
All generation methods share these parameters:
92+
- `size`: Tuple of (width, height) for the pattern size
93+
- `image_size`: Tuple of (width, height) for the output image size in pixels
94+
- `background`: Background color (can be color name, hex or RGB tuple)
95+
- `foreground`: Foreground color (can be color name, hex or RGB tuple)
96+
- `padding`: Optional padding around the pattern (default: (20, 20))
97+
- `mode`: Image mode for PNG output (default: 'RGB', can be 'RGBA' for transparency)
98+
99+
## Examples
100+
101+
### Basic (from string)
102+
![Basic example](examples/basic.png)
103+
104+
### Colored
105+
![Colored example](examples/colored.png)
106+
107+
### Transparent
108+
![Transparent example](examples/transparent.png)
109+
110+
### Small
111+
![Small example](examples/small.png)
112+
113+
### Large
114+
![Large example](examples/large.png)
60115

61-
Check out the awesome userpic you can generate:
116+
### Random (non-deterministic)
117+
![Random example](examples/random.png)
62118

63-
![Awesome generated userpic!](example.png)
119+
### Seeded (deterministic)
120+
![Seeded example](examples/seeded.png)

example.png

-944 Bytes
Binary file not shown.

examples/basic.png

1005 Bytes
Loading

examples/colored.png

1.02 KB
Loading

examples/large.png

1.13 KB
Loading

examples/random.png

1023 Bytes
Loading

examples/seeded.png

1023 Bytes
Loading

examples/small.png

980 Bytes
Loading

examples/transparent.png

1.17 KB
Loading

0 commit comments

Comments
 (0)