Giter Site home page Giter Site logo

geopatterns's Introduction

GeoPatterns

Generate beautiful SVG patterns from a string. This is a Python-port of Jason Long's Ruby library.

Installation

GeoPatterns is installable via pip:

$ pip install geopatterns

Usage

Create a new pattern by initializing GeoPattern() with a string and a generator (the result of this string/generator pair is the above image).

>>> from geopatterns import GeoPattern
>>> pattern = GeoPattern('A string for your consideration.', generator='xes')

Currently available generators are:

  • hexagons
  • overlapping_circles
  • overlapping_rings
  • plaid
  • plus_signs
  • rings
  • sinewaves
  • squares
  • triangles
  • xes

Get the SVG string:

>>> print(pattern.svg_string)
u'<svg xmlns="http://www.w3.org/2000/svg" ...

Get the Base64-encoded string:

>>> print(pattern.base64_string)
'PHN2ZyB4bWxucz0iaHR0cDov...

In the case of the Base64-encoded string, you can use it in CSS as follows:

body {
  background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz...zdmc+');
}

You can use cairosvg to save the SVG string as a PNG image. First, install cairosvg:

pip install cairosvg

And then run:

>>> import cairosvg
>>> from geopatterns import GeoPattern
>>> pattern = GeoPattern('A string for your consideration.', generator='xes')
>>> cairosvg.svg2png(bytestring=pattern.svg_string, write_to="output.png")

If you just want to visualize the pattern, you can use cairosvg with PIL:

>>> import matplotlib.pyplot as plt
>>> from PIL import Image
>>> import cairosvg
>>> from geopatterns import GeoPattern
>>> pattern = GeoPattern('A string for your consideration.', generator='xes')
>>> png = cairosvg.svg2png(bytestring=pattern.svg_string)
>>> image = Image.open(BytesIO(png))
>>> plt.imshow(image)
>>> plt.show()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.