【问题标题】:PIL Image.size returns the opposite width/heightPIL Image.size 返回相反的宽度/高度
【发布时间】:2014-10-25 19:35:27
【问题描述】:

使用 PIL 确定图像的宽度和高度

在特定图像上(幸运的是只有这个 - 但它很麻烦)从 image.size 返回的宽度/高度是相反的。 图片:
http://storage.googleapis.com/cookila-533ebf752b9d1f7c1e8b4db3/IMG_0004.JPG

代码:

from PIL import Image
import urllib, cStringIO

file = cStringIO.StringIO(urllib.urlopen('http://storage.googleapis.com/cookila-533ebf752b9d1f7c1e8b4db3/IMG_0004.JPG').read())
im=Image.open(file)
print im.size

结果是 - (2592, 1936)
应该是相反的

【问题讨论】:

  • @mhawke - 在浏览器中打开图片并找出问题...

标签: python python-imaging-library


【解决方案1】:

原因是该图像具有与之关联的Exif Orientation metadata,这将导致尊重该属性的应用程序旋转它:

# identify -verbose IMG_0004.JPG | grep Orientation
  Orientation: RightTop
    exif:Orientation: 6

与常规图像比较:

# identify -verbose iceland_pano.jpg | grep Orientation
  Orientation: TopLeft
    exif:Orientation: 1

所以图像尺寸实际上是横向的(比高更宽),但它会在浏览器、图像查看器等显示时旋转。

【讨论】:

  • 谢谢@Lukas - 那么你有什么建议(假设我想表现得像浏览器/图像查看器等)
  • 嗯,很难说。幸运的是,我从来没有遇到过必须处理这个问题的情况。我可能会检查图像是否具有非常规的方向,如果是,请将其标准化并apply the rotation to the actual image data
【解决方案2】:

答:这是标准状态。

Numpy 数组携带图像(来自 OpenCV2 ),一旦被 data.shape 检查,PIL/Pillow Image.size


可以像 >>> Python Pillow v2.6.0 paletted PNG (256) How to add an Alpha channel?

那样审查和验证

print data.shape 给出(1624, 3856)print im.size 给出(3856, 1624)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2019-07-21
    • 1970-01-01
    相关资源
    最近更新 更多