Ok, thanks omz. Not a huge problem.
This appears to work. I have done some tests. Appears to be 1 or 2 pixelsout. But this could be margins in the label. Anyway, close enough for what I wanted. Maybe there's a lot easier way to do this. It's the best I could come up with my limited experience. I did it as a class so not to have the overhead of setting up the image etc... If this is a stupid idea or there is a far simpler way of doing it , would appreciate the feedback.
import Image, ImageDraw, ImageFont
class MeasureString(object):
def __init__(self, font):
self.image = Image.open("")
self.draw = ImageDraw.Draw(self.image)
self.font = ImageFont.truetype(font[0], font[1])
def set_font(self, font):
#not sure if i have to del the prev font
self.font = ImageFont.truetype(font[0], font[1])
def measure_string(self, s, font = None):
self.set_font(font) if font else False
return self.draw.textsize(s, self.font)
def __del__(self):
# i think this is correct
del self.draw
if __name__ == '__main__':
font = ('Helvetica', 16)
ms = MeasureString(font)
print ms.measure_string('a test string')