Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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/8/python-3.x/16.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
Image 使用python 3.6.0在elasticsearch中存储图像的正确方法_Image_Python 3.x_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Image,Python 3.x,elasticsearch" /> elasticsearch,Image,Python 3.x,elasticsearch" />

Image 使用python 3.6.0在elasticsearch中存储图像的正确方法

Image 使用python 3.6.0在elasticsearch中存储图像的正确方法,image,python-3.x,elasticsearch,Image,Python 3.x,elasticsearch,约束 语言Python 3.6.0 图像大小高达5MB[格式.PNG.JPG和.JEPG] 我必须在elasticsearch中存储图像。这是一项要求。然而,只要图像可以重建,我使用哪种格式并不重要 我有图像的物理位置。我打开图像并将其转换为base64格式。然后我尝试在运行在我的localhost上的elasticsearch中对其进行索引。但它不起作用。我假设我需要在这里使用批量api。但我发现批量api执行操作或生成器。在我的情况下,如何使用批量在elasticsearch中保存图像?

约束

  • 语言Python 3.6.0
  • 图像大小高达5MB[格式.PNG.JPG和.JEPG]
  • 我必须在elasticsearch中存储图像。这是一项要求。然而,只要图像可以重建,我使用哪种格式并不重要
我有图像的物理位置。我打开图像并将其转换为
base64
格式。然后我尝试在运行在我的
localhost
上的elasticsearch中对其进行索引。但它不起作用。我假设我需要在这里使用批量api。但我发现批量api执行
操作
生成器
。在我的情况下,如何使用批量在elasticsearch中保存图像?或者在elasticsearch中是否有其他有效的方法来索引图像

请注意,我可以成功地将图像加载并编码为
字节
。另外,其他
索引
搜索(GET)
查询在我的
localhost:9200
上运行良好

这是我目前的做法

from elasticsearch import Elasticsearch
import uuid
import base64

client = Elasticsearch([{'host': 'localhost', 'port':9200}])
def persist_image_in_elastic(imagePath):
     curMethodst = time.time()
     # imagePath = 'images/heroalom/image_22.png'
     with open(imagePath, "rb") as imageFile:
          rawImage = base64.b64encode(imageFile.read())

     elasticIndex = 'raw-image-index'
     doc_type = 'raw-image'
     rawImageModel = {'id': 'f00b5f7c17534d22ab5cfb950bea972c', 'raw': rawImage }
     elasticResp = client.index(index = elasticIndex, doc_type = doc_type,id = idForReceivedImage, body = rawImageModel)
弹性搜索的映射

{
   "raw-image-index": {
      "mappings": {
         "raw-image": {
            "properties": {
               "id": {
                  "type": "text"
               },
               "raw": {
                  "type": "text"
               }
            }
         }
      }
   }
}

你就快到了。您只需在
str()
调用中包装
rawImage
,如下所示:

rawImageModel = {'id': 'f00b5f7c17534d22ab5cfb950bea972c', 'raw': str(rawImage) }
现在我来解释一下。返回类型为的对象,而ElasticSearch客户端需要
string

事实上,您提供的python代码引发了一个可用于调试的异常:

Traceback (most recent call last):
  File "code.py", line 19, in <module>
    persist_image_in_elastic('/Users/vasiliev/Downloads/es_logo_small.png')
  File "code.py", line 17, in persist_image_in_elastic
    elasticResp = client.index(index = elasticIndex, doc_type = doc_type,id = 'f00b5f7c17534d22ab5cfb950bea972c', body = rawImageModel)
  File "/Users/vasiliev/.virtualenvs/es-blob-3.6/lib/python3.6/site-packages/elasticsearch/client/utils.py", line 73, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/Users/vasiliev/.virtualenvs/es-blob-3.6/lib/python3.6/site-packages/elasticsearch/client/__init__.py", line 298, in index
    _make_path(index, doc_type, id), params=params, body=body)
  File "/Users/vasiliev/.virtualenvs/es-blob-3.6/lib/python3.6/site-packages/elasticsearch/transport.py", line 278, in perform_request
    body = self.serializer.dumps(body)
  File "/Users/vasiliev/.virtualenvs/es-blob-3.6/lib/python3.6/site-packages/elasticsearch/serializer.py", line 50, in dumps
    raise SerializationError(data, e)
elasticsearch.exceptions.SerializationError: ({'id': 'f00b5f7c17534d22ab5cfb950bea972c', 'raw': b'iVB...mCC'}, TypeError("Unable to serialize b'iVB...mCC' (type: <class 'bytes'>)",))

另一方面,您使用的是哪个IDE?(只是问一下)为了处理这段代码,我只使用了vim。对于较大的python应用程序/脚本,我使用PyCharm,它有社区版(免费),适合我的需要。
{
   "raw-image-index": {
      "mappings": {
         "raw-image": {
            "properties": {
               "id": {
                  "type": "text"
               },
               "raw": {
                  "type": "text",
                  "index": "no"
               }
            }
         }
      }
   }
}