# Get pixel array of a Char

**URL:** <https://discourse.julialang.org/t/get-pixel-array-of-a-char/106426>\
**Category:** General Usage\
**Tags:** images, array, char\
**Created:** [November 19, 2023, 1:56am UTC](https://discourse.julialang.org/t/get-pixel-array-of-a-char/106426 "2023-11-19T01:56:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yly](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yly](https://discourse.julialang.org/u/Yly)\
**Post date:** [November 19, 2023, 1:56am UTC](https://discourse.julialang.org/t/get-pixel-array-of-a-char/106426/1 "2023-11-19T01:56:27Z")

</div>

I am doing image processing work where I need to be able to manipulate images as arrays. I would like to make an array with values 0 or 1 such that the 1’s make the shape of a given letter. It is proving surprisingly difficult to find an elegant way to do this.

An example of what I would like:

```
function makeLetter(c::Char,nPixels::Int)
    # code....
end

makeLetter('o',10) 

 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 1 1 1 1 0 0 0
 0 0 1 1 0 0 1 1 0 0
 0 0 1 1 0 0 1 1 0 0
 0 0 1 1 0 0 1 1 0 0
 0 0 1 1 0 0 1 1 0 0
 0 0 0 1 1 1 1 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0

```

I can imagine some clunky ways to do this, like saving .pngs of all the letters and then importing them and converting to arrays. Is there a better way using some handy Julia package? Preferably it would not involve saving auxiliary files.

(Remark: Having the array values be 0 or 1 is just for simplicity. If they are e.g. `Float`s or `UInt`s in some other range, that’s fine.)

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [November 19, 2023, 4:19am UTC](https://discourse.julialang.org/t/get-pixel-array-of-a-char/106426/2 "2023-11-19T04:19:53Z")

</div>

You could use [GitHub - JuliaGraphics/FreeTypeAbstraction.jl: A Julian abstraction layer over FreeType.jl](https://github.com/JuliaGraphics/FreeTypeAbstraction.jl) for that

---

<div class="post-metadata">

**Author:** ![Yly](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yly](https://discourse.julialang.org/u/Yly)\
**Post date:** [November 21, 2023, 9:08pm UTC](https://discourse.julialang.org/t/get-pixel-array-of-a-char/106426/3 "2023-11-21T21:08:00Z")

</div>

The suggestion of ericphanson worked with some modifications, which I’m recording here for posterity (the package linked has no documentation, but works wonderfully once you figure it out). I followed the description in the package readme, but got an error when I ran the line `face = FTFont("hack_regular.ttf")`. There was no such file. I downloaded the required file from [this link](https://gitlab.eyeskills.org/community/godot/blob/965185c765bf5d17f0f035b7685dbaf4611e5288/thirdparty/fonts/Hack_Regular.ttf). Then I replaced the above call with `face = FTFont("path\\to\\file")`.

The next line of the readme, `img, metric = renderface(face, 'C')`, also errored. It turns out it requires a third argument which is an integer. This has something to do with the image size, though it’s not exactly the size in pixels. Anyhow, with something like `img, metric = renderface(face, 'C',100)`, I got a result.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [November 21, 2023, 9:30pm UTC](https://discourse.julialang.org/t/get-pixel-array-of-a-char/106426/4 "2023-11-21T21:30:49Z")

</div>

I have suggested [this improvement](https://github.com/JuliaGraphics/FreeTypeAbstraction.jl/pull/80) to the readme based on your feedback, hopefully that will help clarify it for the next person!

---

<div class="post-metadata">

**Author:** ![Yly](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yly](https://discourse.julialang.org/u/Yly)\
**Post date:** [November 21, 2023, 10:43pm UTC](https://discourse.julialang.org/t/get-pixel-array-of-a-char/106426/5 "2023-11-21T22:43:43Z")

</div>

Thanks! However, FYI part of your suggested change also does not work for me. `findfont("juliamono")` returns `nothing`, and it is unclear to me how to remedy this.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [November 21, 2023, 10:59pm UTC](https://discourse.julialang.org/t/get-pixel-array-of-a-char/106426/6 "2023-11-21T22:59:18Z")

</div>

ah ok, in that case you need to choose the name of a font installed on your system. I’ll update my PR to add a comment. `juliamono` is just an example
