pil扩展执行打开的jpeg图像的自动旋转
imdirect的Python项目详细描述
pil扩展执行打开的jpeg图像的自动旋转。
说明
拍摄对象或场景相对于 数码相机在所得图像的EXIF ^ {A3}数据中编码。 (假设它保存为JPEG格式)。当使用这种数码设备时 相机图像,此方向可能导致处理 形象,往往是很想被抵消的。
这个模块是Pillow的一个小扩展 monkey patchesPIL.Image.open方法 自动旋转图像[2](通过无损方法)并更新 相应地,如果图像是JPEG的,则将其标记为。
包还包含一个包含ExIF数据的保存方法 默认情况下保存JPEG时。
安装
pip install imdirect
用法
演示猴子修补程序及其工作原理:
>>>fromPILimportImage>>>importimdirect>>>img=Image.open('image.jpg')>>>print("{0}, Orientation: {1}".format(img,img._getexif().get(274)))<PIL.JpegImagePlugin.JpegImageFileimagemode=RGBsize=4032x3024at0x7F44B5E4FF10>,Orientation:6>>>imdirect.monkey_patch()>>>img_autorotated=Image.open('image.jpg')>>>print("{0}, Orientation: {1}".format(img_autorotated,img_autorotated._getexif().get(274)))<PIL.JpegImagePlugin.JpegImageFileimagemode=RGBsize=3024x4032at0x7F44B5DF5150>,Orientation:1
通过应用 imdirect.imdirect_open直接方法:
>>>fromimdirectimportimdirect_open>>>img=imdirect_open('image.jpg')
或者在PIL.Image.Image对象上使用imdirect.autorotate:
>>>fromPILimportImage>>>importimdirect>>>img=Image.open('image.jpg')>>>img_rotated=imdirect.autorotate(img)
最后一个方法不返回PIL.JpegImagePlugin.JpegImageFile,但仍可以使用 如果原始图像的EXIF信息是不需要的。
测试
测试可以使用pytest:
Testing started at 13:28 ... =============================test session starts============================== platform linux2 -- Python 2.7.12, pytest-3.0.1, py-1.4.31, pluggy-0.3.1 rootdir: /home/hbldh/Repos/imdirect, inifile: collected 4 items test_autorotate.py ... test_monkey_patching.py . ===========================4 passed in 0.08 seconds===========================
参考文献
[1] | Exif on Wikipedia (https://en.wikipedia.org/wiki/Exif) |
[2] | Exif orientation (http://sylvana.net/jpegcrop/exif_orientation.html) |