h2non / bimg

Go package for fast high-level image processing powered by libvips C library
https://pkg.go.dev/github.com/h2non/bimg?tab=doc
MIT License
2.71k stars 339 forks source link

The new image is missing "Color Profile: Display P3" after WatermarkImage #148

Open wangjinbei opened 7 years ago

wangjinbei commented 7 years ago

go env:

go version go1.6.3 linux/amd64 GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/usr/local/src/gocode" GORACE="" GOROOT="/usr/lib/golang" GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64" GO15VENDOREXPERIMENT="1" CC="gcc" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0" CXX="g++" CGO_ENABLED="1"

Code:

    buffer, _ := bimg.Read("1.jpeg")
    bufferWater, _ := bimg.Read("1.png")
    watermark := bimg.WatermarkImage{
        Left:    0,
        Top:     0,
        Buf:     bufferWater,
        Opacity: 0.25,
    }
    newImage, _ := bimg.NewImage(buffer).WatermarkImage(watermark)
    bimg.Write("new.jpg", newImage)

Result: new.jpg missing "Color Profile: Display P3" , And there is a slight change in color

1.jpeg info: image

new.jpg info: image

ajdevries commented 7 years ago

With what version did you test this? I rewrote the image watermarking, and can look into this.

wangjinbei commented 7 years ago

const Version = "1.0.7"

ajdevries commented 7 years ago

Can you give this version a try?

wangjinbei commented 7 years ago

It's not work~~ After : "go get -u github.com/h2non/bimg"

ajdevries commented 7 years ago

You probably used master?

Because

wangjinbei commented 7 years ago

yes! bimg.v1 doesn't support image watermarking

package main

import (
    "fmt"
    "os"

    "github.com/h2non/bimg"
)

func main() {
    buffer, err := bimg.Read("/Users/william/gocode/src/awesomeProject/imagek/1.jpeg")
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    }
    bufferWater, err := bimg.Read("/Users/william/gocode/src/awesomeProject/imagek/1.png")
    watermark := bimg.WatermarkImage{
        Left:    0,
        Top:     0,
        Buf:     bufferWater,
        Opacity: 0.25,
    }
    newImage, err := bimg.NewImage(buffer).WatermarkImage(watermark)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    }

    bimg.Write("new.jpg", newImage)
}
ajdevries commented 7 years ago

So to try the PR change your import to github.com/waldophotos/bimg (go get github.com/waldophotos/bimg)

wangjinbei commented 7 years ago

can not work...

package main

import (
        "fmt"
        "os"

        "github.com/waldophotos/bimg"
)

func main() {
        buffer, err := bimg.Read("1.jpeg")
        if err != nil {
                fmt.Fprintln(os.Stderr, err)
        }
        bufferWater, err := bimg.Read("1.png")
        watermark := bimg.WatermarkImage{
                Left:0,
                Top:0,
                Buf:bufferWater,
                Opacity:    1,
        }
        newImage, err := bimg.NewImage(buffer).WatermarkImage(watermark)
        if err != nil {
                fmt.Fprintln(os.Stderr, err)
        }

        bimg.Write("new.jpg", newImage)
}
ajdevries commented 7 years ago

Can you be more specific, and maybe supply the images your testing with?

wangjinbei commented 7 years ago

Original image is like this : image

After watermarking : image

wangjinbei commented 7 years ago

And Images info are different as follows: image

wangjinbei commented 7 years ago

Test file: image

ajdevries commented 7 years ago

Only see PNG images, not the original JPG?

wangjinbei commented 7 years ago

Can you access this image link?

wangjinbei commented 7 years ago

Or you can use this github link

ajdevries commented 7 years ago

Yeah, got it... Even not doing watermarking is changing the colour profile, not sure what is happening. Will look into it.

wangjinbei commented 7 years ago

👌 But,If do not add watermark, the color will not change .

The problem is a little bit of color change,will solve this problem?

ajdevries commented 7 years ago

Could be the watermarking. Will look into it. Thanks for all the info!

ajdevries commented 7 years ago

Okay, I wrote the following code:

package main

import (
    "fmt"
    "os"

    "github.com/waldophotos/bimg"
)

func main() {
    buffer, err := bimg.Read("1.jpeg")
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    }
    bufferWater, err := bimg.Read("1.png")
    watermark := bimg.WatermarkImage{
        Left:    0,
        Top:     0,
        Buf:     bufferWater,
        Opacity: 1,
    }
    original := bimg.NewImage(buffer)
    s, err := bimg.Size(buffer)
    height := s.Height
    withWatermark, err := original.WatermarkImage(watermark)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    }
    bimg.Write("new.jpg", withWatermark)
    size := 400
    originalCropSouth, _ := original.Extract(height-size, 0, size, size)
    options := bimg.Options{
        Top:        -1,
        Left:       0,
        AreaWidth:  size,
        AreaHeight: size,
        Type:       bimg.JPEG,
    }
    originalCropNorth, _ := bimg.NewImage(bufferWater).Process(options)
    withWatermarkCropSouth, _ := bimg.NewImage(withWatermark).Extract(height-size, 0, size, size)
    withWatermarkCropNorth, _ := bimg.NewImage(withWatermark).Process(options)
    bimg.Write("new.jpg", withWatermark)
    bimg.Write("crop_watermark.jpg", originalCropNorth)
    bimg.Write("crop_with_watermark.jpg", withWatermarkCropNorth)
    bimg.Write("crop_photo.jpg", originalCropSouth)
    bimg.Write("crop_photo_with_watermark.jpg", withWatermarkCropSouth)
}

This piece, adds a watermark image to the original image and when done, it extracts a portion of the original image and of the watermarked image. Because the watermark image is PNG, I also convert the PNG to JPG when extract a piece of it, to get a similar result.

When doing this, I don't see any color change. I added the resulting images.

Photo

crop_photo

Photo with watermark

crop_photo_with_watermark

Watermark

crop_watermark

Photo with watermark

crop_with_watermark

ajdevries commented 7 years ago

I'm not seeing any color changes. Env:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/ajdevries/gocode"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8.1/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/84/hpd_s5nj4l9394fnzr4z2ksh0000gn/T/go-build706600612=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

vips-8.4.5-Fri Mar 31 09:06:59 CEST 2017
wangjinbei commented 7 years ago

Can you extract a portion of the original image and of the watermarked image here? image

wangjinbei commented 7 years ago

The difference is as follows Original image:

image

After watermarking: image

ajdevries commented 7 years ago

I see the difference and will do some more testing. A guess, could it be related to the image format? When creating an watermarker images with a PNG and a JPG file -where the PNG is the watermark - results in a JPG file. Which uses a different compressing (lossy) instead of PNG (lossless compression).

A test could be watermarking a PNG image instead of a JPG.

wangjinbei commented 7 years ago

Hi @ajdevries, sorry for the late reply. Unfortunately,i can only use this JPG image to reproduce the problem(I can notice the problem clearly). If watermarking images with a PNG and a JPG file, the color will change slightly, is this a problem?

ajdevries commented 7 years ago

So converting an image from PNG to JPG can cause quality loss and maybe color changes. I don't think that this is related to watermarking.

You can save the JPG image as a PNG image (in a Photo Editor) and try the watermark functionality with two PNG files.