disintegration / imaging

Imaging is a simple image processing package for Go
MIT License
5.28k stars 440 forks source link

How to crop file same as `imaging.Fit` without quality loss? #114

Closed mesuutt closed 3 years ago

mesuutt commented 4 years ago

I want to use imaging.Fit without filtering. When I use .Fit image quality falling. I want to crop file same as .Fit without loss qualty. How can I do this?

disintegration commented 4 years ago

I want to use imaging.Fit without filtering.

To disable filtering use imaging.NearestNeighbor as the last parameter of the imaging.Fit function.

mesuutt commented 4 years ago

Hi @disintegration, thanks for your reply.

I used imaging.NearestNeighbor but image qualty still poor. Maybe this is not related imaging but I don't found any issue at code. I want to crop max 640x640 and save.

img, _, err := image.Decode(bytes.NewReader(*fileContentBytes))
if err != nil {
    return err
}

thumbnailImg := imaging.Fit(img, 640, 640, imaging.NearestNeighbor)
f, err := os.Create("foo.jpeg")
if err != nil {
    return err
}

defer f.Close()
buf := new(bytes.Buffer)
if err := jpeg.Encode(buf, thumbnailImg, &jpeg.Options{Quality:100}); err != nil {
    return err
}
_, err = f.Write(*file.Content)
if err != nil {
    return err
}

Original Image: Belinda   Nan-ccf4df

New created image( there are white dots in image): belinda-and-nan

disintegration commented 4 years ago

Are you sure you've attached the right original image? It's already 640 pixels wide so Fit does essentially nothing.

Here's the code i've used:

package main

import (
    "log"

    "github.com/disintegration/imaging"
)

func main() {
    src, err := imaging.Open("src.jpg")
    if err != nil {
        log.Fatal(err)
    }
    dst := imaging.Fit(src, 640, 640, imaging.NearestNeighbor)
    if err := imaging.Save(dst, "dst.jpg"); err != nil {
        log.Fatal(err)
    }
}

And the result:

dst

I believe the original image in your example was larger.

You asked for no-filtering mode, but for this type of image (photography) you won't get a high quality result by disabling filtering, quite the opposite. My recommendation is to use the CatmullRom or Lanczos filter.

disintegration commented 3 years ago

Closing the issue. Feel free to reopen if you have any further questions.