如何使用 Rawpy 图像处理库从 URL 读取图像?

How do I read an image using Rawpy image processing library, from a URL?

如何使用 rawpy 库从 url 读取图像?

我试过了

filepath = www.google.com/image.jpeg

im = rawpy.imread(filepath)

但是没用。

我深入研究了 rawpy 的代码,它说它需要“路径或文件”。

我也尝试过其他方法,例如使用内存对象 BytesIO 中的临时文件,但 none 它有效。

请提供帮助。

编辑一:

关于 urllib.request 库,这些是我遇到的错误:

我)

import rawpy
import io
import urllib

filepath = 'https://filesamples.com/samples/image/jpeg/sample_640%C3%97426.jpeg'

data = urllib.request.urlopen(filepath).read()
stream = io.BytesIO(data)
im = rawpy.imread(stream)

产生错误

LibRawFileUnsupportedError                Traceback (most recent call last)
<ipython-input-88-7965dccc6908> in <module>
     35 stream = io.BytesIO(data)
---> 36 im = rawpy.imread(stream)
     37 
     38 """

~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/rawpy/__init__.py in imread(pathOrFile)
     16     d = RawPy()
     17     if hasattr(pathOrFile, 'read'):
---> 18         d.open_buffer(pathOrFile)
     19     else:
     20         d.open_file(pathOrFile)

rawpy/_rawpy.pyx in rawpy._rawpy.RawPy.open_buffer()

rawpy/_rawpy.pyx in rawpy._rawpy.RawPy.handle_error()

LibRawFileUnsupportedError: b'Unsupported file format or not RAW file'

ii)

import rawpy
import io
import urllib
import tempfile
import shutil

filepath = 'https://filesamples.com/samples/image/jpeg/sample_640%C3%97426.jpeg'

with urllib.request.urlopen(filepath) as response:
    with tempfile.NamedTemporaryFile(delete = False) as tmp_file:
        shutil.copyfileobj(response,tmp_file)
        im = rawpy.imread(tmp_file)

产生错误

LibRawIOError                             Traceback (most recent call last)
<ipython-input-89-ae0405bcc04a> in <module>
     42         shutil.copyfileobj(response,tmp_file)
---> 43         im = rawpy.imread(tmp_file)
     44 

~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/rawpy/__init__.py in imread(pathOrFile)
     16     d = RawPy()
     17     if hasattr(pathOrFile, 'read'):
---> 18         d.open_buffer(pathOrFile)
     19     else:
     20         d.open_file(pathOrFile)

rawpy/_rawpy.pyx in rawpy._rawpy.RawPy.open_buffer()

rawpy/_rawpy.pyx in rawpy._rawpy.RawPy.handle_error()

LibRawIOError: b'Input/output error'

iii)

import rawpy
import io
import urllib
import tempfile
import shutil

filepath = 'https://filesamples.com/samples/image/jpeg/sample_640%C3%97426.jpeg'

with urllib.request.urlopen(filepath) as response:
    data = response.read()
    im = rawpy.imread(data)

产生错误

AttributeError                            Traceback (most recent call last)
<ipython-input-90-f10bfdef4333> in <module>
     55     data = response.read() 
---> 56     im = rawpy.imread(data)
     57 

~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/rawpy/__init__.py in imread(pathOrFile)
     18         d.open_buffer(pathOrFile)
     19     else:
---> 20         d.open_file(pathOrFile)
     21     return d

rawpy/_rawpy.pyx in rawpy._rawpy.RawPy.open_file()

AttributeError: 'bytes' object has no attribute 'encode'

编辑二: 我试过的其他没有使用 urllib 库的东西是

i) 在内存文件中

import io
import rawpy 

filepath = 'https://filesamples.com/samples/image/jpeg/sample_640%C3%97426.jpeg'

response = requests.get(filepath)
in_mem_file = io.BytesIO(response.content)
im = rawpy.imread(in_mem_file)

产生错误

LibRawFileUnsupportedError                Traceback (most recent call last)
<ipython-input-91-1ef650bdc042> in <module>
     48 response = requests.get(filepath)
     49 in_mem_file = io.BytesIO(response.content)
---> 50 im = rawpy.imread(in_mem_file)
     51 

~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/rawpy/__init__.py in imread(pathOrFile)
     16     d = RawPy()
     17     if hasattr(pathOrFile, 'read'):
---> 18         d.open_buffer(pathOrFile)
     19     else:
     20         d.open_file(pathOrFile)

rawpy/_rawpy.pyx in rawpy._rawpy.RawPy.open_buffer()

rawpy/_rawpy.pyx in rawpy._rawpy.RawPy.handle_error()

LibRawFileUnsupportedError: b'Unsupported file format or not RAW file'

ii) 智能开启

from smart_open import open
import rawpy 

filepath = 'https://filesamples.com/samples/image/jpeg/sample_640%C3%97426.jpeg'

def get_file(filepath):
    with open(filepath, 'rb') as s3_source:
        return s3_source
        
s3_source = get_file(filepath)
im = rawpy.imread(s3_source)

产生错误

LibRawIOError                             Traceback (most recent call last)
<ipython-input-92-03b94616ed66> in <module>
     55         return s3_source
     56 s3_source = get_file(filepath)
---> 57 im = rawpy.imread(s3_source)

~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/rawpy/__init__.py in imread(pathOrFile)
     16     d = RawPy()
     17     if hasattr(pathOrFile, 'read'):
---> 18         d.open_buffer(pathOrFile)
     19     else:
     20         d.open_file(pathOrFile)

rawpy/_rawpy.pyx in rawpy._rawpy.RawPy.open_buffer()

rawpy/_rawpy.pyx in rawpy._rawpy.RawPy.handle_error()

LibRawIOError: b'Input/output error'

编辑三(解):

好的,我设法通过使用原始图像格式数据 (.dng) 解决了这个问题。

这是我的代码

import rawpy
import BytesIO
import imageio

filepath = 'www.google.com/sample_file.dng'
response = requests.get(filepath)
raw = rawpy.imread(BytesIO(response.content))
rgb = raw.postprocess()
imageio.imsave('test_rawpy.jpeg', rgb)

JPEG 不是 Raw Image Format。 您需要发送一些原始数据作为输入。

所以,

  1. 如果您只想处理一些 JPEG,请尝试 Pillow
  2. 如果您想处理原始图像,请更改您的输入数据。