duansao20000508 2017-03-21 17:53
浏览 66
已采纳

Golang如何在不旋转的情况下上传外部图像

I have been working on an image upload functionality for a few weeks and I just about have it done. I am using Golang as a backend language and it's purpose is to upload images sent from IOS devices to amazon s3 . In the process of uploading the image I also resize them and this has caused problems primarily the decode method sometimes rotates images which I do not want

file, handler, err := r.FormFile("file")
        if err != nil {
            fmt.Println("Error Uploading Image")
            return
        }
        defer file.Close()
         // the code below sometimes rotates an image
        img,err := imaging.Decode(file)
        if err != nil {
            print("Imaging Open error")
        }
      new_image := imaging.Resize(img,400,400, imaging.Lanczos)

The library I am using is this one https://github.com/disintegration/imaging which is fantastic and the example they showed was this

src, err := imaging.Open("testdata/lena_512.png")
    if err != nil {
        log.Fatalf("Open failed: %v", err)
    }

    src = imaging.Resize(src, 256, 0, imaging.Lanczos)

That example is fine however my images are not stored locally they are coming from IOS devices is there something that I can do to fix this problem ? Some of my images are being saved like this

Some images are rotated like this and it is the Decode method doing it Some images are rotated and saved like this

I can correct it by rotating the image however some other images that do not get rotated by decode end up being rotated by the Rotate270 method .

img,err := imaging.Decode(file)
        if err != nil {
            print("Imaging Open error")
        }
         // rotate the image
        img = imaging.Rotate270(img)

        new_image := imaging.Resize(img,400,400, imaging.Lanczos)

This is how the image is saved and looks after I rotate it . Is there someway I can upload external images without having to use decode or just fixing the decode issue ? Imaging.Resize first parameter takes in type image.Image and here is my full code

func myImages(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    var buff bytes.Buffer
file, handler, err := r.FormFile("file")
        if err != nil {
            fmt.Println("Error Uploading Image")
            return
        }
        defer file.Close()

        img,err := imaging.Decode(file)
        if err != nil {
            print("Imaging Open error")
        }


        new_image := imaging.Resize(img,400,400, imaging.Lanczos)
        var buf bytes.Buffer




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

enter image description here

  • 写回答

2条回答 默认 最新

  • doushanlv5184 2017-03-21 18:42
    关注

    Unfortunately the go stdlib image package does not handle images which are marked as rotated with exif tags (like those here taken on a device upside down). The bug about this is here:

    https://github.com/golang/go/issues/4341

    You can see an example of handling this in camlistore, but it is quite involved:

    https://camlistore.googlesource.com/camlistore/+/master/pkg/images/images.go

    First you have to decode exif options (DecodeOpts), then you have to check which orientation it has (out of 8 possible), then rotate as necessary. It's painful and there is no easy solution as yet. You can use this package to read exif data though:

    https://github.com/rwcarlsen/goexif

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加