Skip to content

Instantly share code, notes, and snippets.

@maethu
Created August 29, 2017 19:04
Show Gist options
  • Save maethu/159227cda2ff0e146ae8e708e8d9a6a5 to your computer and use it in GitHub Desktop.
Save maethu/159227cda2ff0e146ae8e708e8d9a6a5 to your computer and use it in GitHub Desktop.
JPEG_START_BYTE = '\xff\xd8'
JPEG_END_BYTE = '\xff\xd9'
def get_jpeg(stream):
start_pos = None
end_pos = None
while True:
frame_bytes = stream.read(1024)
start_pos = None
end_pos = None
if JPEG_START_BYTE in frame_bytes:
start_pos = frame_bytes.index(JPEG_START_BYTE)
if JPEG_END_BYTE in frame_bytes:
end_pos = frame_bytes.index(JPEG_END_BYTE)
yield frame_bytes[start_pos:end_pos]
if start_pos and end_pos:
break
site = opener.open(url)
data = ''.join(tuple(get_jpeg(site))) # or next(get_jpeg(site))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment