[英]Keras Preprocessing Rotates Image While Importing It With load_img()
I just started learning the Keras API and I am experimenting with the MNIST dataset.我刚开始学习 Keras API,正在试验 MNIST 数据集。 I got it working correctly but I have a problem with the function
load_img()
from the keras.preprocessing.image
library, when I try to test a picture that I took.我让它正常工作,但是当我尝试测试我拍摄的照片时
from the keras.preprocessing.image
库中的函数load_img()
出现问题。 It imports a portrait oriented image as a landscape one.它将纵向图像导入为横向图像。 I took the photo with my smartphone in portrait mode and Windows correctly shows width 3024 and height 4032 pixels.
我用智能手机在人像模式下拍照,Windows 正确显示宽度 3024 和高度 4032 像素。
When I load that image and print the width and height it shows 4032x3024.当我加载该图像并打印宽度和高度时,它显示为 4032x3024。 Also when I do
img.show()
, it seems to have been rotated 90 degrees counterclockwise.此外,当我执行
img.show()
,它似乎逆时针旋转了 90 度。 All that is happening right after loading it, without any processing.所有这一切都在加载后立即发生,无需任何处理。 I tried looking into the API for the
load_img()
and couldn't find any arguments that make it rotate while loading.我尝试查看
load_img()
的 API,但找不到任何使其在加载时旋转的参数。
This is a dummy example to show you the problem:这是一个向您展示问题的虚拟示例:
from keras.preprocessing.image import load_img
img = load_img('filepath/test.jpg') # Load portrait mode image Windows says 3024x4032
width, height = img.size
print(width, height) # Prints 4032 3024
img.show() # Shows it rotated by 90 degrees counterclockwise
I want it to be imported in portrait mode.我希望它以纵向模式导入。 Why does it get rotated?
为什么会旋转? The problem is that a picture taken in landscape mode is also imported as 4032 x 3024, so I can't differentiate between the 2 orientations.
问题是在横向模式下拍摄的照片也被导入为 4032 x 3024,所以我无法区分这两个方向。 I want to be able to rotate the image if it's in portrait mode but not rotate it if it's in landscape mode.
如果图像处于纵向模式,我希望能够旋转图像,但如果处于横向模式,则不能旋转它。
EDIT: I just tried to load the image with Pillow and the results are exactly the same编辑:我只是尝试使用 Pillow 加载图像,结果完全相同
Use:用:
jhead -v YourImage.jpg
to check the EXIF parameter called Orientation
- phone cameras set it so that images can be rotated.检查名为
Orientation
的 EXIF 参数 - 手机相机将其设置为可以旋转图像。 Try it for one image that works and another image that is "unhappy" .尝试使用一个有效的图像和另一个“不愉快”的图像。
You can correct it with ImageMagick :您可以使用ImageMagick纠正它:
convert unhappy.jpg -auto-orient happy.jpg
Or maybe more easily with exiftool
.或者使用
exiftool
可能更容易。 Discussion and example here .讨论和例子在这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.