douyong1908 2016-02-03 13:51
浏览 228
已采纳

Golang将Image magick对象转换为[] byte

I am using the following code which fetch the object from Amazon s3 and after performing resizing and cropping. I want to store it on s3. But the problem is i am not able convert the mw (Image maigck object) to byte array. Which will be used for storing it on s3. Moreover in current method it uses jpeg.Encode. What if the image in .png or .gif format. How will we convert it to []byte?

Could you please also tell me how to evenly crop an image just passing the aspect ratio not cropping coordinates. imgStream.Crop((int)originalWidth, ((int)(originalWidth / masterAspectRatio)), Gravity.Center) like we do it in .net. Reason i am asking is there is no method in library which provides this flexibility.

s3Client := s3.New(session.New(), &aws.Config{Region: aws.String(region)})
            params := &s3.GetObjectInput{
            Bucket: aws.String(bucketName),
            Key: aws.String(keyName),
            }

        out, err := s3Client.GetObject(params)

        if err != nil {
             log.Fatal(err)
        }

        img, err := ioutil.ReadAll(out.Body)
        if err != nil {
            log.Fatal(err)
        }      

        mw := imagick.NewMagickWand()   

        err = mw.ReadImageBlob(img)
        if err != nil {
            log.Fatal(err)
        }

        //Perform resizing and cropping on mw object

        buf := new(bytes.Buffer)
        err = jpeg.Encode(buf, mw, nil)
        sendmw_s3 := buf.Bytes()

         paramsPut := &s3.PutObjectInput{
                        Bucket:         aws.String(masterBucketName),
                        Key:            aws.String(keyName),
                        Body:         bytes.NewReader(sendmw_s3),
                }

        resp, err := s3Client.PutObject(paramsPut)
        if err != nil {
            log.Fatal(err)
        }

Error :

 cannot use mw (type *imagick.MagickWand) as type image.Image in argument to jpeg.Encode:
    *imagick.MagickWand does not implement image.Image (missing At method)
  • 写回答

2条回答 默认 最新

  • donglv7097 2016-02-03 20:09
    关注

    This question is actually two questions, and @SirDarius answered one of them, by suggesting the use of GetImageBlob(). You can also use SetImageFormat() to change the image format before generating the blob.

    For the part about the crop, I am sure there are a bunch of ways to do this with ImageMagick. The way I have done it, to achieve a center crop is to first transform the image so that the smaller dimension fits into my desired target resolution. And then to crop away the parts that overflow.

    // Create a new image where smallest dimension is fit
    // and the rest overflows the dimensions
    size := fmt.Sprintf("%dx%d^+0+0", w, h)
    tx := wand.TransformImage("", size)
    
    // Center Crop away the extra parts of the image, to perform
    tx.SetImageGravity(imagick.GRAVITY_CENTER)
    offsetX := -(int(w) - int(tx.GetImageWidth())) / 2
    offsetY := -(int(h) - int(tx.GetImageHeight())) / 2
    err := tx.ExtentImage(w, h, offsetX, offsetY)
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?