Python-Imaging pakedi Python ile resim dosyaları üzerinde çalışmak için gereken fonksiyonları içerir.
| PyPI: | http://pypi.python.org/pypi/PIL |
|---|---|
| Kurulum: | pip install pil |
| Dökümantasyon: | http://effbot.org/imagingbook/ |
image = (
width,
height,
[(r0, g0, b0),
(r1, g1, b1),
(r2, g2, b2),
# snip
(rn, gn, bn)]
)
Yukarıdaki veri yapısında 3 kanallı (yeşil, kırmızı, mavi) gerçek renkli bir resmi saklayabiliriz.
image = (
width,
height,
[v0,
v1,
v2,
# snip
vn]
)
Tek kanallı (siyah-beyaz) resimler için.
from PIL import Image
image_instance = Image.new('RGB', (640, 480), (0, 0, 0))
# some awesome image manipulation code here
image_instance.save('filename.png')
Bir yükseklik haritasını girdi olarak kullanarak üzerine eşyükselti eğrileri yerleştireceğiz.

import random
from PIL import Image, ImageOps
def create_noise(width, height, scale, temporary_border=2):
raise NotImplementedError
def create_noise(width, height, scale, temporary_border=2):
size = (width + 2 * temporary_border,
height + 2 * temporary_border)
img = Image.new('L', size)
# super-cool noise creation happens
img = img.resize((size[0] * scale, size[1] * scale),
Image.BICUBIC)
img = ImageOps.crop(img, scale * temporary_border)
return img
img resmi üzerindeki her bir piksele rastgele bir değer atıyoruz.
img_cursor = img.load()
for y in range(size[1]):
for x in range(size[0]):
img_cursor[x, y] = random.randint(0, 255)
def create_noise(width, height, scale, temporary_border=2):
size = (width + 2 * temporary_border,
height + 2 * temporary_border)
img = Image.new('L', size)
img_cursor = img.load()
for y in range(size[1]):
for x in range(size[0]):
img_cursor[x, y] = random.randint(0, 255)
img = img.resize((size[0] * scale, size[1] * scale),
Image.BICUBIC)
img = img.filter(ImageFilter.SMOOTH)
img = ImageOps.crop(img, scale * temporary_border)
return img
Bir resim ince detaylar için...

noise_1 = create_noise(128, 128, 2)
...bir resim daha büyük detaylar için...

noise_2 = create_noise(32, 32, 8)
...bir resim de en kaba detaylar için.

noise_3 = create_noise(8, 8, 32)
Üçünü birleştirdiğimizde doğal bir desen elde ederiz:

noise_combined = ImageChops.blend(noise_1, noise_2, 0.667)
noise_combined = ImageChops.blend(noise_combined, noise_3, 0.667)
noise_combined.save('noise_combined.png')
def naive_perlin(width, height, n, c=1):
img = Image.new('L', (width, height), 127)
for i in range(c, n+c):
f = 2 ** i
noise = create_noise(width/f, height/f, f)
img = ImageChops.blend(img, noise, 0.333)
img = ImageOps.autocontrast(img)
return img
Yükseklik haritası için kullanacağımız gürültü resmi:

WIDTH = HEIGHT = 256
random.seed(369147258)
hm_img = naive_perlin(WIDTH, HEIGHT, 5, 4)
Yükseklik haritamızdaki her pikselin değeri belli bir yüksekliğe işaret ediyor.
Aynı yükseklikteki noktaları birleştiren eğrilere eşyükselti eğrileri denir.
def draw_isometric_curve(img, value, threshold=1.55):
def _curve_func(p_value):
return max(threshold - abs(p_value - value), 0) * 255
# make sure we have float and offset a little further
threshold += 0.05
curve_img = Image.eval(img.copy(), _curve_func)
curve_img = curve_img.filter(ImageFilter.SMOOTH_MORE)
curve_img = ImageOps.autocontrast(curve_img)
return curve_img

contours_example = draw_isometric_curve(noise_img, 127)
def draw_contours(img, count=8):
value = 256 / count
contours = [draw_isometric_curve(hm_img, c*value) \
for c in range(count)]
return reduce(ImageChops.add, contours)
contour_img = draw_contours(hm_img)
result = ImageChops.screen(ImageOps.colorize(hm_img,
(16, 8, 256),
(255, 64, 196)),
ImageOps.colorize(contour_img,
(0, 0, 0),
(64, 256, 96)))
import random
from PIL import Image, ImageFilter, ImageDraw
def square_brush(img, count, size_min=4, size_max=32):
raise NotImplementedError
random.seed(12345)
img = Image.open('tesla.jpeg')
squares_coarse = square_brush(img, 3000, 5, 25)
squares_coarse.save('tesla_squares_coarse.png')
def square_brush(img, count, size_min=4, size_max=32):
output = Image.new('RGB', img.size, (0, 0, 0))
img_cursor = img.filter(ImageFilter.MedianFilter(9)).load()
draw = ImageDraw.Draw(output)
for _ in range(count):
searching = 10
while searching:
x = random.randint(0, img.size[0] - 1)
y = random.randint(0, img.size[1] - 1)
if output.getpixel((x, y)) == (0, 0, 0):
searching = 0
else:
searching -= 1
box_size = random.randint(size_min, size_max) / 2
box = (x-box_size, y-box_size, x+box_size, y+box_size)
draw.rectangle(box, fill=img_cursor[x, y])
return output
def square_brush(img, count, size_min=4, size_max=32):
output = Image.new('RGB', img.size, (0, 0, 0))
img_cursor = img.filter(ImageFilter.MedianFilter(9)).load()
# classy yet sexy brush strokes
return output
draw = ImageDraw.Draw(output)
for _ in range(count):
searching = 10
## start of modified code ##
# Naive implementation
x = random.randint(0, img.size[0] - 1)
y = random.randint(0, img.size[1] - 1)
## end of modified code ##
box_size = random.randint(size_min, size_max) / 2
box = (x-box_size, y-box_size, x+box_size, y+box_size)
draw.rectangle(box, fill=img_cursor[x, y])
searching = 10
while searching:
x = random.randint(0, img.size[0] - 1)
y = random.randint(0, img.size[1] - 1)
if output.getpixel((x, y)) == (0, 0, 0):
searching = 0
else:
searching -= 1
squares_coarse = square_brush(img, 3000, 5, 25)
squares_fine = square_brush(img, 9000, 3, 15)
Teşekkür ederim.