Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Add support for png_text metadata, allow to customize metadata for other backends. #7349
Conversation
|
Instead of replacing the entire metadata, I'd have the argument update the existing dictionary that was originally generated in the code. That way, the user doesn't need to figure out the default strings (version info and such), but can still provide other metadata or override it if really wanted. |
QuLogic
added this to the
2.1 (next point release)
milestone
Oct 25, 2016
QuLogic
added backend/AGG backend/ps backend/pdf
labels
Oct 25, 2016
tacaswell
reviewed
Oct 25, 2016
Please update the existing meta-data rather than completely replacing it with the user supplied metadata.
| @@ -552,8 +552,16 @@ def print_png(self, filename_or_obj, *args, **kwargs): | ||
| else: | ||
| close = False | ||
| + version_str = 'matplotlib version ' + __version__ + \ | ||
| + ', http://matplotlib.org/' | ||
| + metadata = {six.b('Software'): six.b(version_str)} |
QuLogic
Oct 27, 2016
Member
This would be confusing for users, as they would probably provide regular strings (as that's what all the other ones accept.) If bytestrings are really required here, it should do the encoding after obtaining the user's metadata.
| @@ -469,10 +469,13 @@ def __init__(self, filename): | ||
| revision = '' | ||
| self.infoDict = { | ||
| - 'Creator': 'matplotlib %s, http://matplotlib.org' % __version__, | ||
| + 'Creator': 'matplotlib ' + __version__ + | ||
| + ', http://matplotlib.org', |
| +#ifdef PNG_TEXT_SUPPORTED | ||
| + // Save the metadata | ||
| + if (metadata != NULL) | ||
| + { |
| + // Save the metadata | ||
| + if (metadata != NULL) | ||
| + { | ||
| + meta_size = PyDict_Size(metadata); |
tacaswell
dismissed
their
review
Oct 27, 2016
Concern has been addressed.
| - text[meta_pos].key = PyBytes_AsString(meta_key); | ||
| - text[meta_pos].text = PyBytes_AsString(meta_val); | ||
| + if (PyUnicode_Check(meta_key)) { | ||
| + PyObject *temp_key = PyUnicode_AsEncodedString(meta_key, "ASCII", "strict"); |
| @@ -2423,8 +2425,11 @@ def __init__(self, filename, keep_empty=True): | ||
| keep_empty: bool, optional | ||
| If set to False, then empty pdf files will be deleted automatically | ||
| when closed. | ||
| + metadata: dictionary, optional | ||
| + Information dictionary object (see PDF reference section 10.2.1 |
NelleV
Oct 29, 2016
Contributor
I don't find this documentation particularly useful. You could you add at least some examples of metadata ?
Also, numpydoc format requires a space before the column : metadata : dictionary, …
QuLogic
Oct 29, 2016
Member
The original parameters all don't have a space before the colon, and rendered fine, so I don't think that's a requirement.
| @@ -1073,13 +1073,19 @@ def write(self, *kl, **kwargs): | ||
| self.figure.set_facecolor(origfacecolor) | ||
| self.figure.set_edgecolor(origedgecolor) | ||
| + # check for custom metadata | ||
| + metadata = kwargs.pop("metadata", None) |
tacaswell
Oct 31, 2016
Owner
err, sorry, there were two of these and I grew confused reading the diffs.
|
Can you add |
|
Sorry in advance if I am asking obvious questions, just trying to understand the libpng API. From http://www.libpng.org/pub/png/libpng-manual.txt
I can not quite sort out the difference between This is valuable work thank you again for taking this on! |
AFAICT, it's totally ignored. There's an explicit
I'm not sure how to interpret the standard either. In libpng there's explicit:
in the loop over
It's checked internally. If the key exceeds 79 it is truncated and warning is issued:
|
|
It makes sense that it takes care of the length it's self (so we can not lie to it, that seems like an obvious attack vector). I am a tad concerned about the Might be worth turning that warning into a python warning? I am |
|
ping, is there anything else left to do before this PR is merged? |
|
This looks like a good contribution, but it would be nice to have @mdboom look at the agg part and @jkseppan look at the pdf and pd parts. As @tacaswell noted, it will need a whats-new entry. I have a question about the API and behavior: it looks like the present scheme, in which metadata comes in as a dictionary, has the undesirable side effect of making the order in which the metadata appear in the file non-deterministic (and at the very least, not under the control of the user). This could be avoided by using an OrderedDict, or by using a sequence of (key, value) tuples. Correct? |
Sorry, it wasn't clear who's supposed to add it. I'll work on it.
In principle you are right. However, since there are no methods to read text value by its index why would it matter? I would say that random order is desirable side effect. It will prevent people from writing software that makes ill assumptions about metadata. |
|
On the bright side, with python 3.6 dictionary order is deterministic again! |
|
I know zilch about metadata in image files, but I am guessing that utilities dump the metadata in the order in which it is found, e.g. for display to the screen, in which case it would be nice to control the order--wouldn't it? Regarding deterministic vs non-deterministic, see #6317 for a discussion. |
codecov-io
commented
Nov 20, 2016
•
Current coverage is 62.07% (diff: 81.81%)@@ master #7349 diff @@
==========================================
Files 174 174
Lines 56007 56021 +14
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 34765 34773 +8
- Misses 21242 21248 +6
Partials 0 0
|
tacaswell
closed this
Nov 20, 2016
tacaswell
reopened this
Nov 20, 2016
|
'power cycled' to restart against current master |
NelleV
changed the title from
Add support for png_text metadata, allow to customize metadata for other backends. to [MRG+1] Add support for png_text metadata, allow to customize metadata for other backends.
Dec 19, 2016
|
Hi @Xarthisius |
Xarthisius
added some commits
Oct 25, 2016
tacaswell
merged commit ab98852
into matplotlib:master
Dec 25, 2016
5 checks passed
|
@Xarthisius Thanks! Sorry this took so long to get merged. |
Xarthisius commentedOct 25, 2016
PNG Specification allows for custom
png_textstructures as an image attributes. The keywords that are given in the PNG specs are:This PR allows to pass a dictionary with a metadata that can be used to fill those key/value pairs in png writer. Structure of the data and its type is not validated.
Additionally this PR allows to customize "software/creator" values in other backends such as PDF and PS.