Python image get image size

How to Get Image Size in Python

The “size” attribute of PIL.Image in Pillow (PIL) returns The width and height order.

To get the image size (width, height) with OpenCV, use the “ndarray.shape.”

To get the image size with PIL, you can use the “size” attribute.

For example, it works on the following kind of image.

For color Image

In the case of a color image, it is a 3D ndarray of row (height) x column (width) x color (3). So the shape is a tuple of (row (height), column (width), color (3)).

# Importing cv2 import cv2 # Path img = cv2.imread('data.jpg') # print(img.shape)

You can see that we get the tuple containing height, width, and color. We can assign each value to a variable and unpack the tuple as follows.

# app.py # Importing cv2 import cv2 # Image img = cv2.imread('data.jpg') h, w, c = img.shape print('width: ', w) print('height: ', h) print('channel:', c)
width: 3000 height: 4000 channel: 3

When unpacking the tuple, values not used after that may be assigned to _(underscore) by convention.

# Importing cv2 import cv2 # Image img = cv2.imread('data.jpg') h, w, _ = img.shape print('width: ', w) print('height: ', h)

You can also use an index to access the width and height of the img variable.

# Importing cv2 import cv2 # Image img = cv2.imread('data.jpg') print('width: ', img.shape[0]) print('height: ', img.shape[1])

When resetting an image’s size using the cv2.resize() method, it needs to be (width, height).

For Grayscale Image

When you are working with grayscale (monochrome) images, it is a 2D ndarray of rows (height) x columns (width). So the shape is a tuple of (row (height), column (width)).

# Importing cv2 import cv2 # Image img = cv2.imread('grayimage.jpg', cv2.IMREAD_GRAYSCALE) print(img.shape)

The unpacking tuple is the same as the color image, and also we can access the image width and height using the index.

# Importing cv2 import cv2 # Image img = cv2.imread('grayimage.jpg', cv2.IMREAD_GRAYSCALE) h, w = img.shape print('width: ', w) print('height:', h) # Accessing using index print('Accessing width and height using index') print('width: ', img.shape[1]) print('height:', img.shape[0])
width: 4000 height: 6000 Accessing width and height using index width: 4000 height: 6000

Pillow (PIL): Get image size (width, height) with size, width, height

A PIL.Image object, obtained by reading an image using Pillow (PIL), has size, width, and height attributes.

from PIL import Image img = Image.open('Krunal.jpg') print(img.size) print(type(img.size)) w, h = img.size print('width: ', w) print('height:', h) 
(200, 200) width: 200 height: 200 

Источник

Get image size (width, height) with Python, OpenCV, Pillow (PIL)

This article explains how to get the image size (width and height) in Python with OpenCV and Pillow (PIL).

You can obtain the image size as a tuple using the shape attribute of ndarray in OpenCV and the size attribute of PIL.Image in Pillow (PIL). It’s important to note that the order of width and height is different in OpenCV and Pillow (PIL).

Refer to the following articles for image resizing and getting the size of a file in bytes:

OpenCV: Get image size (width, height) with ndarray.shape

OpenCV treats an image as a NumPy array ndarray . The image size (width, height) can be obtained using the shape attribute, which returns a tuple of dimensions.

In addition to OpenCV, you can also obtain the image size using the shape attribute when using other libraries, such as Pillow, to read an image file and convert it into an ndarray .

Note that OpenCV loads color image files in BGR order, not RGB.

For color images

For color images, the ndarray is a 3D array with dimensions (height, width, 3) .

import cv2 im = cv2.imread('data/src/lena.jpg') print(type(im)) # print(im.shape) print(type(im.shape)) # (225, 400, 3) # 

To assign each value to a variable, unpack the tuple as follows:

h, w, c = im.shape print('width: ', w) print('height: ', h) print('channel:', c) # width: 400 # height: 225 # channel: 3 

When unpacking a tuple, it is a common convention to assign unused values to _ . In the following example, the number of colors (or channels) is not used:

h, w, _ = im.shape print('width: ', w) print('height:', h) # width: 400 # height: 225 

Of course, you can also directly access them by index.

print('width: ', im.shape[1]) print('height:', im.shape[0]) # width: 400 # height: 225 

If you want to get tuples in the order of (width, height) , you can use slicing, as demonstrated in the following example:

When passing the size as an argument to functions such as cv2.resize() , ensure it follows the (width, height) format.

See the following article for details of slicing:

For grayscale (monochrome) images

For grayscale images, the ndarray is a 2D array with dimensions (height, width) .

import cv2 im_gray = cv2.imread('data/src/lena.jpg', cv2.IMREAD_GRAYSCALE) print(im_gray.shape) print(type(im_gray.shape)) # (225, 400) # 

The procedure for grayscale images is essentially the same as for color images:

h, w = im_gray.shape print('width: ', w) print('height:', h) # width: 400 # height: 225 print('width: ', im_gray.shape[1]) print('height:', im_gray.shape[0]) # width: 400 # height: 225 

To assign width and height to variables, the following approach works for both color and grayscale images.

