https://github.com/ageitgey/image_to_numpy
Load an image file into a numpy array with Exif orientation support. Prevents upside-down and sideways images!
https://github.com/ageitgey/image_to_numpy
Last synced: 2 months ago
JSON representation
Load an image file into a numpy array with Exif orientation support. Prevents upside-down and sideways images!
- Host: GitHub
- URL: https://github.com/ageitgey/image_to_numpy
- Owner: ageitgey
- License: mit
- Created: 2019-10-09T14:13:03.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-25T17:25:02.000Z (about 4 years ago)
- Last Synced: 2025-03-24T07:48:35.982Z (3 months ago)
- Language: Python
- Size: 39 MB
- Stars: 180
- Watchers: 9
- Forks: 37
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# image_to_numpy
Load an image file into a numpy array - while automatically rotating the image based on
Exif orientation. Prevents upside-down and sideways images!
```
import image_to_numpyimg = image_to_numpy.load_image_file("my_file.jpg")
```The image is automatically rotated into the correct orientation if the image contains Exif orientation metadata.
Otherwise, it is loaded normally.From there, you can pass the numpy array to any Python library that works with images
in numpy array format, like face_recognition, Keras, etc.## Installation
You can install from [PyPI](https://pypi.org/project/image_to_numpy/):
pip install image_to_numpy
## What is Exif Orientation data?Most images captured by cell phones and consumer cameras aren't stored on disk in the same orientation they appear on screen. Exif Orientation data tells the program which way the image needs to be rotated to display correctly. Not handling Exif Orientation is a common source of bugs in Computer Vision and Machine Learning applications.
[You can learn more about images and Exif Orientation data in my article here](https://medium.com/@ageitgey/the-dumb-reason-your-fancy-computer-vision-app-isnt-working-exif-orientation-73166c7d39da).
## Usage
```python
import image_to_numpyimg = image_to_numpy.load_image_file("my_file.jpg")
```Your image is loaded - with the correct orientation!
By default, the image array is returned as a numpy array
with 3-channels of 8-bit RGB data.You can control the output format by passing in an optional `mode` parameter:
```python
import image_to_numpyimg = image_to_numpy.load_image_file("my_file.jpg", mode="RGB")
# Supported modes:
# 1 (1-bit pixels, black and white, stored with one pixel per byte)
# L (8-bit pixels, black and white)
# RGB (3x8-bit pixels, true color)
# RGBA (4x8-bit pixels, true color with transparency mask)
# CMYK (4x8-bit pixels, color separation)
# YCbCr (3x8-bit pixels, color video format)
# I (32-bit signed integer pixels)
# F (32-bit floating point pixels)
```If you have matplotlib installed, here's a quick way to show your image
on the screen:```python
import matplotlib.pyplot as plt
import image_to_numpyimg = image_to_numpy.load_image_file("my_file.jpg")
plt.imshow(img)
plt.show()
```## Thanks
- EXIF test images used in the unittests created by Dave Perrett / @daveperrett
- https://github.com/recurser/exif-orientation-examples