# How to programmatically create and save an image with some text?

**URL:** https://discourse.julialang.org/t/how-to-programmatically-create-and-save-an-image-with-some-text/55086
**Category:** General Usage
**Created:** [February 11, 2021, 8:24pm UTC](https://discourse.julialang.org/t/how-to-programmatically-create-and-save-an-image-with-some-text/55086 "2021-02-11T20:24:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [February 11, 2021, 8:24pm UTC](https://discourse.julialang.org/t/how-to-programmatically-create-and-save-an-image-with-some-text/55086/1 "2021-02-11T20:24:12Z")

</div>

I would like to programmatically “compose” and save as PNG an image with a white background and a digit in the center, of a given font, colour, size.

Which approach may I have? I am thinking to plots, but it seems “exaggerated” for what I need…

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [February 11, 2021, 8:33pm UTC](https://discourse.julialang.org/t/how-to-programmatically-create-and-save-an-image-with-some-text/55086/2 "2021-02-11T20:33:06Z")

</div>

Seems that Luxor does exactly this: [http://juliagraphics.github.io/Luxor.jl/stable/examples/](http://juliagraphics.github.io/Luxor.jl/stable/examples/)

I’ll give it a try …

---

<div class="post-metadata">

### Author: ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)
#### Post date: [February 12, 2021, 3:47am UTC](https://discourse.julialang.org/t/how-to-programmatically-create-and-save-an-image-with-some-text/55086/3 "2021-02-12T03:47:25Z")

</div>

compose.jl can do it too: [Forms · Compose.jl](https://giovineitalia.github.io/Compose.jl/latest/gallery/forms/#%5Btext%5D(@ref))

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [February 12, 2021, 9:04am UTC](https://discourse.julialang.org/t/how-to-programmatically-create-and-save-an-image-with-some-text/55086/4 "2021-02-12T09:04:48Z")

</div>

In Luxor I have problems to center the text string: [How to center a text with Luxor - #2 by cormullion](https://discourse.julialang.org/t/how-to-center-a-text-with-luxor/55125/2)

So I went back to Compose, but not speaking Lisp, it looks very scaring.  
I have tried this, but the text doesn’t go on top of the rectangle:

```julia

using Colors, Compose
set_default_graphic_size(300px,300px)
img = compose(context(units=UnitBox(0,0,2,2)),
  compose(compose(context(), rectangle()), fill("white")),
  (context(), text(1, 1, "123", hcenter, vcenter), stroke("red"), fontsize(30pt)),
)

```

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [February 12, 2021, 9:06am UTC](https://discourse.julialang.org/t/how-to-programmatically-create-and-save-an-image-with-some-text/55086/5 "2021-02-12T09:06:39Z")

</div>

ok, got it with:

```julia
using Colors, Compose
set_default_graphic_size(300px,300px)
img = compose(context(units=UnitBox(0,0,2,2)),
  (context(), text(1, 1, "123", hcenter, vcenter), stroke("black"), fontsize(160pt)),
  compose(compose(context(), rectangle()), fill("white")),
)

```

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [February 12, 2021, 9:29am UTC](https://discourse.julialang.org/t/how-to-programmatically-create-and-save-an-image-with-some-text/55086/6 "2021-02-12T09:29:06Z")

</div>

Still I can’t change the font with Compose… `font("Helvetica-Oblique")` or `font("CMU-Sans-Serif-Medium")` seems not to have any effect.

Also, the exported PNG looks different than the preview in Juno.

EDIT: good about the last point… for some reasons the preview always display a serif font, but the PNG exported with `img |> PNG("hello-world4.png")` always draws with a sans-serif font that is what I want…

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [February 12, 2021, 4:34pm UTC](https://discourse.julialang.org/t/how-to-programmatically-create-and-save-an-image-with-some-text/55086/7 "2021-02-12T16:34:29Z")

</div>

With GMT

```julia
using GMT

text(["Olá"], x=5, y=5, region=(0,10,0,10), font=(40,:red), justify=:CM, frame=(grid=5, fill=254), fmt=:png, show=true)

```

Replace `frame=(grid=5, fill=254)` by `frame=(fill=254,)` if you don’t want the frame and the grid. It was only to show the text positioning.

 ![GMTjl_tmp](https://global.discourse-cdn.com/julialang/original/3X/2/8/2881c615fe726777173ba2d39c8749e404a30edf.png)
