FAQ
Hi

I've got some simple code to write out the same image in 3 different
formats: png, jpeg and gif. The image is correctly written for jpeg and
gif, but I get a zero length file for the png. Any idea why this would
happen, or does it look like a bug?

Here's the code:

package main

import (
     "bufio"
     "fmt"
     "image"
     "image/color"
     "image/draw"
     "image/gif"
     "image/jpeg"
     "image/png"
     "os"
)

func main() {
     img := image.NewRGBA(image.Rect(0, 0, 640, 480))

     c := color.RGBA{255, 0, 0, 255}
     draw.Draw(img, image.Rect(10, 10, 100, 100), &image.Uniform{c},
image.ZP, draw.Src)

     if imgFilePng, err := os.Create("red.png"); err != nil {
         fmt.Println("Png error: ", err)
     } else {
         defer imgFilePng.Close()
         png.Encode(bufio.NewWriter(imgFilePng), img)
     }

     if imgFileGif, err := os.Create("red.gif"); err != nil {
         fmt.Println("Gif error: ", err)
     } else {
         defer imgFileGif.Close()
         gif.Encode(bufio.NewWriter(imgFileGif), img, &gif.Options{})
     }

     if imgFileJpeg, err := os.Create("red.jpeg"); err != nil {
         fmt.Println("Jpeg error: ", err)
     } else {
         defer imgFileJpeg.Close()
         jpeg.Encode(bufio.NewWriter(imgFileJpeg), img,
&jpeg.Options{jpeg.DefaultQuality})
     }
}


