我如何与PIL获得图片尺寸?

时间:2022-11-20 00:21:57

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

如何获得具有PIL或任何其他Python库的图片边的大小?

3 个解决方案

#1


203  

from PIL import Image

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

According to the documentation.

根据文档。

#2


43  

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

你可以使用枕头(网站,文件,GitHub, PyPI)。Pillow与PIL有相同的接口,但适用于Python 3。

Installation

$ pip install Pillow

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

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

$ 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 31x21 to 424x428, training data from National Data Science Bowl on Kaggle)

30336张图片需要3.21秒(jpg从31x21到424x428,来自国家数据科学碗关于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.

这可能是使用枕头而不是自创的最重要的原因。您应该使用Pillow而不是PIL (Python -imaging),因为它适用于Python 3。

Alternative #1: Numpy

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()

#3


2  

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

由于scipy的imread是不赞成的,所以使用imageio.imread。

  1. Install - pip install imageio
  2. 安装- pip安装imageio。
  3. Use height, width, channels = imageio.imread(filepath).shape
  4. 使用高度、宽度、通道= image .imread(filepath).shape

#1


203  

from PIL import Image

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

According to the documentation.

根据文档。

#2


43  

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

你可以使用枕头(网站,文件,GitHub, PyPI)。Pillow与PIL有相同的接口,但适用于Python 3。

Installation

$ pip install Pillow

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

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

$ 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 31x21 to 424x428, training data from National Data Science Bowl on Kaggle)

30336张图片需要3.21秒(jpg从31x21到424x428,来自国家数据科学碗关于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.

这可能是使用枕头而不是自创的最重要的原因。您应该使用Pillow而不是PIL (Python -imaging),因为它适用于Python 3。

Alternative #1: Numpy

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()

#3


2  

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

由于scipy的imread是不赞成的,所以使用imageio.imread。

  1. Install - pip install imageio
  2. 安装- pip安装imageio。
  3. Use height, width, channels = imageio.imread(filepath).shape
  4. 使用高度、宽度、通道= image .imread(filepath).shape