What is the most pythonic way to create a Word File for a .png image, having only it's bounding box?

For example, given the following bounding box:


the result should be a text file reporting, in order:

  1. pixel size in the x-direction
  2. rotation about y-axis
  3. rotation about x-axis
  4. pixel size in the y-direction
  5. x-coordinate of the center of the upper left pixel
  6. y-coordinate of the center of the upper left pixel

1 Answer 1


Assuming a zero-based rotation factor for x and y, here you need the png file image dimensions in order to derive x and y pixel size.

Rudimentary example:

import xml.etree.ElementTree as etree

image_width = 500
image_height = 300
x_rotation = 0.0
y_rotation = 0.0

bbox_xml = etree.fromstring('''<LatLonBox>

minx = float(bbox_xml.find('west').text)
miny = float(bbox_xml.find('south').text)
maxx = float(bbox_xml.find('east').text)
maxy = float(bbox_xml.find('north').text)

x_pixel_size = (maxx - minx) / image_width
y_pixel_size = ((maxy - miny) / image_height) * -1

  • 1
    The origin is at top-left, thus minx, maxy. Pixel size in South-North direction is negative, and don't forget the half a pixel shifts for the origin: + 1/2 pixel to the east and - 1/2 pixel to the south.
    – user30184
    Nov 17, 2015 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.