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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!