Skip to content

Commit

Permalink
Include a pixels-to-rectangles example
Browse files Browse the repository at this point in the history
  • Loading branch information
spakin committed Jan 21, 2024
1 parent d774390 commit 9784376
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ List of examples
| [New layer](new_layer.py) | Add a layer to an image. |
| [Overlapping circles](overlapping_circles.py) | Draw an array of hundreds of overlapping circles. |
| [Path effects](path_effects.py) | Demonstrate the application of live path effects to a curve. |
| [Pixels to rectangles](pixels_to_rectangles.py) | Show how to use the Python Imaging Library to convert each pixel of a bitmapped image to a colored rectangle. |
| [Poly by angle](poly_by_angle.py) | Draw (possibly self-intersecting) polygons. |
| [Screen vs print](screen_vs_print.py) | Create a printable page that is differently sized and disjoint from the main canvas. |
| [Sierpinski triangle](sierpinski_triangle.py) | Draw a colorful Sierpinski triangle. |
Expand Down
48 changes: 48 additions & 0 deletions examples/pixels_to_rectangles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
###########################################################
# Show how to use the Python Imaging Library to convert #
# each pixel of a bitmapped image to a colored rectangle. #
###########################################################

import base64
from PIL import Image
import io


def render_with_rectangles(obj):
'Render an image with SVG rectangles.'
# Convert the SVG image to an RGBA PIL image by parsing the raw data.
img_data_b64 = obj.svg_get('xlink:href', True).partition(',')[2]
img_data = base64.b64decode(img_data_b64)
img = Image.open(io.BytesIO(img_data)).convert(mode='RGBA')

# Acquire image properties.
pix_x0, pix_y0 = obj.svg_get('x'), obj.svg_get('y')
pix_wd = inkex.units.convert_unit(obj.svg_get('width'), 'px')
pix_ht = inkex.units.convert_unit(obj.svg_get('height'), 'px')
iwd, iht = img.width, img.height
xedge, yedge = pix_wd/iwd, pix_ht/iht
xform = obj.transform

# Convert each pixel to an SVG rectangle.
rectangles = []
for y in range(iht):
for x in range(iwd):
red, grn, blu, alp = img.getpixel((x, y))
x0, y0 = x*xedge + pix_x0, y*yedge + pix_y0
r = rect((x0, y0), (x0 + xedge, y0 + yedge),
fill='#%02x%02x%02x' % (red, grn, blu),
stroke='none',
opacity=alp/255,
transform=xform)
rectangles.append(r)
return rectangles


# Download a bitmapped image from the web.
img_src = 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c0/' + \
'Gnome-emblem-web.svg/48px-Gnome-emblem-web.svg.png'
img = image(img_src, (20, 20), embed=True, transform='scale(2)')

# Convert each pixel of the image to an SVG rectangle.
group(render_with_rectangles(img))
img.remove()

0 comments on commit 9784376

Please sign in to comment.