Create a Colorful Random Pixel Art on a Black Canvas with Python
This tutorial shows how to use Python's Pillow library to generate a 512 × 512 black canvas and randomly color 3 % of its pixels with one of seven preset colors, then save and display the resulting vibrant image, detailing installation, palette definition, pixel iteration, probability selection, and PNG output.
Today we will create a simple exercise: randomly fill sparse colorful pixels on a black background.
1. Main Process
Create a 512 × 512 canvas with an opaque black background, then randomly select pixels with a certain probability and assign each a color chosen from seven preset colors (red, orange, yellow, green, cyan, blue, purple).
2. Install required Python library
First install Python and add the python and pip commands to the environment path.
pip install pillow3. Create RGBA image canvas
from PIL import Image
width = 512
height = 512
# Image size 512x512
img = Image.new('RGBA', (width, height), (0, 0, 0, 255))4. Randomly color 3% of the pixels
# Set selection probability to 0.03 (3%)
percent = 0.03
# Iterate over all pixels
for i in range(512):
for j in range(512):
# random.random() returns a uniform value between 0 and 1
if random.random() <= percent:
# Choose a random color from the predefined list
rgba = random.choice(colors)
# Set pixel color
img.putpixel((j, i), rgba)5. Save the image
img.save('c.png', 'PNG')
print("哈哈, 五彩斑斓的黑大功告成!")
img.show()Result
Final image:
Full code
#!/bin/env python
# coding: utf-8
# author: ZhangTao
# Date : 2019/12/12
# 五彩斑斓的黑
from PIL import Image
import random
width = 512
height = 512
# 图片大小为 512x512
img = Image.new('RGBA', (width, height), (0, 0, 0, 255))
# 预设7中颜色,后面随机生成像素点颜色要用到
colors = [
# 赤
(255, 0, 0, 255),
# 橙
(255, 128, 0, 255),
# 黄
(255, 255, 0, 255),
# 绿
(0, 255, 0, 255),
# 青
(0, 255, 255, 255),
# 蓝
(0, 0, 255, 255),
# 紫
(128, 0, 255, 255)
]
# 设定挑选的点概率 0.01 就是1%
percent = 0.01
# 遍历 512x512 图像的所有像素点
for i in range(512):
for j in range(512):
# 因为 random.random() 产生的随机数是 0到 1 之间均匀分布的
# 就直接用 random.random()产生随机值是 0 到 percent之间的就改变颜色
if random.random() <= percent:
# 从预设的colors颜色列表中随机挑选一个颜色
rgba = random.choice(colors)
# 设定坐标颜色
img.putpixel((j, i), rgba)
img.save('c.png', 'PNG')
print("哈哈, 五彩斑斓的黑大功告成!")
img.show()Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
