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

Add to_svg #519

Merged
merged 12 commits into from Jan 8, 2020
Merged

Add to_svg #519

merged 12 commits into from Jan 8, 2020

Conversation

jojolebarjos
Copy link
Contributor

This is another tentative at vector file export. I initially did some experiments using freetype-py, but I was able to get the same results using Pillow bindings of FreeType (there are inconsistencies, though).

There are however slight mismatches between rasterized and vector outputs. I also spotted inconsistencies using different browsers for some fonts (e.g. Segoe Script). I added an option (embed_image) to include the output of to_image in the SVG file, which is useful for debugging. I believe most differences are due to subtleties in kerning and ligatures, which is not significant in common fonts.

It may also be useful to consider embedding the font file directly in the SVG file (or only the required subset, ideally). As my personal use case does not require fancy fonts, I did not dig any further on this topic. This means that using the default font will render properly only if Droid Sans Mono is available to the SVG reader.

I have attached two modified examples, which use different fonts for portability.

Also, contour is not implemented.

Last but not least, I want to credit @jnothman and his pull request (in particular, his hack to detect font name/weight/style). As I am not fluent in forks and pull requests, I had to create a new pull request. But I would be glad to learn how to extend an existing pull request!

@jnothman
Copy link

jnothman commented Dec 25, 2019 via email

@jojolebarjos
Copy link
Contributor Author

Sure, here are rasterized versions of the SVG I attached:

  • to_image: constitution
  • to_svg (rasterized with Inkscape):
    constitution svg
  • to_image: parrot_new
  • to_svg (rasterized with Inkscape): parrot_new svg

I also attach an example that use Segoe Script, to highlight differences between browsers:

  • to_image:
    airplane
  • to_svg (rasterized with Chrome):
    airplane svg chrome
  • to_svg (rasterized with Internet Explorer):
    airplane svg ie

@jnothman
Copy link

Your PR has flake8 failures. Please add another commit rectifying these:


[scikit-ci] Executing: flake8
./wordcloud/wordcloud.py:733: [W293] blank line contains whitespace
    
^
./wordcloud/wordcloud.py:802: [W293] blank line contains whitespace
        
^
./wordcloud/wordcloud.py:822: [W293] blank line contains whitespace
            
^
./wordcloud/wordcloud.py:829: [E501] line too long (156 > 120 characters)
            # TODO some browser do not render glyphs the same way (e.g. Segoe Script in Chrome is different, while in Internet Explorer it matches to_image)
                                                                                                                        ^
./wordcloud/wordcloud.py:832: [F841] local variable 'min_y' is assigned to but never used
            min_y = ascent - size_y
            ^

Copy link

@jnothman jnothman left a comment

Choose a reason for hiding this comment

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

Neat!

wordcloud/wordcloud.py Show resolved Hide resolved
wordcloud/wordcloud.py Outdated Show resolved Hide resolved
wordcloud/wordcloud.py Outdated Show resolved Hide resolved
@jnothman
Copy link

I agree the masking of each word is not perfect, but it's decent...

@jojolebarjos
Copy link
Contributor Author

Thank you for your feedback, I really appreciate. I have fixed my code, according to your suggestions. Indeed, I'm not sure whether we can do anything about these differences in SVG renderers.

@jnothman
Copy link

jnothman commented Dec 31, 2019 via email

@jojolebarjos
Copy link
Contributor Author

I have added an option to embed the required font subset inside the SVG. This does solve the missing font problem, for instance when using the default wordcloud font. fonttools is required only if embed_font is true.

@amueller
Copy link
Owner

amueller commented Jan 7, 2020

Hm looks like python34 is failing but looks unrelated. Thanks a lot for the contribution. Good to merge?

@jnothman
Copy link

jnothman commented Jan 7, 2020

would it be worth adding at least a smoke test before merge, if not an image comparison test?

@amueller
Copy link
Owner

amueller commented Jan 7, 2020

Smoke test should be good. So far I have not done any image comparison tests, for better or worse, so I wouldn't ask for that.

@jojolebarjos
Copy link
Contributor Author

I added a small smoke test, which run to_svg and check that it is valid XML.

@amueller amueller merged commit 1fc6868 into amueller:master Jan 8, 2020
@michaelsjackson
Copy link

It seems we (I) can not access this new svg export feature from the command line yet? (wordcloud_cli.py). It would be cool if it could be added there as well.

It could work as follows:

--imagefile /path/image.png   would export as png
--imagefile /path/image.svg   would export as svg

Or anything similar which works in the end.

@amueller
Copy link
Owner

@michaelsjackson I think that would be cool!

@EidrianGM
Copy link

Hi, when will the method to_svg() uploaded to pip ?

@amueller
Copy link
Owner

amueller commented Apr 7, 2020

There's some issues with the build right now: #535
wordcloud right now uses a build system written by @jcfr and I'm not sure what the issue is.
If I don't hear from him, I might redo the whole build process which might be some work. Any help trying to debug this is welcome.

@jcfr
Copy link
Collaborator

jcfr commented Apr 7, 2020

Sorry for the late reply.
I will have a look at #535

@amueller
Copy link
Owner

amueller commented Apr 7, 2020

Thanks!

@thaoth58
Copy link

thaoth58 commented Apr 9, 2020

Thanks, guys!
Waiting for this feature in pip

@michaelsjackson
Copy link

which repository has to_svg, if any?

@amueller
Copy link
Owner

amueller commented May 2, 2020

released as 1.7.0 https://pypi.org/project/wordcloud/

@KonradHoeffner
Copy link

Is SVG export now possible from the command line? If yes, how?

@amueller
Copy link
Owner

amueller commented Jul 9, 2021

@KonradHoeffner looks like it hasn't been added to the CLI yet. PR welcome :)

@KonradHoeffner
Copy link

KonradHoeffner commented Jul 12, 2021

@amueller: I tried to create a PR but I'm not a Python expert and couldn't figure out how to develop your tool quickly, as the README doesn't contain a section about it. Maybe you could add a section about that to the README?

Steps tried

  • fork the project
  • create a virtual environment with venv
  • python -r requirements.txt
  • python -r requirements-dev.txt
(venv) word_cloud$ python wordcloud/wordcloud_cli.py 

To execute the CLI, instead consider running:

  wordcloud_cli --help

or

  python -m wordcloud --help

It seems to me like it denies being called directly and only wants to be called as a production version? Unfortunately I don't have more time right now and I'm not versed enough in Python and the project so I hope someone with more knowledge about both can do that PR.

P.S.: I asked my colleague, who is more experienced in Python, but he also couldn't figure it out how the development workflow is supposed to be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants