shibukawa / imagesize_py

MIT License
227 stars 44 forks source link

Reading image size of remote file #44

Open SpangleLabs opened 4 years ago

SpangleLabs commented 4 years ago

Has anyone considered adding this functionality? From my brief experimentation, it's a little more tricky than swapping

    # with open(str(filepath), 'rb') as fhandle:
    with urllib.request.urlopen(str(url)) as fhandle:

Though that works fine for png files, anything that needs to seek, like a jpeg, will fail.

ffreemt commented 4 years ago

Has anyone considered adding this functionality? From my brief experimentation, it's a little more tricky than swapping

    # with open(str(filepath), 'rb') as fhandle:
    with urllib.request.urlopen(str(url)) as fhandle:

Though that works fine for png files, anything that needs to seek, like a jpeg, will fail.

Need to read the raw bytes and wrap the bunch in io.BytesIO, for exmaple

raw = urllib.request.urlopen(str(url)).read() fhandle = io.BytesIO(raw) # Nothing needs to be changed after this

See PR#46 (from me right) https://github.com/shibukawa/imagesize_py/pull/46

SpangleLabs commented 4 years ago

Yeah, I was wondering if it could be done without reading the whole image into memory, but that's probably over-optimising! Your solution looks like it would probably be fine

yucongo commented 4 years ago

without reading the whole image into memory Can probably be done, I remember seeing some post in SO doing similar stuff, wont be just inserting a few lines though

SpangleLabs commented 4 years ago

Indeed, I was planning on researching this and giving it a go later this month. My suspicion is that it can be done for png, just request a few bytes from the top of the file, but for jpeg, it might be necessary to request the whole file, rather than requesting various little sections of it.