mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-15 10:42:19 +03:00
Added pythonic demos
This commit is contained in:
parent
4aaf00458c
commit
43b9e48f58
10
.gitignore
vendored
Executable file
10
.gitignore
vendored
Executable file
|
@ -0,0 +1,10 @@
|
|||
*.o
|
||||
*.pyc
|
||||
build/
|
||||
dist/
|
||||
.DS_Store
|
||||
.hg/
|
||||
.hgignore
|
||||
.idea/
|
||||
.project/
|
||||
Demos/ignores/
|
10
Demos/README.md
Normal file
10
Demos/README.md
Normal file
|
@ -0,0 +1,10 @@
|
|||
# Resources about PIL
|
||||
|
||||
PIL Handbook
|
||||
http://www.pythonware.com/library/pil/handbook/index.htm
|
||||
|
||||
python 简单图像处理 series, ¥lan¥
|
||||
http://www.cnblogs.com/xianglan/archive/2010/12/25/1916953.html
|
||||
|
||||
PIL 学习笔记 series, Neil Chen
|
||||
http://www.cnblogs.com/RChen/archive/2007/03/31/pil_1.html
|
35
Demos/access_pixsel/access_pixsel.py
Normal file
35
Demos/access_pixsel/access_pixsel.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||
parent_path = os.path.dirname(PWD)
|
||||
|
||||
import Image
|
||||
|
||||
file_path = os.path.join(parent_path, "image_resources", "captcha.jpg")
|
||||
|
||||
im = Image.open(fp = file_path)
|
||||
im = im.draft("L", im.size)
|
||||
w, h = im.size[0], im.size[1]
|
||||
pixsels = im.load()
|
||||
|
||||
print "width:", w
|
||||
print "high:", h
|
||||
print "white(255) ~ black(0):", pixsels[0, 0]
|
||||
|
||||
def print_im(im, w = None, h = None):
|
||||
if isinstance(im, Image.Image):
|
||||
w, h = im.size[0], im.size[1]
|
||||
pixsels = im.load()
|
||||
else:
|
||||
pixsels = im
|
||||
|
||||
for x in xrange(w):
|
||||
for y in xrange(h):
|
||||
|
||||
if pixsels[x, y] > 128:
|
||||
print " ",
|
||||
else:
|
||||
print "1",
|
||||
print
|
||||
|
||||
print_im(im, w, h)
|
19
Demos/crop/crop.py
Normal file
19
Demos/crop/crop.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||
parent_path = os.path.dirname(PWD)
|
||||
|
||||
import Image
|
||||
|
||||
file_path = os.path.join(parent_path, "image_resources", "l_hires.jpg")
|
||||
|
||||
im = Image.open(fp = file_path)
|
||||
|
||||
left_upper_x, left_upper_y = 400, 100
|
||||
right_lower_x, right_lower_y = 700, 450
|
||||
box = (left_upper_x, left_upper_y, right_lower_x, right_lower_y)
|
||||
|
||||
region = im.crop(box)
|
||||
|
||||
new_filename = "x".join([str(i) for i in box]) + ".jpg"
|
||||
region.save(new_filename)
|
|
@ -0,0 +1,24 @@
|
|||
#!/usr/bin/env python
|
||||
"""
|
||||
copy image
|
||||
"""
|
||||
import os
|
||||
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||
parent_path = os.path.dirname(PWD)
|
||||
|
||||
import Image
|
||||
|
||||
file_path = os.path.join(parent_path, "image_resources", "l_hires.jpg")
|
||||
im = Image.open(fp = file_path)
|
||||
|
||||
left_upper_x, left_upper_y = 10, 10
|
||||
right_lower_x, right_lower_y = 50, 50
|
||||
box = (left_upper_x, left_upper_y, right_lower_x, right_lower_y)
|
||||
|
||||
region = im.crop(box)
|
||||
|
||||
region = region.transpose(Image.ROTATE_90)
|
||||
im.paste(region, box)
|
||||
|
||||
new_filename = "C-c-C-v-left" + ".jpg"
|
||||
im.save(new_filename)
|
20
Demos/draw_line/draw_line.py
Normal file
20
Demos/draw_line/draw_line.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/env python
|
||||
import Image
|
||||
import ImageDraw
|
||||
|
||||
BLACK = 0
|
||||
WHITE = 255
|
||||
|
||||
|
||||
canvas_w, canvas_h = 100, 100
|
||||
im = Image.new(mode = "L", size = (canvas_w, canvas_h), color = WHITE)
|
||||
|
||||
draw = ImageDraw.Draw(im = im)
|
||||
|
||||
left_top_x, left_top_y = 10, 10
|
||||
right_bottom_x, right_bottom_y = 30, 100
|
||||
box = (left_top_x, left_top_y, right_bottom_x, right_bottom_y)
|
||||
|
||||
draw.line(xy = box, fill = BLACK, width = 1)
|
||||
|
||||
im.save("draw_line.bmp")
|
28
Demos/draw_points/draw_points.py
Normal file
28
Demos/draw_points/draw_points.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/env python
|
||||
import Image
|
||||
import ImageDraw
|
||||
|
||||
|
||||
BLACK = 0
|
||||
WHITE = 255
|
||||
|
||||
|
||||
canvas_w, canvas_h = 100, 100
|
||||
im = Image.new(mode = "L", size = (canvas_w, canvas_h), color = WHITE)
|
||||
|
||||
draw = ImageDraw.Draw(im = im)
|
||||
|
||||
p1_x, p1_y = 10, 10
|
||||
p2_x, p2_y = 15, 15
|
||||
p3_x, p3_y = 20, 10
|
||||
|
||||
xy = (p1_x, p1_y)
|
||||
# or
|
||||
# xy = (p1_x, p1_y, p2_x, p2_y, p3_x, p3_y)
|
||||
|
||||
|
||||
fill = "#000"
|
||||
|
||||
draw.point(xy, fill)
|
||||
|
||||
im.save("draw_points.bmp")
|
20
Demos/draw_rectangle/draw_rectangle.py
Normal file
20
Demos/draw_rectangle/draw_rectangle.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/env python
|
||||
import Image
|
||||
import ImageDraw
|
||||
|
||||
BLACK = 0
|
||||
WHITE = 255
|
||||
|
||||
|
||||
canvas_w, canvas_h = 100, 100
|
||||
im = Image.new(mode = "L", size = (canvas_w, canvas_h), color = WHITE)
|
||||
|
||||
draw = ImageDraw.Draw(im = im)
|
||||
|
||||
left_top_x, left_top_y = 10, 10
|
||||
right_bottom_x, right_bottom_y = 30, 100
|
||||
box = (left_top_x, left_top_y, right_bottom_x, right_bottom_y)
|
||||
|
||||
draw.rectangle(xy = box, fill = BLACK, outline = None)
|
||||
|
||||
im.save("draw_rectangle.bmp")
|
31
Demos/draw_text/draw_text.py
Normal file
31
Demos/draw_text/draw_text.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env python
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
import sys
|
||||
|
||||
BLACK = 0
|
||||
WHITE = 255
|
||||
|
||||
canvas_w, canvas_h = 100, 100
|
||||
im = Image.new(mode = "L", size = (canvas_w, canvas_h), color = WHITE)
|
||||
|
||||
draw = ImageDraw.Draw(im = im)
|
||||
|
||||
left_top_x, left_top_y = 10, 10
|
||||
begin = left_top_x, left_top_y
|
||||
|
||||
text = "hello world"
|
||||
fill = "#000"
|
||||
|
||||
if sys.platform == "darwin":
|
||||
filename = "/Library/Fonts/Microsoft/Times New Roman Bold.ttf"
|
||||
elif sys.platform == "win32":
|
||||
#filename = "C:/Windows/Fonts/timesbd.ttf"
|
||||
filename = "timesbd.ttf"
|
||||
else:
|
||||
raise Exception
|
||||
font_size = 14
|
||||
font = ImageFont.truetype(filename = filename, size = font_size)
|
||||
|
||||
draw.text(xy = begin, text = text, fill = fill, font = font)
|
||||
|
||||
im.save("draw_text.bmp")
|
17
Demos/flip/flip.py
Normal file
17
Demos/flip/flip.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||
parent_path = os.path.dirname(PWD)
|
||||
|
||||
import Image
|
||||
|
||||
file_path = os.path.join(parent_path, "image_resources", "captcha.jpg")
|
||||
im = Image.open(fp = file_path)
|
||||
|
||||
#new_im = im.transpose(Image.FLIP_LEFT_RIGHT)
|
||||
#new_filename = os.path.splitext(filepath)[0] + "flip_left_right" + ".jpg"
|
||||
|
||||
new_im = im.transpose(Image.FLIP_TOP_BOTTOM)
|
||||
new_filename = os.path.splitext(os.path.basename(file_path))[0] + '-' + "flip_top_bottom" + ".jpg"
|
||||
|
||||
new_im.save(new_filename)
|
18
Demos/grayscale/grayscale.py
Normal file
18
Demos/grayscale/grayscale.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||
parent_path = os.path.dirname(PWD)
|
||||
|
||||
import Image
|
||||
|
||||
file_path = os.path.join(parent_path, "image_resources", "l_hires.jpg")
|
||||
|
||||
im = Image.open(fp = file_path)
|
||||
|
||||
# "L" (8-bit pixels, black and white)
|
||||
# http://www.pythonware.com/library/pil/handbook/concepts.htm
|
||||
new_im = im.convert("L")
|
||||
new_file_name = os.path.splitext(os.path.basename(file_path))[0]
|
||||
new_file_name = new_file_name + '-' + 'grayscale' + '.bmp'
|
||||
new_im.save(new_file_name)
|
||||
|
55
Demos/histogram/histogram.py
Normal file
55
Demos/histogram/histogram.py
Normal file
|
@ -0,0 +1,55 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||
parent_path = os.path.dirname(PWD)
|
||||
|
||||
import Image, ImageDraw
|
||||
|
||||
file_path = os.path.join(parent_path, "image_resources", "captcha.jpg")
|
||||
|
||||
BLACK = 0
|
||||
WHITE = 255
|
||||
# white(255) ~ black(0)
|
||||
|
||||
im = Image.open(fp = file_path)
|
||||
w, h = im.size[0], im.size[1]
|
||||
|
||||
print "width:", w
|
||||
print "high:", h
|
||||
|
||||
|
||||
im = im.draft("L", im.size)
|
||||
|
||||
pixsels = im.load()
|
||||
|
||||
|
||||
for x in xrange(w):
|
||||
for y in xrange(h):
|
||||
if pixsels[x, y] > 128:
|
||||
pixsels[x, y] = WHITE
|
||||
else:
|
||||
pixsels[x, y] = BLACK
|
||||
|
||||
|
||||
counts = []
|
||||
for x in xrange(w):
|
||||
count = len([1 for y in xrange(h)
|
||||
if pixsels[x, y] is BLACK])
|
||||
|
||||
counts.append(count)
|
||||
|
||||
|
||||
hist_im = Image.new(mode = "L", size = (w, h), color = WHITE)
|
||||
draw = ImageDraw.Draw(hist_im)
|
||||
h_step = h / max(counts)
|
||||
|
||||
for x in xrange(w):
|
||||
left_top_x, left_top_y = x, h - counts[x] * h_step
|
||||
right_bottom_x, right_bottom_y = x + 1, h
|
||||
box = (left_top_x, left_top_y, right_bottom_x, right_bottom_y)
|
||||
draw.rectangle(xy = box, fill = BLACK)
|
||||
|
||||
new_file_name = os.path.splitext(os.path.basename(file_path))[0]
|
||||
new_file_name = new_file_name + '-' + 'histogram' + '.bmp'
|
||||
|
||||
hist_im.save(new_file_name)
|
22
Demos/image_info/get_img_info.py
Normal file
22
Demos/image_info/get_img_info.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||
parent_path = os.path.dirname(PWD)
|
||||
|
||||
import Image
|
||||
|
||||
file_path = os.path.join(parent_path, "image_resources", "captcha.jpg")
|
||||
|
||||
im = Image.open(fp = file_path)
|
||||
w, h = im.size[0], im.size[1]
|
||||
|
||||
print "format:", type(im.format), im.format
|
||||
print "info:", type(im.info), im.info
|
||||
print "mode:", type(im.mode), im.mode
|
||||
print "size:", type(im.size), im.size
|
||||
print "bands:", type(im.getbands()), im.getbands()
|
||||
print "histogram:", type(im.histogram())
|
||||
|
||||
data = im.getdata()
|
||||
print "getdata:", type(data)
|
||||
assert len(im.getdata()) == w * h
|
BIN
Demos/image_resources/captcha.jpg
Normal file
BIN
Demos/image_resources/captcha.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.9 KiB |
BIN
Demos/image_resources/l_hires.jpg
Normal file
BIN
Demos/image_resources/l_hires.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 558 KiB |
BIN
Demos/image_resources/lena_std.tif
Normal file
BIN
Demos/image_resources/lena_std.tif
Normal file
Binary file not shown.
15
Demos/rotate/rotate.py
Normal file
15
Demos/rotate/rotate.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||
parent_path = os.path.dirname(PWD)
|
||||
|
||||
import Image
|
||||
|
||||
file_path = os.path.join(parent_path, "image_resources", "captcha.jpg")
|
||||
|
||||
im = Image.open(fp = file_path)
|
||||
degress = 90
|
||||
new_im = im.rotate(degress)
|
||||
|
||||
new_filename = os.path.splitext(os.path.basename(file_path))[0] + "-rotate-" + str(degress) + ".jpg"
|
||||
new_im.save(new_filename)
|
20
Demos/thumbnail/create_thumbnail.py
Normal file
20
Demos/thumbnail/create_thumbnail.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||
parent_path = os.path.dirname(PWD)
|
||||
|
||||
import Image
|
||||
|
||||
file_path = os.path.join(parent_path, "image_resources", 'l_hires.jpg')
|
||||
im = Image.open(fp = file_path)
|
||||
|
||||
width, height = im.size[0], im.size[1]
|
||||
new_size = (width/8, height/8)
|
||||
|
||||
im.thumbnail(new_size)
|
||||
|
||||
new_filename= "x".join([str(i) for i in new_size])
|
||||
new_filename = os.path.splitext(os.path.basename(file_path))[0] + '-' + new_filename + ".jpg"
|
||||
new_file_path = os.path.join(PWD, new_filename)
|
||||
|
||||
im.save(new_filename)
|
Loading…
Reference in New Issue
Block a user