mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-18 19:24:47 +03:00
Merge 1746903f82
into e4faa1069a
This commit is contained in:
commit
adf86423f9
0
.gitignore
vendored
Normal file → Executable file
0
.gitignore
vendored
Normal file → Executable file
53
Demos/README.md
Normal file
53
Demos/README.md
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
# About
|
||||||
|
|
||||||
|
Some simple Pillow demos
|
||||||
|
|
||||||
|
how to run
|
||||||
|
|
||||||
|
[ -d image_resources ] || mkdir image_resources
|
||||||
|
wget http://upload.wikimedia.org/wikipedia/commons/6/69/Captcha.jpg -O image_resources/captcha.jpg
|
||||||
|
python crop/crop.py
|
||||||
|
|
||||||
|
|
||||||
|
## Build pillow on Ubuntu 12.04
|
||||||
|
|
||||||
|
sudo apt-get install zlib1g-dev
|
||||||
|
sudo apt-get install libjpeg8-dev
|
||||||
|
sudo apt-get install libpng12-dev
|
||||||
|
sudo apt-get install libfreetype6-dev
|
||||||
|
sudo apt-get install liblcms1-dev
|
||||||
|
sudo apt-get install python-setuptools
|
||||||
|
python setup.py build
|
||||||
|
|
||||||
|
|
||||||
|
## Build it on Mac OS X 10.6.*
|
||||||
|
|
||||||
|
Build an egg for i386 with Python 2.5/Python 2.6
|
||||||
|
|
||||||
|
export ARCHFLAGS="-arch i386"
|
||||||
|
export CC="/usr/bin/gcc-4.0 -arch i386"
|
||||||
|
|
||||||
|
python2.5 setup.py bdist_egg
|
||||||
|
python2.6 setup.py bdist_egg
|
||||||
|
|
||||||
|
Build an egg for x86_64 with Python 2.7(install it via MacPorts)
|
||||||
|
|
||||||
|
python setup.py bdist_egg
|
||||||
|
|
||||||
|
|
||||||
|
## Install it via package management system
|
||||||
|
|
||||||
|
HomeBrew is cool, you could use it instead of MacPorts on OS X.
|
||||||
|
|
||||||
|
|
||||||
|
## See also
|
||||||
|
|
||||||
|
PIL Handbook
|
||||||
|
|
||||||
|
- http://www.pythonware.com/library/pil/handbook/index.htm
|
||||||
|
|
||||||
|
|
||||||
|
PIL Tutorial
|
||||||
|
|
||||||
|
- http://www.pythonware.com/library/pil/handbook/introduction.htm
|
||||||
|
- http://nadiana.com/category/pil
|
36
Demos/access_pixels/access_pixels.py
Normal file
36
Demos/access_pixels/access_pixels.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
from __future__ import print_function
|
||||||
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
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]
|
||||||
|
pixels = im.load()
|
||||||
|
|
||||||
|
print("width:", w)
|
||||||
|
print("high:", h)
|
||||||
|
print("white(255) ~ black(0):", pixels[0, 0])
|
||||||
|
|
||||||
|
def print_im(im, w=None, h=None):
|
||||||
|
if isinstance(im, Image.Image):
|
||||||
|
w, h = im.size[0], im.size[1]
|
||||||
|
pixels = im.load()
|
||||||
|
else:
|
||||||
|
pixels = im
|
||||||
|
|
||||||
|
for x in range(w):
|
||||||
|
for y in range(h):
|
||||||
|
|
||||||
|
if pixels[x, y] > 128:
|
||||||
|
print(" ", end=' ')
|
||||||
|
else:
|
||||||
|
print("1", end=' ')
|
||||||
|
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
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
file_path = os.path.join(parent_path, "image_resources", "captcha.jpg")
|
||||||
|
|
||||||
|
im = Image.open(fp = file_path)
|
||||||
|
|
||||||
|
left_upper_x, left_upper_y = 0, 0
|
||||||
|
right_lower_x, right_lower_y = 100, 50
|
||||||
|
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(os.path.join(PWD, new_filename))
|
|
@ -0,0 +1,22 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
file_path = os.path.join(parent_path, "image_resources", "captcha.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(os.path.join(PWD, new_filename))
|
24
Demos/draw_line/draw_line.py
Normal file
24
Demos/draw_line/draw_line.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
from PIL import ImageDraw
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
|
||||||
|
BLACK = "#000000"
|
||||||
|
WHITE = "#ffffff"
|
||||||
|
|
||||||
|
canvas_w, canvas_h = 100, 100
|
||||||
|
im = Image.new(mode="RGB", 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(os.path.join(PWD, "draw_line.jpg"))
|
26
Demos/draw_points/draw_points.py
Normal file
26
Demos/draw_points/draw_points.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
from PIL import ImageDraw
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
|
||||||
|
BLACK = "#000000"
|
||||||
|
WHITE = "#ffffff"
|
||||||
|
|
||||||
|
canvas_w, canvas_h = 100, 100
|
||||||
|
im = Image.new(mode="RGB", size=(canvas_w, canvas_h), color=WHITE)
|
||||||
|
|
||||||
|
draw = ImageDraw.Draw(im=im)
|
||||||
|
|
||||||
|
#xy = (p1_x, p1_y)
|
||||||
|
#or
|
||||||
|
#xy = (p1_x, p1_y, p2_x, p2_y, p3_x, p3_y)
|
||||||
|
|
||||||
|
points = ((10, 10), (40, 10), (55, 35), (40,50))
|
||||||
|
for point in points:
|
||||||
|
draw.point(xy=point, fill=BLACK)
|
||||||
|
|
||||||
|
im.save(os.path.join(PWD, "draw_points.jpg"))
|
25
Demos/draw_polygon/draw_polygon.py
Normal file
25
Demos/draw_polygon/draw_polygon.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
from PIL import ImageDraw
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
|
||||||
|
BLACK = "#000000"
|
||||||
|
WHITE = "#ffffff"
|
||||||
|
RED = "#ff0000"
|
||||||
|
|
||||||
|
canvas_w, canvas_h = 100, 100
|
||||||
|
im = Image.new(mode="RGB", size=(canvas_w, canvas_h), color=WHITE)
|
||||||
|
|
||||||
|
draw = ImageDraw.Draw(im=im)
|
||||||
|
|
||||||
|
#xy = (p1_x, p1_y)
|
||||||
|
#or
|
||||||
|
#xy = (p1_x, p1_y, p2_x, p2_y, p3_x, p3_y)
|
||||||
|
|
||||||
|
xy = ((10, 10), (40, 10), (55, 35), (40,50))
|
||||||
|
draw.polygon(xy=xy, fill=RED, outline=BLACK)
|
||||||
|
im.save(os.path.join(PWD, "draw_polygon.jpg"))
|
24
Demos/draw_rectangle/draw_rectangle.py
Normal file
24
Demos/draw_rectangle/draw_rectangle.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
from PIL import ImageDraw
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
|
||||||
|
BLACK = "#000000"
|
||||||
|
WHITE = "#ffffff"
|
||||||
|
|
||||||
|
canvas_w, canvas_h = 100, 100
|
||||||
|
im = Image.new(mode="RGB", 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(os.path.join(PWD, "draw_rectangle.jpg"))
|
43
Demos/draw_text/draw_text.py
Normal file
43
Demos/draw_text/draw_text.py
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
#-*- coding:utf-8 -*-
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
from PIL import ImageDraw
|
||||||
|
from PIL import ImageFont
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
|
||||||
|
BLACK = "#000000"
|
||||||
|
WHITE = "#ffffff"
|
||||||
|
|
||||||
|
canvas_w, canvas_h = 250, 50
|
||||||
|
im = Image.new(mode="RGB", 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 = u"hello world 中文"
|
||||||
|
|
||||||
|
if sys.platform == "darwin":
|
||||||
|
filename = "/Library/Fonts/Microsoft/Times New Roman Bold.ttf"
|
||||||
|
elif sys.platform == "win32":
|
||||||
|
filename = "timesbd.ttf"
|
||||||
|
elif sys.platform == "linux2":
|
||||||
|
# this script required wqy truetype font,
|
||||||
|
# install it on Debian/Ubuntu: apt-get install ttf-wqy-microhei
|
||||||
|
filename = "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc"
|
||||||
|
else:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
font_size = 26
|
||||||
|
font = ImageFont.truetype(filename=filename, size=font_size)
|
||||||
|
|
||||||
|
draw.text(xy=begin, text=text, fill=BLACK, font=font)
|
||||||
|
|
||||||
|
im.save(os.path.join(PWD, "draw_text.bmp"))
|
18
Demos/flip/flip.py
Normal file
18
Demos/flip/flip.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
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(os.path.join(PWD, new_filename))
|
20
Demos/grayscale/grayscale.py
Normal file
20
Demos/grayscale/grayscale.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
|
||||||
|
file_path = os.path.join(parent_path, "image_resources", "captcha.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(os.path.join(PWD, new_file_name))
|
||||||
|
|
50
Demos/histogram/histogram.py
Normal file
50
Demos/histogram/histogram.py
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
from PIL import ImageDraw
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
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]
|
||||||
|
im = im.draft("L", im.size)
|
||||||
|
|
||||||
|
pixels = im.load()
|
||||||
|
|
||||||
|
for x in range(w):
|
||||||
|
for y in range(h):
|
||||||
|
if pixels[x, y] > 128:
|
||||||
|
pixels[x, y] = WHITE
|
||||||
|
else:
|
||||||
|
pixels[x, y] = BLACK
|
||||||
|
|
||||||
|
|
||||||
|
counts = []
|
||||||
|
for x in range(w):
|
||||||
|
count = len([1 for y in range(h)
|
||||||
|
if pixels[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 range(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(os.path.join(PWD, new_file_name))
|
24
Demos/image_info/get_img_info.py
Normal file
24
Demos/image_info/get_img_info.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
from __future__ import print_function
|
||||||
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
|
||||||
|
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
|
16
Demos/rotate/rotate.py
Normal file
16
Demos/rotate/rotate.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
|
||||||
|
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(os.path.join(PWD, new_filename))
|
21
Demos/thumbnail/thumbnail.py
Normal file
21
Demos/thumbnail/thumbnail.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
PWD = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
parent_path = os.path.dirname(PWD)
|
||||||
|
|
||||||
|
file_path = os.path.join(parent_path, "image_resources", 'captcha.jpg')
|
||||||
|
im = Image.open(fp=file_path)
|
||||||
|
|
||||||
|
width, height = im.size[0], im.size[1]
|
||||||
|
new_size = (width/4, height/4)
|
||||||
|
|
||||||
|
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(os.path.join(PWD, new_filename))
|
Loading…
Reference in New Issue
Block a user