# Generate a simple digits captcha image

**URL:** <https://forum.crystal-lang.org/t/generate-a-simple-digits-captcha-image/8148>\
**Category:** Help & Support\
**Created:** [June 12, 2025, 10:55am UTC](https://forum.crystal-lang.org/t/generate-a-simple-digits-captcha-image/8148 "2025-06-12T10:55:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [June 12, 2025, 10:55am UTC](https://forum.crystal-lang.org/t/generate-a-simple-digits-captcha-image/8148/1 "2025-06-12T10:55:30Z")

</div>

There are some generate PNG shards. but i don’t know how to make image with text in it.

> [@PNG implementation in Crystal](https://forum.crystal-lang.org/t/png-implementation-in-crystal/6652):
>
> Hi everyone, I wanted to share my PNG implementation in crystal: [GitHub - SleepingInsomniac/png: PNG implementation in Crystal](https://github.com/SleepingInsomniac/png). I started working on it to support another project since I didn’t see an existing project with support for all the bit depths, color types, paletted images etc… It was a pretty fun learning experience, and I’m wondering what others think.

I found some example code in [pixie](https://github.com/watzon/pixie) shards works for what i need, but the example code not work on my Arch Linux, check [this](https://github.com/watzon/pixie/issues/2) issue.

Right now, I just want to make a small, simple picture with some numbers on it. It will be used as a simple check when people sign up on my website.

Thanks.

---

<div class="post-metadata">

**Author:** ![ralsina](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ralsina/32/2139_2.png) [@ralsina](https://forum.crystal-lang.org/u/ralsina)\
**Post date:** [June 12, 2025, 12:15pm UTC](https://forum.crystal-lang.org/t/generate-a-simple-digits-captcha-image/8148/2 "2025-06-12T12:15:30Z")

</div>

This file has code doing it. If you want I can do a more minimal example.

> <https://github.com/ralsina/tartrazine/blob/main/src/formatters/png.cr>

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [June 12, 2025, 1:11pm UTC](https://forum.crystal-lang.org/t/generate-a-simple-digits-captcha-image/8148/3 "2025-06-12T13:11:57Z")

</div>

Thanks a lot, will be check later.

I found another working solution, which use crystal-vips.

> <https://github.com/naqvis/crystal-vips/blob/main/example/captcha_generator.cr>

![4560f5ede927992c21343aff21d3c440](https://canada1.discourse-cdn.com/flex036/uploads/crystal_lang/original/2X/6/64881eb287006b674dcfb6135695f395c4f5d92b.png)

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [June 12, 2025, 5:20pm UTC](https://forum.crystal-lang.org/t/generate-a-simple-digits-captcha-image/8148/4 "2025-06-12T17:20:38Z")

</div>

Create a `captcha` shard use [naqvis/crystal-vips](https://github.com/naqvis/crystal-vips/blob/main/example/captcha_generator.cr)

> **[GitHub - crystal-china/captcha: Crystal library that generates image CAPTCHAs.](https://github.com/crystal-china/captcha)**
>
> Crystal library that generates image CAPTCHAs.

All credits goes to [the example code in crystal-vips](https://github.com/naqvis/crystal-vips/blob/0f4d3914985865a020168b0f48ece07416eeb459/example/captcha_generator.cr) by @naqvis

---

<div class="post-metadata">

**Author:** ![syeopite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/syeopite/32/1420_2.png) [@syeopite](https://forum.crystal-lang.org/u/syeopite)\
**Post date:** [June 12, 2025, 6:37pm UTC](https://forum.crystal-lang.org/t/generate-a-simple-digits-captcha-image/8148/5 "2025-06-12T18:37:53Z")

</div>

I don’t think this will be viable for a text-based captcha implementation but for the general case where the images looks relatively the same you could get away with just creating SVGs and then converting to a PNG.

The clock captchas that Invidious uses during registrations are generated this way by manipulating SVG attributes on each picture:

> <https://github.com/iv-org/invidious/blob/df8839d1f018644afecb15e144f228d811708f8f/src/invidious/user/captcha.cr#L22-L47>

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [June 15, 2025, 6:26am UTC](https://forum.crystal-lang.org/t/generate-a-simple-digits-captcha-image/8148/6 "2025-06-15T06:26:14Z")

</div>

> [@zw963](#):
>
> Create a `captcha` shard use [naqvis/crystal-vips](https://github.com/naqvis/crystal-vips/blob/main/example/captcha_generator.cr)

There is a sister shard, name [simple\_captcha](https://github.com/crystal-china/simple_captcha)

All credits goes to @Sunrise

The different is:

- SimpleCaptcha has no external dependencies other than Crystal，so, you can  
built static binary (if this is your’s need)
- this shard captcha code only support number.

These two libraries should be interchangeable without the need to change you code.

![image](https://canada1.discourse-cdn.com/flex036/uploads/crystal_lang/original/2X/0/04d9cc078cb8ba4a0addce2e0a44141268e61b70.png)
