Skip to content

Commit

Permalink
Improve pixel density logic in OcrdExif, OCR-D#256, OCR-D#37, OCR-D/o…
Browse files Browse the repository at this point in the history
  • Loading branch information
bertsky committed Jul 11, 2019
1 parent 6f925e3 commit ef37dc4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Versioned according to [Semantic Versioning](http://semver.org/).

## Unreleased

Changed:

* Improve pixel density logic in OcrdExif, #256, #37, OCR-D/ocrd_tesserocr#54

## [1.0.0b10] - 2019-06-25

Fixed:
Expand Down
20 changes: 17 additions & 3 deletions ocrd_models/ocrd_models/ocrd_exif.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@
Technical image metadata
"""

from math import sqrt

class OcrdExif():
"""
Represents technical image metadata
"""Represents technical image metadata.
Members:
- `width` / `height`: pixel dimensions
- `photometricInterpretation`: pixel type/depth, e.g.
'1' for b/w,
'L' for 8-bit grayscale,
'RGB' for 24-bit truecolor,
'I' for 32-bit signed integer grayscale,
'F' for floating-point grayscale
(see PIL concept `mode`)
- `resolution` / `xResolution` / `yResolution`: pixel density
- `resolutionUnits`: unit of measurement (either `inches` or `cm`)
"""

def __init__(self, img):
Expand All @@ -27,7 +40,7 @@ def __init__(self, img):
elif img.format == 'JPEG':
self.xResolution = img.info['jfif_density'][0]
self.yResolution = img.info['jfif_density'][1]
self.resolutionUnit = img.info['jfif_unit']
self.resolutionUnit = 'cm' if img.info['jfif_unit'] == 2 else 'inches'
elif img.format == 'PNG' and 'aspect' in img.info:
self.xResolution = img.info['aspect'][0]
self.yResolution = img.info['aspect'][1]
Expand All @@ -38,6 +51,7 @@ def __init__(self, img):
self.xResolution = 1
self.yResolution = 1
self.resolutionUnit = 'inches'
self.resolution = round(sqrt(self.xResolution * self.yResolution))

def to_xml(self):
"""
Expand Down
6 changes: 6 additions & 0 deletions tests/model/test_exif.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_tiff(self):
self.assertEqual(exif.height, 3749)
self.assertEqual(exif.xResolution, 300)
self.assertEqual(exif.yResolution, 300)
self.assertEqual(exif.resolution, 300)
self.assertEqual(exif.compression, 'jpeg')
self.assertEqual(exif.photometricInterpretation, 'RGB')

Expand All @@ -31,6 +32,7 @@ def test_png1(self):
self.assertEqual(exif.height, 2084)
self.assertEqual(exif.xResolution, 300)
self.assertEqual(exif.yResolution, 300)
self.assertEqual(exif.resolution, 300)
self.assertEqual(exif.compression, None)
self.assertEqual(exif.photometricInterpretation, '1')

Expand All @@ -40,6 +42,7 @@ def test_png2(self):
self.assertEqual(exif.height, 3062)
self.assertEqual(exif.xResolution, 1)
self.assertEqual(exif.yResolution, 1)
self.assertEqual(exif.resolution, 1)
self.assertEqual(exif.photometricInterpretation, '1')

def test_jpg(self):
Expand All @@ -48,6 +51,8 @@ def test_jpg(self):
self.assertEqual(exif.height, 1472)
self.assertEqual(exif.xResolution, 1)
self.assertEqual(exif.yResolution, 1)
self.assertEqual(exif.resolution, 1)
self.assertEqual(exif.resolutionUnit, 'inches')
self.assertEqual(exif.photometricInterpretation, 'RGB')

def test_jp2(self):
Expand All @@ -56,6 +61,7 @@ def test_jp2(self):
self.assertEqual(exif.height, 2084)
self.assertEqual(exif.xResolution, 1)
self.assertEqual(exif.yResolution, 1)
self.assertEqual(exif.resolution, 1)
self.assertEqual(exif.photometricInterpretation, 'RGB')

if __name__ == '__main__':
Expand Down

0 comments on commit ef37dc4

Please sign in to comment.