问题:如何使用PIL获取图片尺寸?

如何使用PIL或任何其他Python库获取图片边的大小?

How do I get a size of a pictures sides with PIL or any other Python library?


回答 0

from PIL import Image

im = Image.open('whatever.png')
width, height = im.size

根据文档

from PIL import Image

im = Image.open('whatever.png')
width, height = im.size

According to the documentation.


回答 1

您可以使用Pillow(网站文档GitHubPyPI)。Pillow与PIL具有相同的界面,但可与Python 3一起使用。

安装

$ pip install Pillow

如果您没有管理员权限(在Debian上为sudo),则可以使用

$ pip install --user Pillow

有关安装的其他说明在这里

from PIL import Image
with Image.open(filepath) as img:
    width, height = img.size

速度

这需要3.21秒才能获得30336张图像(JPG从31×21到424×428,来自Kaggle 国家数据科学碗的训练数据)

这可能是使用枕头而不是自己写的东西的最重要的原因。而且您应该使用Pillow而不是PIL(python-imaging),因为它可以在Python 3中使用。

备选方案1:Numpy(已弃用)

我坚持scipy.ndimage.imread认为信息仍然存在,但请记住:

不推荐使用imread!在SciPy 1.0.0中不推荐使用imread,而在1.2.0中已删除了[read]。

import scipy.ndimage
height, width, channels = scipy.ndimage.imread(filepath).shape

备选方案2:Pygame

import pygame
img = pygame.image.load(filepath)
width = img.get_width()
height = img.get_height()

You can use Pillow (Website, Documentation, GitHub, PyPI). Pillow has the same interface as PIL, but works with Python 3.

Installation

$ pip install Pillow

If you don’t have administrator rights (sudo on Debian), you can use

$ pip install --user Pillow

Other notes regarding the installation are here.

Code

from PIL import Image
with Image.open(filepath) as img:
    width, height = img.size

Speed

This needed 3.21 seconds for 30336 images (JPGs from 31×21 to 424×428, training data from National Data Science Bowl on Kaggle)

This is probably the most important reason to use Pillow instead of something self-written. And you should use Pillow instead of PIL (python-imaging), because it works with Python 3.

Alternative #1: Numpy (deprecated)

I keep scipy.ndimage.imread as the information is still out there, but keep in mind:

imread is deprecated! imread is deprecated in SciPy 1.0.0, and [was] removed in 1.2.0.

import scipy.ndimage
height, width, channels = scipy.ndimage.imread(filepath).shape

Alternative #2: Pygame

import pygame
img = pygame.image.load(filepath)
width = img.get_width()
height = img.get_height()

回答 2

由于scipyimread已过时,使用imageio.imread

  1. 安装- pip install imageio
  2. height, width, channels = imageio.imread(filepath).shape

Since scipy‘s imread is deprecated, use imageio.imread.

  1. Install – pip install imageio
  2. Use height, width, channels = imageio.imread(filepath).shape

回答 3

这是一个完整的示例,从URL加载图像,使用PIL创建,打印尺寸并调整大小…

import requests
h = { 'User-Agent': 'Neo'}
r = requests.get("https://images.freeimages.com/images/large-previews/85c/football-1442407.jpg", headers=h)

from PIL import Image
from io import BytesIO
# create image from binary content
i = Image.open(BytesIO(r.content))


width, height = i.size
print(width, height)
i = i.resize((100,100))
display(i)

This is a complete example loading image from URL, creating with PIL, printing the size and resizing…

import requests
h = { 'User-Agent': 'Neo'}
r = requests.get("https://images.freeimages.com/images/large-previews/85c/football-1442407.jpg", headers=h)

from PIL import Image
from io import BytesIO
# create image from binary content
i = Image.open(BytesIO(r.content))


width, height = i.size
print(width, height)
i = i.resize((100,100))
display(i)

回答 4

这是从Python 3中的给定URL获取图像大小的方法:

from PIL import Image
import urllib.request
from io import BytesIO

file = BytesIO(urllib.request.urlopen('http://getwallpapers.com/wallpaper/full/b/8/d/32803.jpg').read())
im = Image.open(file)
width, height = im.size

Here’s how you get the image size from the given URL in Python 3:

from PIL import Image
import urllib.request
from io import BytesIO

file = BytesIO(urllib.request.urlopen('http://getwallpapers.com/wallpaper/full/b/8/d/32803.jpg').read())
im = Image.open(file)
width, height = im.size

回答 5

以下给出尺寸和通道:

import numpy as np
from PIL import Image

with Image.open(filepath) as img:
    shape = np.array(img).shape

Followings gives dimensions as well as channels:

import numpy as np
from PIL import Image

with Image.open(filepath) as img:
    shape = np.array(img).shape

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。