Save texture file with same or smaller file size?


I added a texture in my assets folder.

The texture is created with a free raster graphics editor and is exported as an 8-bit grayscale texture.
The files size is 21 KB.
Texture size is 2048 x 2048px
Here is the texture;

Inspector settings of the texture:

I've created a script, which gets the texture and saves it.
The problem is that the saved image has a file size of 55 KB.

When I save the same image again in the raster graphics editor as a *.png file format the file size stays the same (21 KB).

Here is the script:

I use SetPixels32() in the code. "m_textureFormat" can only be these Texture Formats:
"You can use SetPixels with the following texture formats:"

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Experimental.Rendering;

public class SaveTextureTest : MonoBehaviour

    public Texture2D textureSource;

    public TextureFormat m_textureFormat;

    public GraphicsFormat m_graphicsFormat;

    void Start()

    public void SaveTexture()

        Texture2D tmpTexture = new Texture2D(textureSource.width, textureSource.height, m_textureFormat, false);

        Color32[] pixels = textureSource.GetPixels32();


        // Test 1
        byte[] bytes = ImageConversion.EncodeToPNG(tmpTexture);

        // Test 2
        // using UnityEngine.Experimental.Rendering;
        //byte[] bytes = ImageConversion.EncodeArrayToPNG(tmpTexture.GetRawTextureData(), m_graphicsFormat, (uint)textureSource.width, (uint)textureSource.height);

        // Unity Editor: <path to project folder>/Assets
        File.WriteAllBytes(Application.dataPath + "/SavedTexture.png", bytes);



How can I save the texture with the same file size (or if possible smaller file size)?

The texture will only contain black or white colors. No antialiasing.


If it's just black or white I suggest using a single-channel texture format, probably R8.
If it is just 2 colors you could find some way to export it directly to the GIF format, as that might be the most efficient format to locally store images with very limited colors.

But first, just use any format to get a base working. The optimization should not be the first step for this unless you need to those thousands or millions of images.
This might help (mostly 2nd answer): android - Unity c#, take screenshot and save to file as jpg - Stack Overflow