I want to call this Picture bed API to upload my picture and return its link.I have translated the official instructional the page into English.And there is a example succeed to use it with Python. I want to use it in Mathematica also, but the parameter (smfile
) seem is Python image object.Such as following try.So how to make it?The img
in code is
http://o8aucf9ny.bkt.clouddn.com/2016-10-14-17-53-00.png
Try one:
URLExecute["https://sm.ms/api/upload", <|"Method" -> "POST",
"smfile" -> img|>]
Try two:
URLExecute["https://sm.ms/api/upload", <|"Method" -> "POST",
"smfile" ->
FromCharacterCode[ToCharacterCode[ImageData[img], "UTF-8"],
"Unicode"]|>]
I think the key of failure is that "smfile"
should be a Python object but not a common Mathematica expression or maybe there are other workaround can do this.
Answer
Using URLFetch[]
:
smmsUpload[img_, detailed : (True | False) : False] := Module[{raw},
raw = ImportString[URLFetch["https://sm.ms/api/upload", Method -> "POST",
"MultipartElements" -> {{"smfile\"; filename=\"tmp.png",
"image/png"} -> ExportString[img, "PNG"]}], "RawJSON"];
If[raw === $Failed || Lookup[raw, "code", "error"] =!= "success",
Echo[raw["msg"]]; Return[$Failed]];
If[! detailed, raw["data", "url"],
Grid[Transpose[{{"Dimensions:", "Hash:", "Image Link:", "Delete Link:"},
MapAt[Hyperlink, FlattenAt[TakeDrop[
Lookup[raw["data"], {"width", "height", "hash", "url", "delete"}],
2], 2], {{3}, {4}}]}], Alignment -> Left]]]
smmsUpload[ExampleData[{"AerialImage", "Pentagon"}], True]
I couldn't get URLRead[]
/URLExecute[]
to work, so I'll leave that for somebody else to do.
Comments
Post a Comment