Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for custom colors #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ image.write('1234', 'out.png')
This is the APIs for your daily works. We do have built-in voice data and font
data. But it is suggested that you use your own voice and font data.

### Use Custom Colors

In order to change colors you have to specify your desired color as a tuple of Red, Green and Blue value.
Example:- `(255, 255, 0)` for yellow color, (255, 0, 0)` for red color.

```python
from captcha.image import ImageCaptcha

image = ImageCaptcha(fonts=['/path/A.ttf', '/path/B.ttf'])

data = image.generate('1234')
image.write('1234', 'out.png', bg_color=(255, 255, 0), fg_color=(255, 0, 0)) # red text in yellow background
```


## Useful Links

1. GitHub: https://github.com/lepture/captcha
Expand Down
28 changes: 21 additions & 7 deletions src/captcha/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,39 +187,53 @@ def create_captcha_image(

return image

def generate_image(self, chars: str) -> Image:
def generate_image(self, chars: str,
bg_color: ColorTuple | None = None,
fg_color: ColorTuple | None = None) -> Image:
"""Generate the image of the given characters.

:param chars: text to be generated.
:param bg_color: background color of the image in rgb format (r, g, b).
:param fg_color: foreground color of the text in rgba format (r,g,b,a).
"""
background = random_color(238, 255)
color = random_color(10, 200, random.randint(220, 255))
background = bg_color if bg_color else random_color(238, 255)
random_fg_color = random_color(10, 200, random.randint(220, 255))
color: ColorTuple = fg_color if fg_color else random_fg_color

im = self.create_captcha_image(chars, color, background)
self.create_noise_dots(im, color)
self.create_noise_curve(im, color)
im = im.filter(SMOOTH)
return im

def generate(self, chars: str, format: str = 'png') -> BytesIO:
def generate(self, chars: str, format: str = 'png',
bg_color: ColorTuple | None = None,
fg_color: ColorTuple | None = None) -> BytesIO:
"""Generate an Image Captcha of the given characters.

:param chars: text to be generated.
:param format: image file format
:param bg_color: background color of the image in rgb format (r, g, b).
:param fg_color: foreground color of the text in rgba format (r,g,b,a).
"""
im = self.generate_image(chars)
im = self.generate_image(chars, bg_color=bg_color, fg_color=fg_color)
out = BytesIO()
im.save(out, format=format)
out.seek(0)
return out

def write(self, chars: str, output: str, format: str = 'png') -> None:
def write(self, chars: str, output: str, format: str = 'png',
bg_color: ColorTuple | None = None,
fg_color: ColorTuple | None = None) -> None:
"""Generate and write an image CAPTCHA data to the output.

:param chars: text to be generated.
:param output: output destination.
:param format: image file format
:param bg_color: background color of the image in rgb format (r, g, b).
:param fg_color: foreground color of the text in rgba format (r,g,b,a).
"""
im = self.generate_image(chars)
im = self.generate_image(chars, bg_color=bg_color, fg_color=fg_color)
im.save(output, format=format)


Expand Down