# Loading custom images on sprites is tedious and unwieldy

**URL:** <https://forum.defold.com/t/loading-custom-images-on-sprites-is-tedious-and-unwieldy/10716>\
**Category:** Questions\
**Created:** [August 15, 2017, 6:52am UTC](https://forum.defold.com/t/loading-custom-images-on-sprites-is-tedious-and-unwieldy/10716 "2017-08-15T06:52:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dapetcu21](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dapetcu21/32/25120_2.png) [@dapetcu21](https://forum.defold.com/u/dapetcu21)\
**Post date:** [August 15, 2017, 6:52am UTC](https://forum.defold.com/t/loading-custom-images-on-sprites-is-tedious-and-unwieldy/10716/1 "2017-08-15T06:52:01Z")

</div>

I’m trying to make a sort of a trading card game thing for the perk selection screen of our game. Something like this:

 ![](https://forum-defold-uploads.b-cdn.net/original/2X/f/fc9f1f0a79e9a7066b9b5e10cb97df3c77506b29.png)

I want those polaroids to have custom images and that’s proving to be quite difficult.

Problem #1: Just loading a texture from a PNG requires you to jump through at least one unnecessary conversion and allocation because `resource.*` functions work with buffers and `image.load` works with strings. This is slightly better when working with GUI nodes, because `gui.new_texture` accepts a string, but the inconsistency is still annoying.

Here’s what you have to do to get an image to load:

```lua
    local data = sys.load_resource("/episodes/perks/" .. polaroid.perk .. ".png")
    if data then
      local img = image.load(data, true)
      local texture_path = go.get(msg.url(url.socket, url.path, "image"), "texture0")
      local header = {
        width = img.width,
        height = img.height,
        type = resource.TEXTURE_TYPE_2D,
        format = resource.TEXTURE_FORMAT_RGBA,
      }
     
      -- There is no API to copy a string into a buffer stream
      local src_string = img.buffer
      local rgb_buffer = buffer.create(#src_string, {
        { name = hash("data"), type=buffer.VALUE_TYPE_UINT8, count = 1 }
      })
      local stream = buffer.get_stream(rgb_buffer, hash("data"))
      for i = 1, #src_string do
        stream[i] = string.byte(src_string, i)
      end

      resource.set_texture(texture_path, header, rgb_buffer)
    end

```

Problem #2: There is no way of creating textures at runtime as you can do in GUI. I can only replace an already existing texture, which means I have to create a different empty atlas for each of those polaroids (there can be max 14 of them on the screen at the same time), costing me memory, loading time and manual labor 😆 , which would then just get replaced. Not to mention I have to juggle with a power of two empty image in those atlases just to get my texture coords to stick to [0, 1], and then scale that.

I find it great that you can even go this far, but is there any simpler way of doing this? If not, are there any plans to work on this API?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 15, 2017, 8:18pm UTC](https://forum.defold.com/t/loading-custom-images-on-sprites-is-tedious-and-unwieldy/10716/2 "2017-08-15T20:18:39Z")

</div>

Yes, that’s not an optimal workflow for sure. Being able to create a texture at run-time for game objects would be very useful. @andreas.tadic, any plans to add this?

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [March 30, 2019, 1:35pm UTC](https://forum.defold.com/t/loading-custom-images-on-sprites-is-tedious-and-unwieldy/10716/3 "2019-03-30T13:35:18Z")

</div>

Any plan for this?

---

<div class="post-metadata">

**Author:** ![sergey.lerg](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sergey.lerg/32/3970_2.png) [@sergey.lerg](https://forum.defold.com/u/sergey.lerg)\
**Post date:** [March 30, 2019, 2:59pm UTC](https://forum.defold.com/t/loading-custom-images-on-sprites-is-tedious-and-unwieldy/10716/4 "2019-03-30T14:59:10Z")

</div>

Is this what you want? [ImageLoader - native extension for JPG and PNG](https://forum.defold.com/t/imageloader-native-extension-for-jpg-and-png/42475)
