Skip to content

Instantly share code, notes, and snippets.

@zimeon
Created July 25, 2014 16:25
Show Gist options
  • Save zimeon/8baf705e983e84f5ab3e to your computer and use it in GitHub Desktop.
Save zimeon/8baf705e983e84f5ab3e to your computer and use it in GitHub Desktop.
IIIF w, size calculation
simeon@RottenApple image_api_size_calc>more 2014-07-25_image_width.py
#!/usr/bin/env python
# In OSD Jon uses:
# width_to_request = int(float(desired_height) / image_height * image_width)
# see https://github.com/IIIF/iiif.io/issues/301
matches = 0
for iw in (1,80,200,1234,5678,10567,678991):
for ih in (1,80,200,1234,5678,10567,678991):
for dh in (1,2,3,4,80,99,200,217,1234,2679,5678):
wrf = int(float(dh)/float(ih) * float(iw))
wri = (dh*iw) / ih
if (wrf!=wri):
print "(%d,%d) -> %d height has %d (by float) != %d (by int) width" %\
(iw,ih,dh,wrf,wri)
else:
matches+=1
print "%d matching tests not shown" % (matches)
simeon@RottenApple image_api_size_calc>python 2014-07-25_image_width.py
(200,80) -> 5678 height has 14194 (by float) != 14195 (by int) width
(5678,5678) -> 3 height has 2 (by float) != 3 (by int) width
(678991,678991) -> 99 height has 98 (by float) != 99 (by int) width
536 matching tests not shown
If we consider these three non-matching cases:
#1 - python rounding error:
simeon@RottenApple image_api_size_calc>python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 5678.0 / 80.0
70.975
>>> 70.975 * 200
14194.999999999998
>>> int(14194.999999999998)
14194
simeon@RottenApple image_api_size_calc>dc
40k
5678 80 / 200 * p
14195.0000000000000000000000000000000000000000
#2 & #3 - obviously float is at fault because the square start
image should give a square result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment