python - Upload image with an in-memory stream to input using Pillow + WebDriver? -


i'm getting image url pillow, , creating stream (bytesio/stringio).

r = requests.get("http://i.imgur.com/sh9lkxu.jpg") stream = image.open(bytesio(r.content)) 

since want upload image using <input type="file" /> selenium webdriver. can upload file:

self.driver.find_element_by_xpath("//input[@type='file']").send_keys("path_to_image") 

i know if possible upload image stream without having mess files / file paths... i'm trying avoid filesystem read/write. , in-memory or temporary files. i'm wondering if stream encoded base64, , uploaded passing string send_keys function can see above :$

ps: hope image :p

you seem asking multiple questions here.


first, how convert a jpeg without downloading file? you're doing that, don't know you're asking here.


next, "and in-memory or temporary files." don't know means, can temporary files tempfile library in stdlib, , can in-memory too; both easy.


next, want know how streaming upload requests. easy way that, explained in streaming uploads, "simply provide file-like object body". can tempfile, can bytesio. since you're using 1 in question, assume know how this.

(as side note, i'm not sure why you're using bytesio(r.content) when requests gives way use response object file-like object, , streaming on demand instead of waiting until full content available, isn't relevant here.)

if want upload selenium instead of requests… well do need temporary file. whole point of selenium it's scripting web browser. can't type bunch of bytes @ web browser in upload form, have select file on filesystem. selenium needs fake selecting file on filesystem. perfect job tempfile.namedtemporaryfile.


finally, "i'm wondering if stream encoded base64".

sure can. since you're converting image in-memory, can encode with, e.g., base64.b64encode. or, if prefer, can wrap bytesio in codecs wrapper base-64 on fly. i'm not sure why want here.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -