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
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() } - oneAlthough 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
Result for different interpolation methods when halved
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.
