【问题标题】:Compare one image with Cloudinary storage将一张图像与 Cloudinary 存储进行比较
【发布时间】:2019-12-06 03:09:55
【问题描述】:

我想将一个文件与 Cloudinary 路径中的完整照片路径进行比较,但我不知道如何做。有人可以帮我吗?

这是我用于本地路径的代码

import cv2
import os
import cloudinary

directory = './images'
upload = cv2.imread("images/img1.jpg")

for entry in os.listdir(directory):

    if entry.lower().endswith( ('.jpg', '.jpeg', '.png', '.gif') ):

        fullname = os.path.join(directory, entry)
        print('fullname:', fullname)
        duplicate = cv2.imread(fullname)

        if upload.shape == duplicate.shape:
            print("The images have same size and channels")

            #difference = cv2.subtract(upload, duplicate)
            #b, g, r = cv2.split(difference) 
            #if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
            if (upload == duplicate).all():
                print("images are the same")

        else:
            print("images are different")

【问题讨论】:

  • 在这种情况下,您必须从 http 链接读取图像。 import urllib.request as req url = 'your-url-to-image-file' req.urlretrieve(url, "filename.jpg") img = cv2.imread('filename.jpg')
  • 这是否也适用于一个 Cloudinary 存储中的多个文件?
  • 循环遍历cloudinary storage中文件夹中的所有文件。
  • 我该怎么做?
  • 你的意思是 res.cloudinairy.com/cloudname/ ?

标签: python opencv cloudinary


【解决方案1】:

使用以下代码将给定图像与云存储文件夹中的图像进行比较。我正在按照上面代码中描述的逻辑比较两个图像。

import urllib.request as req 
import cloudinary            # pip install cloudinary

cloudinary.config(
  cloud_name = 'YOUR-CLOUD-NAME',  
  api_key = 'YOUR-CLOUDINARY-API-KEY',  
  api_secret = 'YOUR-CLOUDINARY-API-SECRET-KEY'  
)

# image you want to compare with images present in cloudinary storage folder
image = cv2.imread('sample_image.jpg')
print(image.shape)

for i in cloudinary.Search().expression("folder:your_folder_name").execute()['resources']:
    img_url = cloudinary.CloudinaryImage(i['url']).build_url()
    # sample-url: http://res.cloudinary.com/duutxbavx/image/upload/v1564333096/test/flower3_zj6hqi.jpg

    img_ext = img_url.split('/')[8].split('.')[1]
    req.urlretrieve(img_url, "filename"+img_ext) 

    img = cv2.imread('filename'+img_ext)
    print(img_url,'\n',(img_ext, img.shape))

    if image.shape == img.shape:
        print("The images have same size and channels")

        if (image == img).all():
                print("images are the same")
    else:
        print("images are different")

Cloudinary 文件夹结构示例:

输出:

(243, 208, 3)
http://res.cloudinary.com/duutxbavx/image/upload/v1564333097/test/flower1_kosjtw.jpg 
 ('jpg', (275, 183, 3))
images are different
http://res.cloudinary.com/duutxbavx/image/upload/v1564333097/test/flower4_kvbehv.jpg 
 ('jpg', (258, 195, 3))
images are different
http://res.cloudinary.com/duutxbavx/image/upload/v1564333096/test/flower2_nmrkfq.jpg 
 ('jpg', (183, 275, 3))
images are different
http://res.cloudinary.com/duutxbavx/image/upload/v1564333096/test/flower3_zj6hqi.jpg 
 ('jpg', (185, 272, 3))
images are different

【讨论】:

  • 是的。欢迎。
  • 您可以将image = cv2.imread('sample_image.jpg') 也用于本地文件吗?因为它似乎对我不起作用..?
  • 这是读取图像的正常方式。为什么它不起作用?错误是什么?
  • 检查图片路径是否正确。否则,您将得到无。
  • 我修复了错误,它是路径。但我只得到 (870, 700, 3) 它没有告诉我它是否相同或者不是 url
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
  • 2023-03-06
  • 2015-11-01
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多