{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Masked wordcloud\n\nUsing a mask you can generate wordclouds in arbitrary shapes.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from os import path\nfrom PIL import Image\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport os\n\nfrom wordcloud import WordCloud, STOPWORDS\n\n# get data directory (using getcwd() is needed to support running example in generated IPython notebook)\nd = path.dirname(__file__) if \"__file__\" in locals() else os.getcwd()\n\n# Read the whole text.\ntext = open(path.join(d, 'alice.txt')).read()\n\n# read the mask image\n# taken from\n# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg\nalice_mask = np.array(Image.open(path.join(d, \"alice_mask.png\")))\n\nstopwords = set(STOPWORDS)\nstopwords.add(\"said\")\n\nwc = WordCloud(background_color=\"white\", max_words=2000, mask=alice_mask,\n               stopwords=stopwords, contour_width=3, contour_color='steelblue')\n\n# generate word cloud\nwc.generate(text)\n\n# store to file\nwc.to_file(path.join(d, \"alice.png\"))\n\n# show\nplt.imshow(wc, interpolation='bilinear')\nplt.axis(\"off\")\nplt.figure()\nplt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')\nplt.axis(\"off\")\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.13.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}