Closed TheZoker closed 2 years ago
generic documentation generic source (message by IssueLinks)
Hey there @davet2001, mind taking a look at this issue as it has been labeled with an integration (generic
) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)
@TheZoker thanks for raising the issue! Please can you confirm what type of still image it is?
If possible could you view it in a browser and save a copy to the ticket?
Many thanks!
Thanks for the quick answer :)
Sure thing, here is the image:
I'm not sure if GitHub changes anything about the image during upload
It would be good to zip it up before attaching to make sure GitHub didn't modify it
Looks like there is a bug in the underlying imghdr library meaning it can't recognise some jpeg types.
https://github.com/lektor/lektor/issues/624 also some info here: https://stackoverflow.com/questions/36870661/imghdr-python-cant-detec-type-of-some-images-image-extension
I tried out this code:
def get_image_type(image):
"""Get the format of downloaded bytes that could be an image."""
fmt = imghdr.what(None, h=image)
if fmt is None:
# if imghdr can't figure it out, could be svg.
with contextlib.suppress(UnicodeDecodeError):
if image.decode("utf-8").lstrip().startswith("<svg"):
return "svg+xml"
# JPEG data in JFIF format
if b'JFIF' in image[:23]:
return 'jpeg1'
JPEG_MARK = b'\xff\xd8\xff\xdb\x00C\x00\x08\x06\x06' \
b'\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f'
# JPEG with small header
if len(image) >= 32 and 67 == image[5] and image[:32] == JPEG_MARK:
return 'jpeg2'
# JPEG data in JFIF or Exif format"""
if image[6:10] in (b'JFIF', b'Exif'):
return 'jpeg3'
if image[:2] == b'\xff\xd8':
return 'jpeg4'
return fmt
And on your sample image it returned jpeg4
, which means imghdr.what()
could not work out the image format, but it started with two characters that are a valid jpeg.
Will have another look again this evening.
imghdr is deprecated as per PEP 594, so there won't be further enhancements to it.
source: https://bugs.python.org/issue28591#msg415044
as per https://peps.python.org/pep-0594/#deprecated-modules following replacements are suggested:
@mib1185 Thanks for the suggestion. Yes, it seems imghdr is not a reliable library.
I have also been testing using PIL which seems pretty reliable:
fmt = imghdr.what(None, h=image)
if fmt is None:
imagefile = io.BytesIO(image)
with contextlib.suppress(PIL.UnidentifiedImageError):
img = PIL.Image.open(imagefile)
fmt = img.format.lower()
if fmt is None:
# if imghdr can't figure it out, could be svg.
with contextlib.suppress(UnicodeDecodeError):
if image.decode("utf-8").lstrip().startswith("<svg"):
return "svg+xml"
The above also works but will do a bit more research before settling on a preferred option.
If you have a custom component I can test, let me know :)
Duplicate of #69011
If you have a custom component I can test, let me know :)
@TheZoker If you can test the branch that would be perfect! Se above link for details.
The problem
Since update to the latest beta 2022.04b0 the generic camera is not available as yaml config anymore. So I tried to use the new integration. The still image url for my dafang hacked camera looks like this:
https://<ip-adress>/cgi-bin/currentpic.cgi
This worked very well in HA in the past. But since the change, HA tells me, that the still image is not valid and I can't add it to the integration config.
What version of Home Assistant Core has the issue?
core-2022.4.0b0
What was the last working version of Home Assistant Core?
core-2022.3.8
What type of installation are you running?
Home Assistant OS
Integration causing the issue
generic
Link to integration documentation on our website
https://www.home-assistant.io/integrations/generic
Diagnostics information
No response
Example YAML snippet
Anything in the logs that might be useful for us?
Additional information
No response