在 Haskell 个图表中加载外部图像
Load external image in Haskell Diagrams
所以我有这个疑问。我已经有了处理本地(嵌入式)图像文件的代码,但现在我需要加载外部图像。用 loadImageExt
替换无用:
productImageIO :: IO (Diagram B)
productImageIO = do
res <- loadImageExt "https://s7d2.scene7.com/is/image/dkscdn/16JDNMJRDNCLPSBLKJRD_Black_Black_White_is/"
return $
case res of
Left err -> mempty
Right product -> image product
• No instance for (Renderable (DImage Double External) B)
arising from a use of ‘image’
• In the expression: image product
In a case alternative: Right product -> image product
In the second argument of ‘($)’, namely
‘case res of
Left err -> mempty
Right product -> image product’
|
26 | Right product -> image product
| ^^^^^^^^^^^^^
查看 loadImageExt and readImage 的源代码,我看不到它实际执行 http 内容以获取图像的位置。
文档说,loadImageExt,检查文件是否存在,并使用 JuicyPixels 计算出正确的大小,但保存对图像的引用而不是光栅数据
所以请原谅我的无知,但这是否意味着我需要在这种情况下添加http加载逻辑?或者我只是遗漏了一些要点来完成这项工作?
UPD:我可能错了,有一些快速简便的方法可以通过放置 link 来加载外部图像,但对我有用的是使用 http-conduit,在 ByteString 中获取响应并解析它:
productImageIO :: String -> IO (Diagram B)
productImageIO path = do
response <- fmap getResponseBody $ parseRequest path >>= httpBS
return $
case loadImageEmbBS response of
Left error -> mempty
Right decodedImage -> image decodedImage
它甚至可以加载 https 图片,像这样 https://sneakernews.com/wp-content/uploads/2018/01/jordan-russell-westbrook-signature-shoe-creamsicle-3.jpg?w=1140
loadImageExt
appears to expect a FilePath
, not a URL, but that's not what the compiler is complaining about. It is complaining that the particular image returned, a DImage Double External
, is not Renderable
. This could be because the renderer in use doesn't understand external images; for instance, Diagrams.Backend.SVG
appears to support Embedded
but not External
. In contrast, Diagrams.Backend.Html5
supports External
but not Embedded
. At a guess, it's expected that your diagram refers using local filenames to images, not cross-site URLs. It could be that you can inject URLs with uncheckedImageRef
, but still only if the backend supports External
. The Diagrams manual section on images 声称只有 Cairo 后端会这样做,但这可能不准确。
所以我有这个疑问。我已经有了处理本地(嵌入式)图像文件的代码,但现在我需要加载外部图像。用 loadImageExt
替换无用:
productImageIO :: IO (Diagram B)
productImageIO = do
res <- loadImageExt "https://s7d2.scene7.com/is/image/dkscdn/16JDNMJRDNCLPSBLKJRD_Black_Black_White_is/"
return $
case res of
Left err -> mempty
Right product -> image product
• No instance for (Renderable (DImage Double External) B)
arising from a use of ‘image’
• In the expression: image product
In a case alternative: Right product -> image product
In the second argument of ‘($)’, namely
‘case res of
Left err -> mempty
Right product -> image product’
|
26 | Right product -> image product
| ^^^^^^^^^^^^^
查看 loadImageExt and readImage 的源代码,我看不到它实际执行 http 内容以获取图像的位置。
文档说,loadImageExt,检查文件是否存在,并使用 JuicyPixels 计算出正确的大小,但保存对图像的引用而不是光栅数据
所以请原谅我的无知,但这是否意味着我需要在这种情况下添加http加载逻辑?或者我只是遗漏了一些要点来完成这项工作?
UPD:我可能错了,有一些快速简便的方法可以通过放置 link 来加载外部图像,但对我有用的是使用 http-conduit,在 ByteString 中获取响应并解析它:
productImageIO :: String -> IO (Diagram B)
productImageIO path = do
response <- fmap getResponseBody $ parseRequest path >>= httpBS
return $
case loadImageEmbBS response of
Left error -> mempty
Right decodedImage -> image decodedImage
它甚至可以加载 https 图片,像这样 https://sneakernews.com/wp-content/uploads/2018/01/jordan-russell-westbrook-signature-shoe-creamsicle-3.jpg?w=1140
loadImageExt
appears to expect a FilePath
, not a URL, but that's not what the compiler is complaining about. It is complaining that the particular image returned, a DImage Double External
, is not Renderable
. This could be because the renderer in use doesn't understand external images; for instance, Diagrams.Backend.SVG
appears to support Embedded
but not External
. In contrast, Diagrams.Backend.Html5
supports External
but not Embedded
. At a guess, it's expected that your diagram refers using local filenames to images, not cross-site URLs. It could be that you can inject URLs with uncheckedImageRef
, but still only if the backend supports External
. The Diagrams manual section on images 声称只有 Cairo 后端会这样做,但这可能不准确。