Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
图像大小(Python、OpenCV) 我想在Python中获得图像大小,就像我用C++做的一样。 int w = src->width; printf("%d", 'w');_Python_Opencv_Size - Fatal编程技术网

图像大小(Python、OpenCV) 我想在Python中获得图像大小,就像我用C++做的一样。 int w = src->width; printf("%d", 'w');

图像大小(Python、OpenCV) 我想在Python中获得图像大小,就像我用C++做的一样。 int w = src->width; printf("%d", 'w');,python,opencv,size,Python,Opencv,Size,使用模块cv中的函数,将图像作为参数。它以包含2个元素的元组的形式返回宽度和高度: width, height = cv.GetSize(src) 使用openCV和numpy可以很容易地做到: import cv2 img = cv2.imread('path/to/img',0) height, width = img.shape[:2] 我使用numpy.size()执行相同的操作: import numpy as np import cv2 image = cv2.imread(

使用模块
cv
中的函数,将图像作为参数。它以包含2个元素的元组的形式返回宽度和高度:

width, height = cv.GetSize(src)

使用openCV和numpy可以很容易地做到:

import cv2

img = cv2.imread('path/to/img',0)
height, width = img.shape[:2]
我使用numpy.size()执行相同的操作:

import numpy as np
import cv2

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

对我来说,最简单的方法是获取image.shape返回的所有值:

height, width, channels = img.shape
如果不需要通道数(用于确定图像是bgr还是灰度),只需删除以下值:

height, width, _ = img.shape

下面是一个返回图像维度的方法:

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)
从PIL导入图像
导入操作系统
def get_图像尺寸(图像文件):
"""
返回图像尺寸的Helper函数
:param:imagefile str(图像路径)
:return dict(格式:{宽度:,高度=,大小\字节=)
"""
#PIL的内联导入,因为它不是公共库
使用Image.open(imagefile)作为img:
#计算图像的宽度和高度
宽度、高度=img.size
#以字节为单位计算大小
size\u bytes=os.path.getsize(图像文件)
返回dict(宽度=宽度,高度=高度,大小字节=大小字节)
来自本教程:


从另一个教程:

image=cv2.imread(“jp.png”)

(h,w,d)=图像形状


在发布答案之前,请仔细检查一下。

我相信简单的
img.shape[-1::-1]
会更好。

您可以使用image.shape来获取图像的尺寸。它返回3个值。第一个值是图像的宽度,第二个值是高度,最后一个值是通道。这里不需要最后一个值,所以您可以使用下面的代码来获取图像的高度和宽度:

width, height = src.shape[:2]<br>
print(width, height)
width,height=src.shape[:2]
打印(宽度、高度)
<代码> >代码>语法>什么?以前从未见过……访问对象的属性(如PHP)。如果是,为什么他们不使用点运算符(<代码>?/代码>)?@ YATARTROROCK中的C++ >代码> A->B <代码>等效于<代码>(*A).b
@GabiPurcaru好的,那么
*
操作符做什么呢?在Python中,它解包了一个元组或列表…它被称为解引用,允许我们访问*操作符指向的变量值。该函数在OpenCV 3中似乎不存在。可能它在不久前被删除了?链接似乎断开了,看起来像这个函数h已折旧。为什么需要为此导入
numpy
?它在内部使用numpy
width, height = src.shape[:2]<br>
print(width, height)