I want to insert text into some template images using golang. One specific requirement is that the unit of the text coordination is by px
instead of point
.
I've done some searches before I asked this question. This link gave some suggestions, whose core code is:
func addLabel(img *image.RGBA, x, y int, label string) {
c.SetDst(img)
size := 12.0 // font size in pixels
pt := freetype.Pt(x, y+int(c.PointToFixed(size)>>6))
if _, err := c.DrawString(label, pt); err != nil {
// handle error
}
}
As we can see, the only unit option is Point
and its odd bit operation (>>6).
However, unit of template data generated by PhotoShop coordinating system is px
.
This bothered me a lot that I can not find a perfect way to convert the unit because there is always tiny offset compared with the raw pixel location and size of the offset is determined by the text size. The larger text generates larger offset.
What can I do to insert the text into exactly right pixel location?