Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85171 > unrolled thread
| Started by | Poul Riis <priisdk@gmail.com> |
|---|---|
| First post | 2015-02-03 13:00 -0800 |
| Last post | 2015-02-04 08:58 +0100 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.python
Cairo module Poul Riis <priisdk@gmail.com> - 2015-02-03 13:00 -0800
Re: Cairo module Travis Griggs <travisgriggs@gmail.com> - 2015-02-03 14:19 -0800
Re: Cairo module Poul Riis <priisdk@gmail.com> - 2015-02-04 13:21 -0800
Re: Cairo module Cousin Stanley <cousinstanley@gmail.com> - 2015-02-04 15:56 -0700
Re: Cairo module Cousin Stanley <cousinstanley@gmail.com> - 2015-02-04 16:00 -0700
Re: Cairo module Michiel Overtoom <motoom@xs4all.nl> - 2015-02-04 08:58 +0100
| From | Poul Riis <priisdk@gmail.com> |
|---|---|
| Date | 2015-02-03 13:00 -0800 |
| Subject | Cairo module |
| Message-ID | <361dc1c7-a26c-40de-8b60-910f25bdabd9@googlegroups.com> |
I just tried the Cairo Python module.
I ran the test file below.
It works perfectly but instead of saving the resulting image as a file I want to see it displayed directly on the screen.
How can I do that?
Poul Riis
import math
import cairo
WIDTH, HEIGHT = 256, 256
surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context (surface)
ctx.scale (WIDTH, HEIGHT) # Normalizing the canvas
pat = cairo.LinearGradient (0.0, 0.0, 0.0, 1.0)
pat.add_color_stop_rgba (1, 0.7, 0, 0, 0.5) # First stop, 50% opacity
pat.add_color_stop_rgba (0, 0.9, 0.7, 0.2, 1) # Last stop, 100% opacity
ctx.rectangle (0, 0, 1, 1) # Rectangle(x0, y0, x1, y1)
ctx.set_source (pat)
ctx.fill ()
ctx.translate (0.1, 0.1) # Changing the current transformation matrix
ctx.move_to (0, 0)
ctx.arc (0.2, 0.1, 0.1, -math.pi/2, 0) # Arc(cx, cy, radius, start_angle, stop_angle)
ctx.line_to (0.5, 0.1) # Line to (x,y)
ctx.curve_to (0.5, 0.2, 0.5, 0.4, 0.2, 0.8) # Curve(x1, y1, x2, y2, x3, y3)
ctx.close_path ()
ctx.set_source_rgb (0.3, 0.2, 0.5) # Solid color
ctx.set_line_width (0.02)
ctx.stroke ()
surface.write_to_png ("example.png") # Output to PNG
[toc] | [next] | [standalone]
| From | Travis Griggs <travisgriggs@gmail.com> |
|---|---|
| Date | 2015-02-03 14:19 -0800 |
| Message-ID | <mailman.18445.1423001978.18130.python-list@python.org> |
| In reply to | #85171 |
> On Feb 3, 2015, at 1:00 PM, Poul Riis <priisdk@gmail.com> wrote:
>
> I just tried the Cairo Python module.
> I ran the test file below.
> It works perfectly but instead of saving the resulting image as a file I want to see it displayed directly on the screen.
> How can I do that?
>
I have quiet a bit of experience with Cairo (I wrote language bindings for it in Smalltalk and had the time of my life with it there); I have no experience with the pycairo bindings.
>
> import math
> import cairo
>
> WIDTH, HEIGHT = 256, 256
>
> surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
This is your basic problem right here. And ImageSurface is for creating an Image (sometimes called a raster graphic or bitmap). If you want to display directly to your screen, you need to create a surface that binds to your screen’s display functionality. There is one for each main operating system:
Win32Surface
XLibSurface
QuartzSurface (I see that this is missing from the pycairo documentation, but it is in the cairo documentation, and the pycairo.c file at least has some reference to it)
Allocating one of these involves getting handles (and other information) for a given window on screen of your host OS and creating the surface from it.
> ctx = cairo.Context (surface)
>
> ctx.scale (WIDTH, HEIGHT) # Normalizing the canvas
>
> pat = cairo.LinearGradient (0.0, 0.0, 0.0, 1.0)
> pat.add_color_stop_rgba (1, 0.7, 0, 0, 0.5) # First stop, 50% opacity
> pat.add_color_stop_rgba (0, 0.9, 0.7, 0.2, 1) # Last stop, 100% opacity
>
> ctx.rectangle (0, 0, 1, 1) # Rectangle(x0, y0, x1, y1)
> ctx.set_source (pat)
> ctx.fill ()
>
> ctx.translate (0.1, 0.1) # Changing the current transformation matrix
>
> ctx.move_to (0, 0)
> ctx.arc (0.2, 0.1, 0.1, -math.pi/2, 0) # Arc(cx, cy, radius, start_angle, stop_angle)
> ctx.line_to (0.5, 0.1) # Line to (x,y)
> ctx.curve_to (0.5, 0.2, 0.5, 0.4, 0.2, 0.8) # Curve(x1, y1, x2, y2, x3, y3)
> ctx.close_path ()
>
> ctx.set_source_rgb (0.3, 0.2, 0.5) # Solid color
> ctx.set_line_width (0.02)
> ctx.stroke ()
>
> surface.write_to_png ("example.png") # Output to PNG
> --
> https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Poul Riis <priisdk@gmail.com> |
|---|---|
| Date | 2015-02-04 13:21 -0800 |
| Message-ID | <95cd13d2-bd26-4c65-8f42-461d4a5dae6e@googlegroups.com> |
| In reply to | #85179 |
Could you be a little more specific (giving, for instance, a full working example)? I tried to interchange surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT) with surface = cairo.Win32Surface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT) but that didn't seem to work. Could matplotlib be used to show the image? Poul Riis
[toc] | [prev] | [next] | [standalone]
| From | Cousin Stanley <cousinstanley@gmail.com> |
|---|---|
| Date | 2015-02-04 15:56 -0700 |
| Message-ID | <mau816$gke$1@dont-email.me> |
| In reply to | #85234 |
> .... > Could matplotlib be used to show the image? You might consider using python-imaging to display the image after writing it from cairo .... import image .... surface.write_to_png ( "x_surface.png" ) img = Image.open( "x_surface.png" ) img.show( command = 'display' ) -- Stanley C. Kitching Human Being Phoenix, Arizona
[toc] | [prev] | [next] | [standalone]
| From | Cousin Stanley <cousinstanley@gmail.com> |
|---|---|
| Date | 2015-02-04 16:00 -0700 |
| Message-ID | <mau88u$gke$2@dont-email.me> |
| In reply to | #85237 |
> You might consider using python-imaging > to display the image after writing it > from cairo .... > > import image import statement should be .... import Image note uppercase I .... -- Stanley C. Kitching Human Being Phoenix, Arizona
[toc] | [prev] | [next] | [standalone]
| From | Michiel Overtoom <motoom@xs4all.nl> |
|---|---|
| Date | 2015-02-04 08:58 +0100 |
| Message-ID | <mailman.18454.1423036760.18130.python-list@python.org> |
| In reply to | #85171 |
Hi Poul, I recently used cairo in a python project (https://github.com/luismqueral/jumpcityrecords). To see the cairo drawing directly on the screen I wrote a minimal Gtk application. It's in the 'src' directory and is called 'randomdraw.py'. Maybe it is of some help to you. Greetings, -- "You can't actually make computers run faster, you can only make them do less." - RiderOfGiraffes
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web