Cheers,
Graham

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Jesse McNelis at Dec 5, 2013 at 12:02 am

    On Thu, Dec 5, 2013 at 10:58 AM, Graham MacDonald wrote:

    Hi

    I've got some simple code to write out the same image in 3 different
    formats: png, jpeg and gif. The image is correctly written for jpeg and
    gif, but I get a zero length file for the png. Any idea why this would
    happen, or does it look like a bug?
    The error returned from png.Encode() might point you in the right direction.




    --
    =====================
    http://jessta.id.au

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Graham MacDonald at Dec 5, 2013 at 12:20 am
    Thanks, but I don't get an error from png.Encode. (I should have put the
    check in the given example, though I had it in an earlier version.) Adding
    the check to this example shows the returned value from png.Encode is nil.
    On Thursday, 5 December 2013 00:02:28 UTC, Jesse McNelis wrote:

    On Thu, Dec 5, 2013 at 10:58 AM, Graham MacDonald <grahamam...@gmail.com<javascript:>
    wrote:
    Hi

    I've got some simple code to write out the same image in 3 different
    formats: png, jpeg and gif. The image is correctly written for jpeg and
    gif, but I get a zero length file for the png. Any idea why this would
    happen, or does it look like a bug?
    The error returned from png.Encode() might point you in the right
    direction.




    --
    =====================
    http://jessta.id.au
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jesse McNelis at Dec 5, 2013 at 12:24 am

    On 05/12/2013 11:20 AM, "Graham MacDonald" wrote:
    Thanks, but I don't get an error from png.Encode. (I should have put the
    check in the given example, though I had it in an earlier version.) Adding
    the check to this example shows the returned value from png.Encode is nil.
    >

    Ah, then perhaps the issue is that you're not flushing the bufio.Writer

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Alex Skinner at Dec 5, 2013 at 4:39 am
    This is exactly it. The png is not large enough to trigger the flush
    automatically.

    Alex
    On Wednesday, December 4, 2013 7:24:40 PM UTC-5, Jesse McNelis wrote:

    On 05/12/2013 11:20 AM, "Graham MacDonald" wrote:

    Thanks, but I don't get an error from png.Encode. (I should have put
    the check in the given example, though I had it in an earlier version.)
    Adding the check to this example shows the returned value from png.Encode
    is nil.
    Ah, then perhaps the issue is that you're not flushing the bufio.Writer
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Graham MacDonald at Dec 5, 2013 at 7:20 am
    Yes, that's it. If I flush the writer, the png is written. Curiously
    though, with this example, the png has a white background, while the jpeg
    and gif both have black backgrounds.

    Thanks,
    Graham
    On Wednesday, 4 December 2013 23:58:24 UTC, Graham MacDonald wrote:

    Hi

    I've got some simple code to write out the same image in 3 different
    formats: png, jpeg and gif. The image is correctly written for jpeg and
    gif, but I get a zero length file for the png. Any idea why this would
    happen, or does it look like a bug?

    Here's the code:

    package main

    import (
    "bufio"
    "fmt"
    "image"
    "image/color"
    "image/draw"
    "image/gif"
    "image/jpeg"
    "image/png"
    "os"
    )

    func main() {
    img := image.NewRGBA(image.Rect(0, 0, 640, 480))

    c := color.RGBA{255, 0, 0, 255}
    draw.Draw(img, image.Rect(10, 10, 100, 100), &image.Uniform{c},
    image.ZP, draw.Src)

    if imgFilePng, err := os.Create("red.png"); err != nil {
    fmt.Println("Png error: ", err)
    } else {
    defer imgFilePng.Close()
    png.Encode(bufio.NewWriter(imgFilePng), img)
    }

    if imgFileGif, err := os.Create("red.gif"); err != nil {
    fmt.Println("Gif error: ", err)
    } else {
    defer imgFileGif.Close()
    gif.Encode(bufio.NewWriter(imgFileGif), img, &gif.Options{})
    }

    if imgFileJpeg, err := os.Create("red.jpeg"); err != nil {
    fmt.Println("Jpeg error: ", err)
    } else {
    defer imgFileJpeg.Close()
    jpeg.Encode(bufio.NewWriter(imgFileJpeg), img,
    &jpeg.Options{jpeg.DefaultQuality})
    }
    }


    Cheers,
    Graham
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Carlos Castillo at Dec 5, 2013 at 8:26 am
    For the record, this is a bad way to use bufio.Writer. You should always
    flush the buffer before closing the underlying writer or you will be
    missing data. I suspect that your "working" JPEG and GIF images from your
    original posted code are incomplete files too (just somewhat less
    incomplete than 0-bytes).

    As for the backgrounds, PNG is the only format that supports an alpha
    channel, so the default pixel values of (0,0,0,0) will produce a
    transparent background (0 alpha), while JPEG and GIF images will ignore the
    alpha values and thus use the color of black (0,0,0). The white you see for
    PNG, is either the result of the surface you are drawing it on, or how your
    renderer shows transparent pixels when it doesn't handle an alpha channel.

    If you want a black background, you should fill your image with (0,0,0,255)
    before starting. Also if writing PNG files, if you use image.NRGBA (non
    premultiplied alpha images), then encoding them to png files should be
    faster and lossless as they don't need to be converted first; although for
    non-transparent images the conversion should be lossless as well.
    On Wednesday, December 4, 2013 11:20:55 PM UTC-8, Graham MacDonald wrote:

    Yes, that's it. If I flush the writer, the png is written. Curiously
    though, with this example, the png has a white background, while the jpeg
    and gif both have black backgrounds.

    Thanks,
    Graham
    On Wednesday, 4 December 2013 23:58:24 UTC, Graham MacDonald wrote:

    Hi

    I've got some simple code to write out the same image in 3 different
    formats: png, jpeg and gif. The image is correctly written for jpeg and
    gif, but I get a zero length file for the png. Any idea why this would
    happen, or does it look like a bug?

    Here's the code:

    package main

    import (
    "bufio"
    "fmt"
    "image"
    "image/color"
    "image/draw"
    "image/gif"
    "image/jpeg"
    "image/png"
    "os"
    )

    func main() {
    img := image.NewRGBA(image.Rect(0, 0, 640, 480))

    c := color.RGBA{255, 0, 0, 255}
    draw.Draw(img, image.Rect(10, 10, 100, 100), &image.Uniform{c},
    image.ZP, draw.Src)

    if imgFilePng, err := os.Create("red.png"); err != nil {
    fmt.Println("Png error: ", err)
    } else {
    defer imgFilePng.Close()
    png.Encode(bufio.NewWriter(imgFilePng), img)
    }

    if imgFileGif, err := os.Create("red.gif"); err != nil {
    fmt.Println("Gif error: ", err)
    } else {
    defer imgFileGif.Close()
    gif.Encode(bufio.NewWriter(imgFileGif), img, &gif.Options{})
    }

    if imgFileJpeg, err := os.Create("red.jpeg"); err != nil {
    fmt.Println("Jpeg error: ", err)
    } else {
    defer imgFileJpeg.Close()
    jpeg.Encode(bufio.NewWriter(imgFileJpeg), img,
    &jpeg.Options{jpeg.DefaultQuality})
    }
    }


    Cheers,
    Graham
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Graham MacDonald at Dec 5, 2013 at 2:18 pm
    Yeah, I'd forgotten my flushes. Thanks for the explanation about the
    transparency issue.
    On Thursday, 5 December 2013 08:26:00 UTC, Carlos Castillo wrote:

    For the record, this is a bad way to use bufio.Writer. You should always
    flush the buffer before closing the underlying writer or you will be
    missing data. I suspect that your "working" JPEG and GIF images from your
    original posted code are incomplete files too (just somewhat less
    incomplete than 0-bytes).

    As for the backgrounds, PNG is the only format that supports an alpha
    channel, so the default pixel values of (0,0,0,0) will produce a
    transparent background (0 alpha), while JPEG and GIF images will ignore the
    alpha values and thus use the color of black (0,0,0). The white you see for
    PNG, is either the result of the surface you are drawing it on, or how your
    renderer shows transparent pixels when it doesn't handle an alpha channel.

    If you want a black background, you should fill your image with
    (0,0,0,255) before starting. Also if writing PNG files, if you use
    image.NRGBA (non premultiplied alpha images), then encoding them to png
    files should be faster and lossless as they don't need to be converted
    first; although for non-transparent images the conversion should be
    lossless as well.
    On Wednesday, December 4, 2013 11:20:55 PM UTC-8, Graham MacDonald wrote:

    Yes, that's it. If I flush the writer, the png is written. Curiously
    though, with this example, the png has a white background, while the jpeg
    and gif both have black backgrounds.

    Thanks,
    Graham
    On Wednesday, 4 December 2013 23:58:24 UTC, Graham MacDonald wrote:

    Hi

    I've got some simple code to write out the same image in 3 different
    formats: png, jpeg and gif. The image is correctly written for jpeg and
    gif, but I get a zero length file for the png. Any idea why this would
    happen, or does it look like a bug?

    Here's the code:

    package main

    import (
    "bufio"
    "fmt"
    "image"
    "image/color"
    "image/draw"
    "image/gif"
    "image/jpeg"
    "image/png"
    "os"
    )

    func main() {
    img := image.NewRGBA(image.Rect(0, 0, 640, 480))

    c := color.RGBA{255, 0, 0, 255}
    draw.Draw(img, image.Rect(10, 10, 100, 100), &image.Uniform{c},
    image.ZP, draw.Src)

    if imgFilePng, err := os.Create("red.png"); err != nil {
    fmt.Println("Png error: ", err)
    } else {
    defer imgFilePng.Close()
    png.Encode(bufio.NewWriter(imgFilePng), img)
    }

    if imgFileGif, err := os.Create("red.gif"); err != nil {
    fmt.Println("Gif error: ", err)
    } else {
    defer imgFileGif.Close()
    gif.Encode(bufio.NewWriter(imgFileGif), img, &gif.Options{})
    }

    if imgFileJpeg, err := os.Create("red.jpeg"); err != nil {
    fmt.Println("Jpeg error: ", err)
    } else {
    defer imgFileJpeg.Close()
    jpeg.Encode(bufio.NewWriter(imgFileJpeg), img,
    &jpeg.Options{jpeg.DefaultQuality})
    }
    }


    Cheers,
    Graham
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 4, '13 at 11:58p
activeDec 5, '13 at 2:18p
posts8
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase