Reading image size of remote file
SpangleLabs opened this issue · 4 comments
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.
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) #46
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
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
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.