I'm currently trying to perform on-chain image mutation. I'm writing the smart contract using smartpy.

The idea is:

  • The image is on-chain "Raw PPM encoded", in sp.TBytes format. Each pixel is represented by 3 consecutives RGB bytes
  • The contract has an entrypoint that takes a pseudo-random pixel from the image and changes its value

To modify a pixel of the image, I then have to use sp.slice on the image to isolate the pixel, mutate it, and then reconstruct the image by concatenating:

  1. bytes before pixel
  2. mutated pixel
  3. bytes after mutated pixel.

However, I have troubles when it comes to mutate the pixel's value.

Since there is no operator allowing arithmetic operations on sp.TBytes, I would like to transform pixel's value in sp.TNat.

I naively wanted to use sp.unpack method, but it seems that it assumes the sp.TBytes sequence contains a header.

What would then be the solution to perform some operations on a sp.TBytes sequence?

1 Answer 1


What kind of mutation do you want to feature? There is currently no arithmetic operation on bytes but you can compare and concatenate bytes which might be enough for your use case.

Or you can switch to sp.TNat by replacing splicing by Euclidean division.

Or you can wait for bitwise operators to be added (they are currently available in the Alpha version of the protocol which powers Mondaynet).

Or you can wait for operators for conversion between sp.TNat and sp.TBytes to be added.

Or you can implement the conversion yourself in SmartPy. All you need are two constant maps storing the mappings in both directions.

  • 1
    I did not decide yet which kind of manipulation I want to do, but it woul involve adding/dividing/modulating pixel values. I did not know that such features were planned for Tezos, thank you for the information. I actually found the following Utils lib doing what I waned to do. It seems they are using pack and unpack by formatting the sp.TBytes themselves. github.com/RomarQ/tezos-sc-utils/blob/main/smartpy/README.md
    – wakobu
    Nov 23, 2022 at 14:29
  • 1
    Thank you for the link ;) Jan 2, 2023 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.