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

Make image plugin to attempt to detect SVG types #1440

Merged
merged 2 commits into from
Sep 27, 2018
Merged

Make image plugin to attempt to detect SVG types #1440

merged 2 commits into from
Sep 27, 2018

Conversation

ssjhv
Copy link
Member

@ssjhv ssjhv commented Sep 19, 2018

If known image detection fails, and if the beginning of the data is either XML or SVG header, then set mimetype to image/svg+xml.

If known image detection fails, and if the beginning of the data is either XML or SVG header, then set mimetype to image/svg+xml.
@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

@@ -314,6 +314,10 @@ def _serve_individual_image(self, request):
data = self._get_individual_image(run, tag, index, sample)
image_type = imghdr.what(None, data)
content_type = _IMGHDR_TO_MIMETYPE.get(image_type, _DEFAULT_IMAGE_MIMETYPE)
if content_type == _DEFAULT_IMAGE_MIMETYPE:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change to add SVG support!

Do you mind adapting this logic to integrate with imghdr? The module has a imghdr.tests variable that you can extend to add support for new image types. That way SVG doesn't have to be special-cased as a fallback here. See https://github.com/python/cpython/blob/3.6/Lib/imghdr.py#L32

I believe the code would look like

import imghdr

def detect_svg(data, f):
    if data.startswith(b'<?xml ') or data.startswith(b'<svg '):
        return 'svg'
imghdr.tests.append(detect_svg)

Then you'd just add the appropriate entry to _IMGHDR_TO_MIMETYPE and the code here should work without any changes.

@ssjhv
Copy link
Member Author

ssjhv commented Sep 27, 2018

Thanks! I'll try to see if making changes to imghdr is acceptable.

@googlebot
Copy link

CLAs look good, thanks!

@nfelt
Copy link
Contributor

nfelt commented Sep 27, 2018

@ssjhv Just to be clear, I didn't mean actually changing the imghdr module itself. The module provides a mutable list of tests as the "extension API" where you "register" a new test by appending it to that mutable module-level list. So the code I'd sketched out would just be added to the top of the images plugin file.

Extended imghdr.tests to include svg detection. Removed previous special handling of svg.
Copy link
Contributor

@nfelt nfelt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the update.

@nfelt nfelt merged commit dfe8119 into tensorflow:master Sep 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants