【问题标题】:Python - Find out aspect ratio of imagePython - 找出图像的纵横比
【发布时间】:2020-07-30 00:52:41
【问题描述】:

我正在编写一个脚本,它可以自动让我从网站下载图像并将它们设置为我的背景。但我只想要横向/横向图片,python 有没有办法查看图片的纵横比,然后根据它是横向还是纵向丢弃它们?在下载图片之后,还是在使用 Selenium 之前,这会更容易吗?

【问题讨论】:

  • 欢迎来到 StackOverflow!,为了让社区更好地帮助您,请更详细地解释您的代码,并尝试在您需要帮助的地方放置一段 sn-p 代码。假设你已经用PIL读取了你的图片然后-->im = Image.open('whatever.png') width, height = im.size然后aspect_ratio = weight/height,那么你可以判断是横向/纵向

标签: python landscape aspect-ratio


【解决方案1】:

您可以使用 Python OpenCV 库来实现这一点,here 可用。 使用 openCV,您可以读取所选图像的宽度和高度。就像这样:

import cv2

im = cv2.imread('www.your_image_url.com')
h, w, c = im.shape
# h is the image height, and w is the width. Note that for portrait images w is always less that h
# then pass your download condition
if w < h:
   # do not download image code
else:
   # download image code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-25
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多