Python | Serpino carpet

| | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

How does it work?
The Sierpinski carpet begins with a square. This square is divided into nine equal parts. The smallest smaller square is removed from the original larger square. Then the remaining squares are again divided into nine equal parts, and the most central square from each square is removed. When this process is repeated, a beautiful Sierpinski carpet pattern is observed.

Suppose we start with a black square.

Divide it into 9 equal parts and remove the central square.

Repeating the process further leads to something like this.

We can visualize this phenomenon in detail in this video .

Let’s see how its code looks like:

# import required modules

import numpy as np

from PIL import Image


# total number of process repetitions

total = 7


# image size

size = 3 * * total


# create image

square = np.empty ([size, size, 3 ], dtype = np.uint8)

color = np.array ([ 255 , 255 , 255 ], dtype = np.uint8)


# filling this with black

square.fill ( 0 )

for i in range ( 0 , total + 1 ):

stepdown = 3 * * (total - i)

for x in range ( 0 , 3 * * i):

# checking the central square

if x % 3 = = 1 :

for y in range ( 0 , 3 * * i):

if y % 3 = = 1 :

# change your color

square [y * stepdown: (y + 1 ) * stepdown, x * stepdown: (x + 1 ) * stepdown] = color


# save the resulting image

save_file = "sierpinski.jpg"

Image.fromarray (square) .save (save_file)


# display it in the console

i = Image. open ( " sierpinski.jpg " )

i.show ()

Output:

This is a Sierpinski carpet after 7 repetitions. You can get its code for other languages ‚Äã‚Äãat rosettacode .

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Python | Serpino carpet, check other File handling-related topics.

Want to excel in Python? See our review of the best Python online courses 2022. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Carlo Chamberlet

London | 2022-11-30

Thanks for explaining! I was stuck with Python | Serpino carpet for some hours, finally got it done 🤗. Will use it in my bachelor thesis

Anna Nickolson

Singapore | 2022-11-30

I was preparing for my coding interview, thanks for clarifying this - Python | Serpino carpet in Python is not the simplest one. I am just not quite sure it is the best method

Dmitry Wu

Texas | 2022-11-30

Maybe there are another answers? What Python | Serpino carpet exactly means?. Will use it in my bachelor thesis

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically