4

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.

yode
  • 26,686
  • 4
  • 62
  • 167
  • Please make this question self contained. Include your mma code and explanation what is going wrong. – Kuba Oct 14 '16 at 09:17
  • "smfile is a Python image object" -- the docs suggest it a normal PNG file. Have you tried the method in the post that you commented on (http://mathematica.stackexchange.com/a/97658/1043)? – ZachB Oct 14 '16 at 17:25
  • @ZachB Yes,I have noted that post and give some try,but I am lack of this area knowledge.I don't know how to do it still. – yode Oct 14 '16 at 17:30

2 Answers2

5

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]

result

I couldn't get URLRead[]/URLExecute[] to work, so I'll leave that for somebody else to do.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
  • That If scare me, actually this answer is what I want exactly which can upload a image produced compute.Thanks very much,maybe you can change the URLFetch to URLRead enlightened by SqRoots's answer. – yode Oct 15 '16 at 08:24
  • As your answer,I think HTTPRequest["https://sm.ms/api/upload",<|Method -> "POST","Body"->{"smfile"-><|"Content" -> ExportString[img,"PNG"],"MIMEType" -> "image/png","filename" -> "tmp.png"|>}|>] should work,but I don't know what happen. – yode Oct 15 '16 at 08:50
  • Did you try your proposal already if it works? – J. M.'s missing motivation Oct 15 '16 at 09:08
  • That work,Would you mind update your answer? – yode Oct 16 '16 at 03:05
3

In Mma 11, you can use this function URLRead[].

e.g.

image = "E:/yourPic.jpg";(*Your Picture*)
url = "https://sm.ms/api/upload";(*Picture Bed URL*)
req = HTTPRequest[url, <|"Body" -> {"smfile" -> <|"Content" -> File[image], 
    "Name" -> FileNameTake[image]|>}|>];(*Upload Request, the "Name" is unnecessary*)
res = URLRead[req, "Body"];(*Upload, return JSON String*)
assoc = ImportString[res, "RawJSON"](*convert to Association*)

Then, assoc is what you want.

  • URL: assoc["data"]["url"]
  • delete: ImportString[URLRead[assoc["data"]["delete"], "Body"]]
  • size: assoc["data"]["size"]
  • width: assoc["data"]["width"]
  • height: assoc["data"]["height"]
Li Xuan
  • 336
  • 1
  • 5
  • "Name" is certainly unnecessary if the image is coming from a file, but what would you propose if the image is already in Mathematica? Exporting it as a file just to upload it seems wasteful... – J. M.'s missing motivation Oct 15 '16 at 08:59
  • I think it is wasteful for I/O, but not to much for CPU. yode's way works, and not need export. – Li Xuan Oct 16 '16 at 02:24