h, w = im.shape[0], im.shape[1] print('width: ', w) print('height:', h) # width: 400 # height: 225 

If you want to get a (width, height) tuple, you can use slicing. The latter approach ( [1::-1] ) works for both color and grayscale images.

print(im_gray.shape[::-1]) print(im_gray.shape[1::-1]) # (400, 225) # (400, 225) 

Pillow (PIL): Get image size (width, height) with size , width , height

A PIL.Image object, obtained by reading an image using Pillow (PIL), has the size , width , and height attributes.

The size attribute returns a (width, height) tuple.

from PIL import Image im = Image.open('data/src/lena.jpg') print(im.size) print(type(im.size)) # (400, 225) # w, h = im.size print('width: ', w) print('height:', h) # width: 400 # height: 225 

You can also get the width and height using the width and height attributes:

print('width: ', im.width) print('height:', im.height) # width: 400 # height: 225 

The process is the same for grayscale (monochrome) images:

im_gray = Image.open('data/src/lena.jpg').convert('L') print(im.size) print('width: ', im.width) print('height:', im.height) # (400, 225) # width: 400 # height: 225 

Источник

Python Pillow – Get Image Size

To get the size of image using Python Pillow, use size property of Image object. The size property returns the width and height of the image.

In this tutorial, we shall learn how to get the size of image, in other words, width and height of an image, using Pillow library.

Syntax – Image.size

The syntax to use size property of PIL Image object is given below.

im = Image.open("sample-image.png") im.size

Examples

1. Get size of given image

In the following program, we will read and image and then print its size using size property of the Image.

Python Program

from PIL import Image #read the image im = Image.open("sample-image.png") #image size print(im.size)

2. Access width and height from image size

You can access the height and width from size property using index. In the following example, we will get width and height of the image.

Python Program

from PIL import Image #read the image im = Image.open("sample-image.png") #image size width = im.size[0] height = im.size[1] print('Width of the image is:', width) print('Height of the image is:', height)
Width of the image is: 640 Height of the image is: 400

Summary

In this tutorial of Python Examples, we learned how to get the size of an image using Python Pillow library, with the help of well detailed example programs.

Источник

Python Pillow – Get Image Size

To get the size of image using Python Pillow, use size property of Image object. The size property returns the width and height of the image.

In this tutorial, we shall learn how to get the size of image, in other words, width and height of an image, using Pillow library.

Syntax – Image.size

The syntax to use size property of PIL Image object is given below.

im = Image.open("sample-image.png") im.size

Examples

1. Get size of given image

In the following program, we will read and image and then print its size using size property of the Image.

Python Program

from PIL import Image #read the image im = Image.open("sample-image.png") #image size print(im.size)

2. Access width and height from image size

You can access the height and width from size property using index. In the following example, we will get width and height of the image.

Python Program

from PIL import Image #read the image im = Image.open("sample-image.png") #image size width = im.size[0] height = im.size[1] print('Width of the image is:', width) print('Height of the image is:', height)
Width of the image is: 640 Height of the image is: 400

Summary

In this tutorial of Python Examples, we learned how to get the size of an image using Python Pillow library, with the help of well detailed example programs.

Источник

Get Image Size in OpenCV Python

In this OpenCV Tutorial, we will learn how to get image size in OpenCV Python using NumPy Array shape property, with an example.

OpenCV Python – Get Image Size

In Image Processing applications, it is often necessary to know the size of an image that is loaded or transformed through various stages.

When working with OpenCV Python, images are stored in numpy ndarray. To get the image shape or size, use ndarray.shape to get the dimensions of the image. Then, you can use index on the dimensions variable to get width, height and number of channels for each pixel.

In the following code snippet, we have read an image to img ndarray. And then we used ndarray.shape to get the dimensions of the image.

img = cv2.imread('/path/to/image.png') dimensions = img.shape

Example

In this example, we have read an image and used ndarray.shape to get the dimension.

OpenCV Python - Get Image Size Shape

We can access height, width and number of channels from img.shape: Height is at index 0, Width is at index 1; and number of channels at index 2.

Python Program

import cv2 # read image img = cv2.imread('/home/img/python.png', cv2.IMREAD_UNCHANGED) # get dimensions of image dimensions = img.shape # height, width, number of channels in image height = img.shape[0] width = img.shape[1] channels = img.shape[2] print('Image Dimension : ',dimensions) print('Image Height : ',height) print('Image Width : ',width) print('Number of Channels : ',channels)
Image Dimension : (149, 200, 4) Image Height : 149 Image Width : 200 Number of Channels : 4

img.shape returns (Height, Width, Number of Channels)

  1. Height represents the number of pixel rows in the image or the number of pixels in each column of the image array.
  2. Width represents the number of pixel columns in the image or the number of pixels in each row of the image array.
  3. Number of Channels represents the number of components used to represent each pixel.

In the above example, Number of Channels = 4 represent Alpha, Red, Green and Blue channels.

Conclusion

Concluding this OpenCV Python Tutorial, we have learnt how to get the image shape using OpenCV ndarray.shape.

Источник

Читайте также:  Python квадрат из символов
Оцените статью