0

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:

<LatLonBox>
  <north>30.46370000000000</north>
  <south>25.15370000000000</south>
  <east>89.06550000000000</east>
  <west>83.06550000000000</west>
</LatLonBox>

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
2

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>
<north>30.46370000000000</north>
<south>25.15370000000000</south>
<east>89.06550000000000</east>
<west>83.06550000000000</west>
</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

print(x_pixel_size)
print(y_rotation)
print(x_rotation)
print(y_pixel_size)
print(minx)
print(miny)
  • 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 '15 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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