How to Save a Tiff File of Any Size in Golang

Asked 2 years ago, Updated 2 years ago, 64 views

We analyze images using Golang.

Currently, we can load the tiff file (input) as Matrix and save the same size file as the matrix as Tiff.

However, I think it is possible to save any size of Matrix as tiff, but
I have a question here because I stuck it when I created the code.

By the way, I understand how to save it in another format, such as PNG, but for some reason I can't just use Tiff.

I will write down the current code here, so I would appreciate it if you could comment on where to change the above objectives.

func SaveintIFF_Color(FName string,Img*[][]int,memo string){
    data,_ —=ioutil.ReadFile(FName)
    img,_:=tiff.Decode(bytes.NewReader(data))

    im: = img. (*image.RGBA)

    s: = im.Bounds().Size();
    MaxColSize: =s.X
    MaxRowSize: =s.Y

    variable,ix,value int;
    for iy=0;iiy<MaxRowSize;iiy++{
        forix=0;ix<MaxColSize;ix++{
            value=(*Img)[iy][ix];
            im.Pix [4*(iy*MaxColSize+ix)+0] = uint8(value)//red signal
            im.Pix[4*(iy*MaxColSize+ix)+1] = uint8(value)//green signal
            im.Pix [4*(iy*MaxColSize+ix)+2] = uint8(value)//blue signal
        }
    }

    toimg,_:=os.Create(FName+"."+memo+".new.tif")
    if(memo==""){
        toimg,_=os.Create(FName+".new.tif")
    }
    prefer toimg.Close()

    err —=tiff.Encode (toimg, img, nil)
    if err!=nil {panic(err)}
}

May I speak to you?

------------------------------------------------------------------------------------------------------------------------------ There was not enough explanation, so I would like to add it here again:

I think it will be easier to understand the situation, so I will talk about the process a little bit.
FName="sample.tif" (10x10pixel)
Img=10x10 matrix (as a result of sample.tif being processed such as denoising)
Let's say

In this assumption, the sample.tif has only been processed a little, so Img is the same size matrix as the sample.tif.This process can be handled with the above code.

However, in the end, I would like to modify this code and do the following:
FName="sample.tif" (size 10x10pixel)
Img = 50x20 matrix (as a result of changing the matrix size by applying image processing to sample.tif)

In the code above,

data,_:= Read ioutil.ReadFile(FName)//sample.tif
img,_:=tiff.Decode(bytes.NewReader(data))//decode to tiff        
im: = img. (*image.RGBA) // Specify RGBA

In this part, the size of the file is specified as 10x10, and
As a result, the im.Pix size is fixed.
Therefore, if the Img is 10x10, it is OK, but other sizes are not allowed.

In my opinion, it would be better if I could change the size of this im.Pix.
I don't know what part of the code above will be modified.

If you know, please let me know.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- I think the explanation of this question is not clear, so I will add it again.
I am very sorry.

Same thing, but simplify the problem.

The goal is to save any size of 2D Matrix (slice type) to tif.Actually, I would like to create a function that can be saved on 10x10 Matrix or 50x20 matrix.

However, there are two problems with the code above.

1. In the process of loading sample.tif, the size regulation was included, so there is no degree of freedom in the size.

2. Also, bytes.NewReader only accepts [] bytes.
You can change the 2D matrix ([][]byte) to 1D ([]byte), but if you save it to tif, it will remain as data, but you will not be able to see the tif file as an image.

How can I avoid this problem and save any size of 2D matrix to tif?

There was something wrong with the explanation.Also, I am very grateful for your patient consideration of my response.May I speak to you?

go

2022-09-30 19:44

1 Answers

I don't want to, but I found a way to use the current script.

Before saving a matrix of any size, prepare a tifimage of the same size as that matrix.

For example, you can create a 200x100 tif using the command (linux+ImageMagic) below.

convert sample.tif-resize 200x100!200x100.tif

Run this command in the script to create a sample.
After that, you can change the file name a little with the above function.

But I think there is a more elegant way.May I speak to you?


2022-09-30 19:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.