4
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ Please make this question self contained. Include your mma code and explanation what is going wrong. $\endgroup$ Commented Oct 14, 2016 at 9:17
  • $\begingroup$ "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 (mathematica.stackexchange.com/a/97658/1043)? $\endgroup$ Commented Oct 14, 2016 at 17:25
  • $\begingroup$ @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. $\endgroup$ Commented Oct 14, 2016 at 17:30

2 Answers 2

5
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$ Commented Oct 15, 2016 at 8:24
  • $\begingroup$ 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. $\endgroup$ Commented Oct 15, 2016 at 8:50
  • $\begingroup$ Did you try your proposal already if it works? $\endgroup$ Commented Oct 15, 2016 at 9:08
  • $\begingroup$ That work,Would you mind update your answer? $\endgroup$ Commented Oct 16, 2016 at 3:05
3
$\begingroup$

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"]
$\endgroup$
2
  • $\begingroup$ "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... $\endgroup$ Commented Oct 15, 2016 at 8:59
  • $\begingroup$ I think it is wasteful for I/O, but not to much for CPU. yode's way works, and not need export. $\endgroup$ Commented Oct 16, 2016 at 2:24

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.