camgrab
Python library to download images from network accessible webcams.
Features
Out of the box, handles any webcam which provides a publically accessible URL to a JPG snapshot of their current image.
Easily swap in different downloaders to handle cams which expose their snapshots in a more complex way.
Highly configurable.
Provides a simple base for more advanced functionality, such as motion detection, CCTV systems, image analysis, etc.
Installation
pip install camgrab
Quick start
To simply grabbing images from a webcam every 2 seconds and start saving them to the default grabbed_images directory:
from camgrab import Grabber
grabber = Grabber('http://www.masconcable.ca/webcams/chase.jpg')
grabber.begin()
Examples
Quickly add a custom result handler
Here we create a custom result handler which will simply print the dimensions of the grabbed image.
The default result handlers (e.g. do_save_image()) will still be used.
from camgrab import Grabber
url = 'http://www.masconcable.ca/webcams/chase.jpg'
def print_dimensions(result, grabber):
if not result.get('image', None):
return
width, height = result['image'].size
print(
'{width} pixels wide, {height} pixels high!'.
format(width=width, height=height)
)
grabber = Grabber(url, every=5, extra_result_handlers=(print_dimensions, ))
grabber.begin()
More complex result handling
In this example, we’ll take full control of the result handling, creating a chain of result handlers to:
resize the image to 320x200
rotate the image by 90 degrees
save the image
print the final result dictionary to the terminal
from camgrab import Grabber
from camgrab.camgrab import do_save_image
url = 'http://www.masconcable.ca/webcams/chase.jpg'
def resize_image(result, grabber):
if not result.get('image', None):
return
result['image'] = result['image'].resize((320, 200))
def rotate_image(result, grabber):
if not result.get('image', None):
return
result['image'] = result['image'].rotate(90)
def print_result(result, grabber):
print(result)
# Setting result_handlers attribute completely overrides any default result
# handlers previously set. Hence making sure `do_save_image` (which is normally
# a default handler) is in this tuple:
result_handlers = (resize_image, rotate_image, do_save_image, print_result)
grabber = Grabber(url, every=5)
grabber.result_handlers = result_handlers
grabber.begin()
Take control of the main loop
If the the main loop created by the begin() method is too simple for your needs, then either override the begin() method or simply call tick() from your own consumer.
In this example, we consume a Grabber but define our own (not terribly useful) main loop which waits a random amount of time between ticks:
from random import random
from time import sleep
from camgrab import Grabber
url = 'http://www.masconcable.ca/webcams/chase.jpg'
grabber = Grabber(url)
while True:
grabber.tick()
# Wait somewhere between 0 and 10 seconds
sleep(random() * 10)
Error handling
Grabbing images from webcams is a messy business… They go offline loads, send corrupted images, sometimes they randomly start sending Server 500 errors, etc.
Because of all this, camgrab’s default settings make it pretty tolerant of common errors which occur when grabbing an image. But this can be configured easily enough.
When an unhandled exception is raised during a tick() which causes a crash, the exception which caused the crash can be found in the failed_exception attribute.
HTTP errors
HTTP errors can be ignored or raised by setting ignore_xxx attributes. For example…
By default HTTP 404 errors are not ignored by default. So when a 404 error occurs the grabber will crash and you can handle the exception in whatever way you want.
from urllib.error import HTTPError
from camgrab import Grabber
grabber = Grabber('http://www.masconcable.ca/webcams/chase.jpg')
try:
grabber.begin()
except HTTPError as e:
if e.code == 404:
print('Was it something I said?')
If you’d rather HTTP 404 errors didn’t cause a crash, then set the ignore_404 attribute:
from camgrab import Grabber
grabber = Grabber('http://www.masconcable.ca/webcams/chase.jpg')
grabber.ignore_404 = True
grabber.begin()
Now when a 404 error occurs, the Grabber will:
add the exception to the result dictionary
set the image in the result dictionary to None
And then continue its normal routine.
By default, the following HTTP status codes are ignored:
307, 400, 408, 409, 429, 444, 451, 499, 500, 502, 503, 504, 507, 599
Network errors
camgrab ignores network errors by default. If you’d rather network timeouts caused a crash, then just set the ignore_timeout attribute:
from socket import timeout
from urllib.error import URLError
from camgrab import Grabber
grabber = Grabber('http://www.masconcable.ca/webcams/chase.jpg')
grabber.ignore_timeout = False
try:
grabber.begin()
except URLError as e:
if isinstance(e.reason, timeout):
print("It's me, not you")
License
camgrab is free software, distributed under the MIT license.
Metadata
Release files for camgrab 0.9.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| camgrab-0.9.1.tar.gz | 206.7 kB | Without provenance | Details for camgrab-0.9.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| camgrab-0.9.1-py3-none-any.whl | Python 3 | none | any | Details for camgrab-0.9.1-py3-none-any.whl |
Total release size: 218.4 kB
Release files / camgrab-0.9.1.tar.gz
| Download URL | camgrab-0.9.1.tar.gz |
|---|---|
| Size | 206.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f8c17dfa4bcb2cba478c860bd9a2237c98b4762cb23aa4163d28c82eaa30195f
|
|
BLAKE2b-256 checksum How to use checksums |
87704443702b723f6bf5b136609f874eb8ca9a7e9e4ca3082befab3be7bb8a17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / camgrab-0.9.1-py3-none-any.whl
| Download URL | camgrab-0.9.1-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
972d3312e37ae06d16814e598506e97b6d6bf8370671b121b6ea68172f289b9b
|
|
BLAKE2b-256 checksum How to use checksums |
00564ffe849110377287ca65bb618ea90dca6825abb65ddb8b43083eeea7008d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for camgrab 0.9.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.