Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

What is gizeh in python?

3 Answer(s) Available
Answer # 1 #

Gizeh is a Python library for vector graphics:

You can see examples of Gizeh in action (combined with MoviePy to make animations) in this blog post.

Gizeh is written on top of the module cairocffi, which is a Python binding of the popular C library Cairo. Cairo is powerful, but difficult to learn and use. Gizeh implements a few classes on top of Cairo that make it more intuitive.

Gizeh should work on any platform and with python 2 and 3.

To use Gizeh you must first install Cairo on your computer (see their website).

Gizeh depends on the Python packages cairocffi and Numpy. They will both be automatically installed (if they aren't already) during the installation of Gizeh. If you have trouble with the installation, head to the last section of this README for troubleshooting. If it doesn't help, you can ask for help on Github.

Installation from the sources: Gizeh can be installed by unzipping the source code in some directory and using this command in the same directory:

Installation with pip: Alternatively, you can install Gizeh directly from the Python Package Index with this command:

This method may fail if ez_setup is not installed on your computer. In this case install ez_setup first, with

Gizeh is an open-source software written by Zulko and released under the MIT licence. The project is hosted on Github. Everyone is welcome to contribute !

This guide, along with the examples in the gizeh/examples folder, should give you everything you need to get started. To go further, read the function docstrings.

A Surface is a rectangle of fixed dimensions (in pixels), on which you will draw elements, and that you can save or export as an image:

Basic elements are circles, rectangles, lines, texts, etc., that you can draw on a surface using my_element.draw(surface). You can specify the properties and coordinates of these elements at creation time:

Examples of elements:

When you make a shape, the fill and stroke parameters can be one of the following:

Any element can be transformed (translated, rotated or scaled). All transformations are outplace: they do not modify the original element, they create a modified version of it.

Examples:

A Group is a collection of elements which will be transformed and drawn together. The elements can be a basic element (square, circle...) or even groups.

Examples:

That's about all there is to know. To go further, see the examples in the examples folder or the documentation directly in the code.

Sometimes the installation through pip fails because

Some people have had problems to install cairocffi, Here is how they solved their problem:

On Debian/Ubuntu

[4]
Edit
Query
Report
Sadhu Jahnvi
INSPECTOR I
Answer # 2 #

Gizeh is a Python library for vector graphics: # Let's draw a red circle ! import gizeh surface = gizeh. Surface(width=320, height=260) # in pixels circle = gizeh. circle(r=30, xy=[40,40], fill=(1,0,0)) circle.

[3]
Edit
Query
Report
Nandi Butterell
Novelist
Answer # 3 #

It was generated using a special animation language called Processing (here is Dave’s code). While it seems powerful, Processing it is not very elegant in my opinion ; this post shows how to do similar animations using two Python libraries, Gizeh (for the graphics) and MoviePy (for the animations).

Gizeh is a Python library I wrote on top of cairocffi ( a binding of the popular Cairo library) to make it more intuitive. To make a picture with Gizeh you create a surface, draw on it, and export it:

We obtain this magnificent Japanese flag:

To make an animation with MoviePy, you write a function make_frame which, given some time t, returns the video frame at time t:

We start with an easy one. In make_frame we just draw a red circle, whose radius depends on the time t:

Now there are more circles, and we start to see the interest of making animations programmatically using for loops. The useful function polar2cart transforms polar coordinates (radius, angle) into cartesian coordinates (x,y).

Here we fill the circles with a slightly excentred radial gradient to give and impression of volume. The colors, initial positions and centers of rotations of the circles are chosen randomly at the beginning.

The shadow is done using a circle with radial fading black gradient whose intensity diminishes when the ball is higher, for more realism (?). The shadow is then squeezed vertically using scale(r,r/2), so that its width is twice its height.

This is a derivative of the Dave Whyte animation shown in the introduction. It is made of stacked circles moving towards the picture’s border, with carefully chosen sizes, starting times, and colors (I say carefully chosen because it took me a few dozens random tries). The black around the picture is simply a big circle with no fill and a very very thick black border.

You can draw more than circles ! And you can group different elements so that they will move together (here, a letter and a pentagon).

We start with just a triangle. By rotating this triangle three time we obtain four triangles which fit nicely into a square. Then we copy this square following a checkerboard pattern. Finally we do the same with another color to fill the missing tiles. Now, if the original triangle is rotated, all the triangles on the picture will also be rotated.

A nice thing to do with vector graphics is fractals. We first build a ying-yang, then we use this ying-yang as the dots of a bigger ying-yang, and we use the bigger ying-yang as the dots of an even bigger ying yang etc. In the end we go one level deep into the imbricated ying-yangs, and we start zooming.

That one is inspired by this Dave Whyte animation. We draw white-filled circles, each of these being almost completely transparent so that they only add 1 to the value of the pixels that they cover. Pixels with an even value, which are the pixels covered by an even number of circles, are then painted white, while the others will be black. To complexify and have a nicely-looping animation, we draw two circles in each direction, one being a time-shifted version of the other.

A pentagon made of rotating squares ! Interestingly, making the squares rotate the other direction creates a very different-looking animation. The squares are placed according to this polar equation.

The difficulty in this animation is that the last square drawn will necessarily be on top of all the others, and not, as it should be, below the first square ! The solution is to draw each frame twice. The first time, we draw the squares starting from the right, so that the faulty square will also be on the right, and we only keep the left part of that picture. The second time we start drawing the squares from the left, so that the faulty square is on the left, and we keep the right part. By assembling the two valid parts we reconstitute a valid picture.

A nice advantage of combining Gizeh with MoviePy is that you can read actual video files (or gifs) and use the frames to fill shapes drawn with Gizeh.

We will use this video from the Blender Foundation (it’s under a Creative Common licence). Since you have read until there I’ll show you a little unrelated trick: at 4:32 the rabbit is jumping rope, so there is a potential for a well-looping GIF. We open the video around 4:32, and let MoviePy automatically decide where to cut to have the best-looping GIF possible:

Now we can feed the frames of this GIF to Gizeh, using MoviePy’s clip.fl(some_filter), which means “I want a new clip made by transforming the frames of the current clip with some_filter”.

Finally, this function adds a zoom on some part of the video.

[0]
Edit
Query
Report
Hodson Arpita
STAMPER I