You need to take a large image file and create a small copy of it with a certain height and width.
How to do this?
thank

    3 answers 3

    I would take imagemagick - there is a binding to go https://github.com/gographics/imagick . And then - reading the documentation and studying the example - https://github.com/gographics/imagick/blob/master/examples/resize/main.go

    But what width and height to set is already up to you.

      Here is a library without binders: https://github.com/nfnt/resize

      A simple example of use:

      package main import ( "image/jpeg" "os" "github.com/nfnt/resize" ) func main(){ imgIn, _ := os.Open("test.jpg") imgJpg, _ := jpeg.Decode(imgIn) imgIn.Close() imgJpg = resize.Resize(600, 0, imgJpg, resize.Bicubic) // <-- БобствСнно ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ imgOut, _ := os.Create("test-out.jpg") jpeg.Encode(imgOut, imgJpg,nil) imgOut.Close() } 
      • one
        Although the link can find the answer to the question, it is better to point out the most important thing here, and give the link as a source. If the page to which the link leads will be changed, the response link may become invalid. - From the queue of checks - Vasily Barbashev
      • This is a link to the library for resizing images, in fact, it is the main one. If the page ceases to exist, then the library will cease to exist and the answer will become irrelevant. It will be necessary to find another library. - rekby
      • Deployed, high-quality response. This goal is pursued by SO. What is under the link is the right answer, I have no doubt) You can describe the library, why it is, its best / worst qualities. Something like that :) - Vasily Barbashev
      • Thank you @rekby. github.com/nfnt/resize liked more, because. imagick could not be installed, he wrote a campaign for C and demanded a bunch of packages, and could not put it on Windows - Rakzin Roman
      • Hello Roman. Then tick the answer as the answer - green check. - rekby

      There is a golang.org/x/image/draw package that allows this.

      When you resize an image, distortion and data loss occur. To achieve the desired effect, one or another interpolation method is used. Some methods give good performance - other quality. The above package has four interpolation methods built in:

      • NearestNeighbor - nearest neighbor interpolation. The easiest and fastest algorithm. However, usually the result is poor quality. With the increase, the result becomes "square"
      • ApproxBiLinear is a mix between NearestNeighbor and BiLinear. It is fast, but usually gives an average quality result
      • BiLinear is slow, but gives good results.
      • CatmullRom uses Catmull-Roma spline (English wiki ). Very slow, but gives a very good result.

      CatmullRom Conversion Example

       import ( "image" "golang.org/x/image/draw" ) func main() { // ΠΎΡ‚ΠΊΡ€Ρ‹Ρ‚ΠΈΠ΅ изобраТСния источника // Π»ΡŽΠ±Ρ‹ΠΌ ΡƒΠ³ΠΎΠ΄Π½Ρ‹ΠΌ способом src := openImage() // создаём пустоС ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ для // записи Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠ³ΠΎ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° dst := image.NewRGBA(image.Rect(0, 0, 200, 200)) // ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° draw.CatmullRom.Scale(dst, dst.Bounds(), src, src.Bounds(), draw.Over, nil) // Π³ΠΎΡ‚ΠΎΠ²ΠΎ // Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ dst содСрТит ΠΈΡ‚ΠΎΠ³ΠΎΠ²ΠΎΠ΅ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ } 

      Image for example enter image description here Result for different interpolation methods when halved

      enter image description here

      On dual-core Intel Core i3 M380 2.53GHz, the result of halving the time:

       | ΠΌΠ΅Ρ‚ΠΎΠ΄ | врСмя | |:---------------:|--------------:| | NearestNeighbor | 7.935489ms | | ApproxBiLinear | 14.876478ms | | BiLinear | 113.903067ms | | CatmullRom | 115.972061ms | 

      A complete example of the application used can be found at gist.github.com . Using this example and various sets of source images, you can, after looking at the results, select the optimal interpolation method for your task.