Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading image size of remote file #44

Open
SpangleLabs opened this issue Sep 10, 2020 · 4 comments
Open

Reading image size of remote file #44

SpangleLabs opened this issue Sep 10, 2020 · 4 comments

Comments

@SpangleLabs
Copy link

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
Copy link
Contributor

ffreemt commented Oct 4, 2020

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

@SpangleLabs
Copy link
Author

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
Copy link

yucongo commented Oct 5, 2020

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants