Golang在图像上传时,一些图像正在旋转。

huangapple go评论92阅读模式
英文:

Golang some images are rotating on image upload

问题

我面临一个相当奇怪的问题,我正在使用Golang作为后端的RESTful API,并且我使用Go上传图像并调整其大小。我有一个用于iPhone的应用程序,我正在进行测试,如果我使用真实设备上传图像,图像会以横向保存在我的S3帐户中。由于某种原因,调整图像大小会导致旋转,但是如果我从Xcode IDE上传图像,则图像会正确保存而不旋转。我认为可能是某些内容被剥离了,但是我不知道可能是什么,我的代码如下:

func UploadStreamImage(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    var buff bytes.Buffer
    var result string

    wg := sync.WaitGroup{}
    print("Exoler-Streams")
    wg.Add(1)
    go func() {
        defer wg.Done()

        sess, _ := 's3 credentials'

        svc := s3.New(sess)

        file, handler, err := r.FormFile("file")
        if err != nil {
            fmt.Println("Error Uploading Image")
            return
        }
        defer file.Close()
        // resize image
        img,err := imaging.Decode(file)
        if err != nil {
            print("Imaging Open error")
        }
        new_image := imaging.Resize(img, 300, 300, imaging.Lanczos)
        var buf bytes.Buffer


        err = imaging.Encode(&buf,new_image, imaging.JPEG)
        if err != nil {
            log.Println(err)
            return
        }

        // end resize

        r := bytes.NewReader(buf.Bytes())

        read_file,err := ioutil.ReadAll(r)
        if err != nil {
            fmt.Println("Error Reading file")
        }
        // s3 specific code
    }
}

我使用的库是https://github.com/disintegration/imaging,我只是在思考从真实设备上传图像时是否有内容被剥离,从而导致旋转出错。前端的代码对于我的真实设备和Xcode来说都是相同的。

英文:

I am facing a rather weird problem, I am using Golang as a backend restful API and I upload images and resize them with Go . I have an app for I-phone that I am testing and if I upload an image using my real device the image gets saved in my s3 account sideways. For some reason resizing my image is rotating it, however if I upload the image from my Xcode IDE then the image gets saved correctly without rotation . I am thinking that maybe something is getting stripped however I have no idea what that could, my code is this

func UploadStreamImage(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	var buff bytes.Buffer
	var result string

	wg := sync.WaitGroup{}
       print("Exoler-Streams")
	wg.Add(1)
	go func() {
		defer wg.Done()


		sess, _ := 's3 credentials'

		svc := s3.New(sess)

		file, handler, err := r.FormFile("file")
		if err != nil {
			fmt.Println("Error Uploading Image")
			return
		}
		defer file.Close()
		// resize image
		img,err := imaging.Decode(file)
		if err != nil {
			print("Imaging Open error")
		}
		new_image := imaging.Resize(img, 300, 300, imaging.Lanczos)
		var buf bytes.Buffer


		err = imaging.Encode(&buf,new_image, imaging.JPEG)
		if err != nil {
			log.Println(err)
			return
		}

		// end resize

		r := bytes.NewReader(buf.Bytes())





		read_file,err := ioutil.ReadAll(r)
		if err != nil {
			fmt.Println("Error Reading file")
               }
    // s3 specific code
		}

The library I am using is this https://github.com/disintegration/imaging and I am just thinking that something is being stripped when uploading the image from my real device thus it is messing up the rotation . The code on the front-end is all the same for my real device and Xcode .

答案1

得分: 4

在处理过程中,图像没有被旋转,原始图像在某些图像查看软件中以旋转的方式显示,这取决于文件的Exif部分中的图像方向标签(值x00112)。当你剥离Exif部分时,就像图像包所做的那样,你会丢失那些信息,图像会以标准相机方向格式(横向)显示。

英文:

The image is not being rotated in the process, the original image was shown in some image view software in rotated mode depending on the image orientation tag (val x00112) in the Exif part of the file. When you strip the Exif part, as image package does, you lose that information and the image is shown in standard camera orientation format (landscape).

答案2

得分: 3

当从手机上传图像时,图像会保持拍摄时的方向。然后,在图像下添加元数据,键为“Orientation”,以告诉您图像的拍摄方向。手机库可以使用这些信息以正确的方式显示图像,您也可以这样做。方向可以是0到8之间的数字。您只需要关注3(旋转180度)、6(旋转270度)和8(旋转90度)这几个方向数字。

要获取图像的元数据信息,您可以使用类似于github.com/rwcarlsen/goexif/exif的工具。以下是如何从中获取旋转角度的代码片段:

x, err := exif.Decode(openedFileExif)
var rotation float64 = 0

if err == nil {
    orientationRaw, err := x.Get("Orientation")

    if err == nil {
        orientation := orientationRaw.String()

        if orientation == "3" {
            rotation = 180
        } else if orientation == "6" {
            rotation = 270
        } else if orientation == "8" {
            rotation = 90
        }
    }
}

现在,您已经获得所需的旋转角度,只需将图像旋转相应的角度,就可以得到正常方向的图片了。

您可以使用github.com/disintegration/imaging这样的工具来实现。uploadedImage 需要是 Golang 的 image.Image 类型。

if rotation != 0 {
    uploadedImage = imaging.Rotate(uploadedImage, rotation, color.Gray{})
}
英文:

Old question but i thought i would add some extra information here to help anyone who is confused about what Exif is.

When an image is uploaded from a phone it stays in the orientation that it was taken in. However meta data is then added to that image under the key Orientation to tell you which way the image was taken. Phone libaries can then use this to show you the image the correct way around and so can you. It can be a number between 0 - 8. The only orientation numbers you will need to worry about is 3 (rotate 180), 6 (rotate 270) and 8 (rotate 90).

To get a hold of the meta information on an image you can use something like github.com/rwcarlsen/goexif/exif. Here is a snippet on how you can then get the rotation from that.

x, err := exif.Decode(openedFileExif)
var rotation float64 = 0

	if err == nil  {
		orientationRaw, err := x.Get("Orientation")

		if err == nil {
			orientation := orientationRaw.String()

			if orientation == "3" {
				rotation = 180
			} else if orientation == "6" {
				rotation = 270
			} else if orientation == "8" {
				rotation = 90
			}
		}

	}

Now that you have the rotation required you just need to rotate the image by that amount and you will have your normally oriented picture.

You can use something like github.com/disintegration/imaging for that. uploadedImage needs to be golang image.Image

if rotation != 0 {
			uploadedImage = imaging.Rotate(uploadedImage, rotation, color.Gray{})
		}

huangapple
  • 本文由 发表于 2017年1月21日 04:00:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/41771442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定