{"id":50491268,"url":"https://github.com/petercorke/photo-orientation","last_synced_at":"2026-06-02T03:03:40.210Z","repository":{"id":353870080,"uuid":"1221238412","full_name":"petercorke/photo-orientation","owner":"petercorke","description":"Tool to get and set the orientation metadata in image files.  Uses an ML model to automatically determine orientation. Specifically designed for processing large datasets.","archived":false,"fork":false,"pushed_at":"2026-04-26T00:44:14.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-26T01:22:58.988Z","etag":null,"topics":["image-metadata","image-metadata-analysis-tool","image-orientation-detection"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/petercorke.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-04-25T23:49:04.000Z","updated_at":"2026-04-26T00:44:18.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/petercorke/photo-orientation","commit_stats":null,"previous_names":["petercorke/photo-orientation"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/petercorke/photo-orientation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petercorke%2Fphoto-orientation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petercorke%2Fphoto-orientation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petercorke%2Fphoto-orientation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petercorke%2Fphoto-orientation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petercorke","download_url":"https://codeload.github.com/petercorke/photo-orientation/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petercorke%2Fphoto-orientation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33803734,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-02T02:00:07.132Z","response_time":109,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["image-metadata","image-metadata-analysis-tool","image-orientation-detection"],"created_at":"2026-06-02T03:03:39.235Z","updated_at":"2026-06-02T03:03:40.203Z","avatar_url":"https://github.com/petercorke.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# photo-orientation\n\nTools to inspect and update image orientation metadata (EXIF and XMP).\n\n## Features\n\n- Read EXIF and XMP orientation values.\n- Check for EXIF/XMP mismatches across files or nested folders.\n- Set a fixed orientation value directly.\n- Optional [ML-based auto-orientation mode](https://huggingface.co/DuarteBarbosa/deep-image-orientation-detection) with lazy imports.\n\n## Project Layout\n\n```text\nphoto-orientation/\n  src/photo_orientation/\n    __init__.py\n    getset.py\n    getorientation.py\n    setorientation.py\n  pyproject.toml\n  Makefile\n  README.md\n```\n\n## Installation\n\nBase install (metadata tools):\n\n```bash\npip install -e .\n```\n\nInstall with optional auto-orientation dependencies:\n\n```bash\npip install -e .[auto]\n```\n\nThe `auto` extra includes: `Pillow`, `torch`, `torchvision`, `huggingface_hub`.\n\n## CLI Commands\n\nAfter install, these console scripts are available.\n\n### Check Orientation\n\n```bash\ngetorientation --check path/to/file.jpg path/to/folder\n```\n\n- Accepts files and folders.\n- Recurses folders with `rglob`.\n- In `--check` mode, prints an EXIF/XMP mismatch table.\n\n### Set Orientation\n\n```bash\nsetorientation --set 1 path/to/file.jpg path/to/other.jpg\n```\n\n- `--set` mode does not require the optional `auto` dependencies.\n- Without `--set`, the tool uses the [ML model](https://huggingface.co/DuarteBarbosa/deep-image-orientation-detection) to predict and set orientation.\n\n## Development\n\nThe `Makefile` includes common tasks:\n\n```bash\nmake help\nmake install\nmake install-auto\nmake run-check ARGS=\"--check photos/\"\nmake run-set ARGS=\"--set 1 a.jpg b.jpg\"\nmake dist\nmake upload\n```\n\n# API\n\nTo access the API first import the package\n\n```\nimport photo_orientation as po\n```\n\n\n```\ndef get_orientation(filepath: str, map: int = 4096) -\u003e tuple[int | None, int | None]:\n```\nreturns a 2-tuple containing the orientation value from the EXIF and XMP blocks respectively.  If\neither is missing the corresponding value is `None`. If the values are not `None` but different,\nthe metadata is inconsistent and some tools like Apple Photos may deem the image to be corrupt.\n\nThe function uses [`mmap`](9https://docs.python.org/3/library/mmap.html) to process the file, and by default maps only the first 4k which is where metadata blocks *typically* live.  \nIf the metadata is not found within the mapped region of the file the function will return `(None, None)` -- that doesn't\nmean the metadata is not somewhere else in the file!\nSetting `map=0` would map the whole file.  The length needs to be a multiple of the page size.\n\n\n```\ndef set_orientation(filepath: str, new_orientation: int, XMP: bool = True, map: int = 4096) -\u003e bool:\n```\nsets the orientation value in the metadata to `new_orientation`. The value will be set in the EXIF, and\nif `XMP=True` and an XMP block exists, that value will be updated as well. If the metadata are not preexisting,\nthe tool will not create them, for that you\nneed to use a tool like [`exiftool`](https://exiftool.org).\n\n`set_orientation` is precise and surgical, and changes at most 2 bytes in the metadata blocks.\n\n```\nexif_to_degrees: dict[int, int]\n```\nis a dict that maps an orientation value [1..8] into a rotation in degrees.\n\n\n```\ndef rotate_exif(current_exif: int, degrees_cw: int) -\u003e int:\n```\nThe values used to represent orientation are non-sequential, see next section.  This function\nreturns an orientation value for the orientation `current_exif` rotated by\na CW rotation of `degrees_cw` in degrees. For example `rotate_exif(1, 90) -\u003e 6`.\n\n\n\n# Notes\n\n## Image metadata\n\nImage metadata is a complex nightmare, layer upon layer of \"standards\".  Images can have:\n\n- binary coded [EXIF](https://en.wikipedia.org/wiki/Exif) blocks with tagged values (the basis of the [TIFF](https://en.wikipedia.org/wiki/TIFF) file format). Image orientation is tag 0x112.\n- XML encoded metadata following the [XMP](https://en.wikipedia.org/wiki/Extensible_Metadata_Platform) data model, where\norientation can be expressed as either:\n  - `tiff:Orientation=\"6\"`\n  - `\u003ctiff:Orientation\u003e6\u003c/tiff:Orientation\u003e`\n\n### Image Orientation Modes\n\nThis project works with EXIF orientation values. The orientation field in the image metadata \ndescribes a transform *from* the image array stored in the\nfile as rows and columns (it could be landscape or portrait mode) *to* how it is displayed correctly on the screen.\n\nThese are the values most cameras and scanners use for plain rotation:\n\n| EXIF/XMP Value | Meaning | Rotation to display upright |\n|---|---|---|\n| 1 | Normal | 0 degrees |\n| 3 | Rotated 180 | 180 degrees clockwise |\n| 6 | Rotated 90 CW | 90 degrees clockwise |\n| 8 | Rotated 270 CW | 270 degrees clockwise (or 90 CCW) |\n\n\n### Full EXIF orientation table\n\nThe standard supports additional mirrored transforms but these are uncommon. It can be useful to\nconsider how the top-left corner of the image, as stored, is transformed in the displayed image.\n\n| Value | Meaning | Top-left (0,0) maps to |\n| :--- | :--- | :--- |\n| 1 | Horizontal (normal) | Top left |\n| 2 | Mirrored about vertical axis | Top right |\n| 3 | Rotated 180 | Bottom right |\n| 4 | Mirrored about horizontal axis | Bottom left |\n| 5 | Mirrored about vertical axis and rotated 270 CW (transpose) | Top left |\n| 6 | Rotated 90 CW | Top right |\n| 7 | Mirrored about vertical axis and rotated 90 CW | Bottom right |\n| 8 | Rotated 270 CW | Bottom left |\n\n### Where this info lives\n\n- EXIF block:\n  - Usually in JPEG APP1 metadata with the `Exif\\0\\0` header.\n  - The orientation value is tag `0x0112` in the TIFF IFD entries.\n  - This code handles the case of the tag being SHORT (type 3) or LONG (type 4)\n  - In this project, EXIF is read/written from that orientation tag.\n\n- XMP block:\n  - Stored as XML metadata, often also in APP1 (or equivalent container metadata).\n  - Common forms are:\n    - `tiff:Orientation=\"6\"`\n    - `\u003ctiff:Orientation\u003e6\u003c/tiff:Orientation\u003e`\n  - This project reads and can update both forms.\n\n## ML model for automatic orientation estimation\n\nThe tool uses the [deep-image-orientation-detection](https://huggingface.co/DuarteBarbosa/deep-image-orientation-detection)\nmodel from Hugging Face.  The model was trained on a huge dataset of 189,018 unique images curated from a number\nof publicly available datasets. Each image is augmented by being rotated in four ways (0°, 90°, 180°, 270°), creating a total of 756,072 samples. This augmented dataset was then split into 604,857 samples for training and 151,215 samples for validation.\nThe model achieves 98.82% accuracy on the validation set.\n\nFull details can be found on the [GitHub repo](https://github.com/duartebarbosadev/deep-image-orientation-detection).\n\nInference is performed using PyTorch with automatic computational fallbacks: CUDA, MPS (Apple Silicon), CPU.\n\n# Practical guidance\n\n- Use `getorientation --check ...` to detect EXIF/XMP mismatch.\n- Use `setorientation --set \u003cvalue\u003e ...` to apply a fixed orientation value.\n- If EXIF and XMP differ, normalize them to the same value to avoid viewer-specific behavior.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetercorke%2Fphoto-orientation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetercorke%2Fphoto-orientation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetercorke%2Fphoto-orientation/lists"}