Skip to content

Commit 7e22b8c

Browse files
authored
feat: support for custom colors (#76)
* support for custom colors * linting fixed * really fixed linting for flake8. --------- Co-authored-by: dshaw0004 <dipankarshaw692@gmail.com>
1 parent bd892b7 commit 7e22b8c

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ image.write('1234', 'out.png')
4141
This is the APIs for your daily works. We do have built-in voice data and font
4242
data. But it is suggested that you use your own voice and font data.
4343

44+
### Use Custom Colors
45+
46+
In order to change colors you have to specify your desired color as a tuple of Red, Green and Blue value.
47+
Example:- `(255, 255, 0)` for yellow color, (255, 0, 0)` for red color.
48+
49+
```python
50+
from captcha.image import ImageCaptcha
51+
52+
image = ImageCaptcha(fonts=['/path/A.ttf', '/path/B.ttf'])
53+
54+
data = image.generate('1234')
55+
image.write('1234', 'out.png', bg_color=(255, 255, 0), fg_color=(255, 0, 0)) # red text in yellow background
56+
```
57+
58+
4459
## Useful Links
4560

4661
1. GitHub: https://github.com/lepture/captcha

src/captcha/image.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,39 +187,53 @@ def create_captcha_image(
187187

188188
return image
189189

190-
def generate_image(self, chars: str) -> Image:
190+
def generate_image(self, chars: str,
191+
bg_color: ColorTuple | None = None,
192+
fg_color: ColorTuple | None = None) -> Image:
191193
"""Generate the image of the given characters.
192194
193195
:param chars: text to be generated.
196+
:param bg_color: background color of the image in rgb format (r, g, b).
197+
:param fg_color: foreground color of the text in rgba format (r,g,b,a).
194198
"""
195-
background = random_color(238, 255)
196-
color = random_color(10, 200, random.randint(220, 255))
199+
background = bg_color if bg_color else random_color(238, 255)
200+
random_fg_color = random_color(10, 200, random.randint(220, 255))
201+
color: ColorTuple = fg_color if fg_color else random_fg_color
202+
197203
im = self.create_captcha_image(chars, color, background)
198204
self.create_noise_dots(im, color)
199205
self.create_noise_curve(im, color)
200206
im = im.filter(SMOOTH)
201207
return im
202208

203-
def generate(self, chars: str, format: str = 'png') -> BytesIO:
209+
def generate(self, chars: str, format: str = 'png',
210+
bg_color: ColorTuple | None = None,
211+
fg_color: ColorTuple | None = None) -> BytesIO:
204212
"""Generate an Image Captcha of the given characters.
205213
206214
:param chars: text to be generated.
207215
:param format: image file format
216+
:param bg_color: background color of the image in rgb format (r, g, b).
217+
:param fg_color: foreground color of the text in rgba format (r,g,b,a).
208218
"""
209-
im = self.generate_image(chars)
219+
im = self.generate_image(chars, bg_color=bg_color, fg_color=fg_color)
210220
out = BytesIO()
211221
im.save(out, format=format)
212222
out.seek(0)
213223
return out
214224

215-
def write(self, chars: str, output: str, format: str = 'png') -> None:
225+
def write(self, chars: str, output: str, format: str = 'png',
226+
bg_color: ColorTuple | None = None,
227+
fg_color: ColorTuple | None = None) -> None:
216228
"""Generate and write an image CAPTCHA data to the output.
217229
218230
:param chars: text to be generated.
219231
:param output: output destination.
220232
:param format: image file format
233+
:param bg_color: background color of the image in rgb format (r, g, b).
234+
:param fg_color: foreground color of the text in rgba format (r,g,b,a).
221235
"""
222-
im = self.generate_image(chars)
236+
im = self.generate_image(chars, bg_color=bg_color, fg_color=fg_color)
223237
im.save(output, format=format)
224238

225239

0 commit comments

Comments
 (0)