Skip to content

Commit

Permalink
initial commit 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
trumully committed Apr 20, 2024
0 parents commit f9727ec
Show file tree
Hide file tree
Showing 14 changed files with 914 additions and 0 deletions.
160 changes: 160 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Profile Picture Generator
Generate a random profile picture based on given text!

## 🔎 Examples
<table>
<tr>
<th><img src="examples/truman.png" alt="truman"/ width="90%"></th>
<th><img src="examples/trumully.png" alt="trumully" width="90%"/></th>
<th><img src="examples/github.png" alt="github" width="90%"></th>
<th><img src="examples/python is awesome.png" alt="python is awesome" width="90%"></th>
</tr>
<tr>
<th>truman</th>
<th>trumully</th>
<th>github</th>
<th>python is awesome</th>
</tr>
</table>

## 🪄 Quick Start
```shell
pfp-generator [-h] [-s SIZE] [-bg BACKGROUND] [-c COLOR] [-w COLOR_WEIGHT] [--save] [text]

Generate profile pictures.

positional arguments:
text Text to generate the profile picture from. If left blank, generate a random profile picture.

options:
-h, --help show this help message and exit
-s SIZE, --size SIZE The size of the base pattern. Defaults to 5.
-bg BACKGROUND, --background BACKGROUND
The color of the background.
-c COLOR, --color COLOR
The color of the profile picture.
-w COLOR_WEIGHT, --color-weight COLOR_WEIGHT
The weight of the profile picture color.
--save Ask to save the profile picture.
```

## 📦 Setting Up
### Prerequisites
[`pipx`](https://pipx.pypa.io/stable/installation/) or `pip` is required. Using `pipx` is *highly* recommended.

### Installation
With `pipx`:
```shell
pipx install git+https://github.com/trumully/pfp_generator
```

With `pip` (not recommended):
```shell
python -m pip install -U git+https://github.com/trumully/pfp_generator
```

## 🧰 Development
### Prerequisites
Install `poetry` with `pipx`:
```shell
pipx install poetry
```
### Installing
Clone the repository:
```shell
git clone https://github.com/trumully/pfp_generator.git
```
Activate the virtual environment:
```shell
cd pfp_generator
poetry shell
```
Install dependencies:
```shell
poetry install
```

## 🧬 Tests
Run tests with `pytest`
```shell
poetry run pytest
```
Binary file added examples/github.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/python is awesome.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/truman.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/trumully.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added pfp_generator/__init__.py
Empty file.
91 changes: 91 additions & 0 deletions pfp_generator/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""This module contains the CLI for the profile picture generator."""

import argparse
import random

from PIL import ImageColor

from pfp_generator.display import display_pfp
from pfp_generator.generate import ColorMatrix


def get_random_color(bg_color: str) -> str:
"""Get a random color that is not the background color.
Args:
bg_color (str): The background color.
Returns:
str: A random color that is not the background color.
"""
colors = [c for c in list(ImageColor.colormap) if c != bg_color]
return random.choice(colors)


def main() -> None:
"""The main function for the CLI."""
parser = argparse.ArgumentParser(
prog="pfp-generator", description="Generate profile pictures."
)

parser.add_argument(
"text",
type=str,
nargs="?",
default=None,
help="Text to generate the profile picture from. If left blank, generate a "
"random profile picture.",
)

parser.add_argument(
"-s",
"--size",
type=int,
default=5,
help="The size of the base pattern. Defaults to 5.",
)
parser.add_argument(
"-bg",
"--background",
type=str,
default="white",
help="The color of the background.",
)
parser.add_argument(
"-c",
"--color",
type=str,
default="",
help="The color of the profile picture.",
)
parser.add_argument(
"-w",
"--color-weight",
type=float,
default=0.35,
help="The weight of the profile picture color.",
)
parser.add_argument(
"--save",
action="store_true",
help="Ask to save the profile picture.",
)
args = parser.parse_args()

if args.text is not None:
random.seed(args.text)

args.color = args.color or get_random_color(args.background)

bg_weight = 1 - args.color_weight
color_matrix = ColorMatrix(
size=args.size,
color=[ImageColor.getrgb(args.background), ImageColor.getrgb(args.color)],
color_weight=[bg_weight, args.color_weight],
seed=args.text,
)
display_pfp(color_matrix, save=args.save)


if __name__ == "__main__":
main()
52 changes: 52 additions & 0 deletions pfp_generator/display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Display a profile picture pattern with a given color matrix."""

from tkinter.filedialog import asksaveasfilename

from PIL import Image

from pfp_generator.generate import ColorMatrix, generate_pfp


def display_pfp(
color_matrix: ColorMatrix,
*,
size: int = 256,
save: bool = False,
) -> None:
"""Display a profile picture pattern with a given color matrix.
Args:
color_matrix (ColorMatrix): The color matrix for the profile picture.
size (int): The size of the profile picture. Defaults to 100.
save (bool): A flag to save the profile picture. Defaults to False.
"""
pattern = generate_pfp(color_matrix)
name = color_matrix.seed or "pfp"
pattern_size = color_matrix.size * 2
image = Image.new("RGB", (pattern_size, pattern_size), "white")
pixels = image.load()

for i, row in enumerate(pattern):
for j, color in enumerate(row):
pixels[j, i] = color

image = image.resize((size, size), Image.NEAREST)
image.show()
if save:
save_file(image, name)


def save_file(image: Image.Image, name: str) -> None:
"""Save an image file.
Args:
image (Image.Image): The image to save.
name (str): The name of the image.
"""
filename = asksaveasfilename(
filetypes=[("PNG files", "*.png"), ("JPEG files", "*.jpg")],
defaultextension=".png",
initialfile=name,
)
if filename:
image.save(filename)
Loading

0 comments on commit f9727ec

Please sign in to comment.