dongmi8980 2015-06-21 22:08
浏览 34
已采纳

Go标准库中是否有一个函数可以将缓冲区编码为go中的任何图像格式

I'm writing a micro-service with some simple image manipulation in go.

When I come to sending the manipulated image back to the user I have to encode it correctly before writing it via a buffer.

I can detect the format easily enough, so it's not precisely a problem. Currently I use my format string and do it like this:

buffer := new(bytes.Buffer)

switch format { //format is just a string as returned by image.Decode()
case "jpeg":
    err := jpeg.Encode(buffer, img, nil) //img is just an image.Image 
    if err != nil {
        //Do some error handling
    }
case "png":
    err := png.Encode(buffer, img)
    if err != nil {
        //Do some error handling
    }
//and so on...

Now this works just fine, I was about to split it out into my own function when I started to think I must have missed something here. It seems too obvious to not have a function like image.Encode(buffer, image, format).

It's not exactly hassle to write it, but my code will soon become unnecessarily unwieldy if I start re-implementing core language functionality.

If it's just not there because go is a nice tidy language with a small footprint, I'm cool with that.

  • 写回答

1条回答 默认 最新

  • douhuxi4145 2015-06-21 22:43
    关注

    The standard library does not contain a function for encoding an image to an arbitrary format.

    One reason that the library does not have such a function is that there are different encoding options for each image format. For example the JPEG options and PNG options are different.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序