web-dev-qa-db-ja.com

画像サイズ(Python、OpenCV)

私はc ++でそれを行うので、pythonで画像サイズを取得したいと思います。

int w = src->width;
printf("%d", 'w');
32
Andrea Diaz

パラメーターとして画像を使用して、モジュールGetSizeから関数 cv を使用します。幅と高さを2つの要素を持つTupleとして返します。

width, height = cv.GetSize(src)
17
halex

OpenCVとnumpyを使用すると、次のように簡単です。

import cv2

img = cv2.imread('path/to/img',0)
height, width = img.shape[:2]
98
cowhi

私はnumpy.size()を使用して同じことをします:

import numpy as np
import cv2

image = cv2.imread('image.jpg')
height = np.size(image, 0)
width = np.size(image, 1)
16
Rodrigo Lara

私にとって最も簡単な方法は、image.shapeによって返されるすべての値を取得することです。

height, width, channels = img.shape

チャンネル数が必要ない場合(画像がbgrかグレースケールかを判断するのに便利です)、値をドロップするだけです:

height, width, _ = img.shape
14
Stein

以下は、画像の寸法を返すメソッドです。

from PIL import Image
import os

def get_image_dimensions(imagefile):
    """
    Helper function that returns the image dimentions

    :param: imagefile str (path to image)
    :return dict (of the form: {width:<int>, height=<int>, size_bytes=<size_bytes>)
    """
    # Inline import for PIL because it is not a common library
    with Image.open(imagefile) as img:
        # Calculate the width and hight of an image
        width, height = img.size

    # calculat ethe size in bytes
    size_bytes = os.path.getsize(imagefile)

    return dict(width=width, height=height, size_bytes=size_bytes)
1
Roei Bahumi