ParthDesai / freetype-go

Automatically exported from code.google.com/p/freetype-go
Other
0 stars 0 forks source link

Image coming out blurry when drawing text over it #18

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have created a composite image using a background single-color png image and 
a text overlay using freetype-go. However, as you can see from the attached 
image file, some of the color below the image is coming out blurred. Can anyone 
tell me what I am doing wrong? Below is the code I am using to create the image.

func getImage(c *context, imageText string) (*image.RGBA, error) {
    colors := []string{"blue", "green", "orange", "red"}

    flag.Parse()
    //read the image file
    reader, err := os.Open("poll_image_" + colors[rand.Intn(len(colors))] + ".png")
    if err != nil {
        return nil, err
    }
    src, _, err := image.Decode(reader)
    if err != nil {
        return nil, err
    }
    b := src.Bounds()
    rgba := image.NewRGBA(image.Rect(0, 0, *imagewidth, *imageheight))
    draw.Draw(rgba, rgba.Bounds(), src, b.Min, draw.Src)
    // Read the font data.
    fontBytes, err := ioutil.ReadFile(*fontfile)
    if err != nil {
        return nil, err
    }
    font, err := freetype.ParseFont(fontBytes)
    if err != nil {
        return nil, err
    }
    // Initialize the context.
    fg := image.White
    ftc := freetype.NewContext()
    ftc.SetHinting(freetype.NoHinting)
    ftc.SetDPI(*dpi)
    ftc.SetFont(font)
    ftc.SetClip(rgba.Bounds())
    ftc.SetDst(rgba)
    ftc.SetSrc(fg)

    // Draw the text.
    sz := *fontsize
    pt := freetype.Pt(*imageleftmargin, *imagetopmargin+int(ftc.PointToFix32(sz)>>8))
    ftc.SetFontSize(sz)
    lines := getImageTextLines(imageText)
    for _, line := range lines {
        _, err = ftc.DrawString(line, pt)
        if err != nil {
            return nil, err
        }
        pt.Y += ftc.PointToFix32(sz * *linespacing)
    }
    return rgba, nil
}

Original issue reported on code.google.com by mryh...@gmail.com on 10 May 2015 at 5:34

Attachments:

GoogleCodeExporter commented 9 years ago
It looks to me like JPEG encoding artifacts. Does the rest of your program use 
image/jpeg anywhere? What does the image look like if you encode the rgba 
variable to PNG at the end of the getImage function? Are you able to share your 
full source code?

Your attached freetype-example.png file also looks like the text was rendered 
at a smaller size and then scaled up. What's the value of your *imagewidth and 
*imageheight flags?

Original comment by nigel...@golang.org on 10 May 2015 at 11:18

GoogleCodeExporter commented 9 years ago
That was it exactly! I was using jpeg encoding for a PNG image. After I changed 
all references to the png package it now works perfectly. Thank you so much for 
your quick and helpful reply!

Original comment by mryh...@gmail.com on 11 May 2015 at 12:10

GoogleCodeExporter commented 9 years ago

Original comment by nigel...@golang.org on 11 May 2015 at 12:34