Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I wirte some simple demos, please merge it #40

Closed
wants to merge 105 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
43b9e48
Added pythonic demos
May 12, 2012
8b76c69
small changed, updated file readme
May 12, 2012
6ac2be4
fixed drawing demos
May 22, 2012
e6838b6
rm lena images
Jun 18, 2012
0ee26c7
added drawing polygon example
Jun 23, 2012
108228d
fixed fg_color/bg_color in hex
Jun 23, 2012
cb74369
Small chagned, clean up code
Jan 15, 2013
d22ae7a
Using relation path in demo; using captcha image instead of lena
Jan 16, 2013
03c5242
Small changed, fixed spell pixsel -> pixel
Jan 16, 2013
acdda34
Small changed, using range() instead of deprecated xrange()
Jan 16, 2013
065f59f
Fixed draw_text/draw_text.py on Ubuntu
Jan 16, 2013
2b54c99
Use "from PIL import Imagexx" instead of "import Imagexx"
Jan 16, 2013
1e120cb
fixed reading of EPS pictures
Apr 26, 2012
3e709fa
Add .gitignore
Jan 16, 2013
353543d
Add support for tox (http://tox.testrun.org/)
msabramo Jun 6, 2012
dae289d
Add .travis.yml for Travis CI (http://travis-ci.org/)
msabramo Jun 6, 2012
b07b19c
Make things more Python 3 friendly. Python 3 still doesn't work because
msabramo Jun 12, 2012
3bd4f26
Better import of _imaging
emberian Jun 15, 2012
b8cabe1
Test travis
aclark4life Jul 11, 2012
8182f04
Test travis
aclark4life Jul 11, 2012
63a4cb7
Change URL
aclark4life Jul 24, 2012
cac1398
Save watchers
aclark4life Jul 24, 2012
61e266a
Save issues and comments
aclark4life Jul 24, 2012
323824f
Fix URL
aclark4life Aug 22, 2012
997de56
Fix a bug in quantize() causing PIL crashes.
larsyencken Sep 5, 2012
1a8f45b
Fix opening psd files with RGBA layers when A mode is not of type 65535
tdesvenain Jul 29, 2012
f242f72
Added a psd mode (mode 4, 16 bits).
tdesvenain Jul 29, 2012
f4d9eac
revert last commit, images rendered didn't match very well
tdesvenain Jul 29, 2012
4d4e2de
Removed doctests.py that made tests of other packages fail.
tdesvenain Sep 21, 2012
8e0c274
Fix formatting of installation summary
aclark4life Nov 1, 2012
833b2b5
Release 1.7.8
aclark4life Nov 1, 2012
3a21c88
Add Image.alpha_composite
twpayne Dec 4, 2012
d03b1fb
Add files to the MANIFEST which were missing from the source distribu…
Nov 10, 2012
ab94dde
Radius was hardcoded to 2.
Dec 7, 2012
4204c24
Make FLI image detection more stringent
cgohlke Dec 21, 2012
23ebec0
Avoid rounding error in Image.alpha_composite
twpayne Dec 19, 2012
5e7920d
Move custom path of JPEG_ROOT, TIFF_ROOT, etc. before system.
Jan 9, 2013
a9d42ae
fixed crash loading broken color profile from file-like object
d-schmidt Jan 9, 2013
3ab0a29
py3k: Use string methods instead of string module
Oct 10, 2012
ab29a5a
py3k: Update exception usage to modern syntax
Oct 11, 2012
edfe106
py3k: Get setup.py to run under python3
Oct 11, 2012
4a4d7dc
py3k: Modernize type declarations
Oct 13, 2012
71c817c
py3k: Use new buffer protocol
Oct 20, 2012
77256df
py3k: Remove HAVE_UNICODE from _imagingft
Oct 13, 2012
30333de
py3k: Fix strict aliasing slip-up in _imaging
Oct 14, 2012
39484c3
py3k: Add module initialization and unicode/bytes int/long thunks
Oct 14, 2012
a948d0a
py3k: Fix strict aliasing warnings under Python 3
Oct 14, 2012
7de6ab5
py3k: Use "y#" code in PyArg_ParseTuple where we expect byte data
Oct 14, 2012
e1009da
py3k: Import Christoph Gohlke's test suite
Oct 15, 2012
c35207c
py3k: Backport Gohlke's tests to run on 2.6/2.7
Oct 15, 2012
3400e6b
FIX: Handle long values in _imaging getink
Oct 15, 2012
f760f9f
py3k: Provide the missing pngtest_bad.png.base64
Oct 15, 2012
4f120b9
py3k: Remove callable() function
Oct 15, 2012
bb1f0b8
py3k: Fix up uses of dictionary views, ranges, and has_key()
Oct 15, 2012
48b0e0c
py3k: Rewrite dictionary support for Tiff ImageFileDictionary
Oct 15, 2012
f1666cb
py3k: map and filter to list comprehensions
Oct 16, 2012
a5e800b
py3k: Add true Unicode support to OleFileIO
Oct 16, 2012
3189876
py3k: __nonzero__ is now __bool__
Oct 16, 2012
659f1ba
py3k: Use isinstance for numbers and sequences
Oct 16, 2012
61038ad
py3k: Import reduce function
Oct 16, 2012
2b31043
py3k: print is a function
Oct 16, 2012
7a3feb2
py3k: Remove tuples in parameter lists
Oct 16, 2012
6a68d03
py3k: Use relative imports
Oct 16, 2012
2a81f62
py3k: sys.maxint renamed to sys.maxsize
Oct 16, 2012
9cdb285
py3k: Convert backticks to repr()
Oct 16, 2012
2868e0c
py3k: Remove Image types import
Oct 16, 2012
59d1372
py3k: __builtin__ module is now builtins
Oct 17, 2012
b0f6f95
py3k: Tkinter module is now tkinter
Oct 17, 2012
d37a2e1
py3k: Convert StringIO.StringIO to io.BytesIO
Oct 17, 2012
1a82abc
py3k: Integer long literals are no longer valid syntax
Oct 17, 2012
da5ca4c
py3k: Change apply() to unpacking syntax
Oct 20, 2012
73f7c7a
py3k: 2to3's "idiom" filter
Oct 17, 2012
67316d7
py3k: Map long type to int
Oct 17, 2012
12d8977
py3k: Sort by key, not cmp
Oct 17, 2012
952685f
py3k: Type coercion is gone
Oct 19, 2012
3c448d8
py3k: Publish both frombytes and fromstring in C modules for old Python
Oct 20, 2012
6f797c5
py3k: The big push
Oct 20, 2012
cb979c4
py3k: Allow slicing for paths
cgohlke Oct 21, 2012
ebc23bf
py3k: Remove ancient Python hacks
Oct 21, 2012
d6c6dd4
py3k: FIX: Bad upside-down check in BmpImagePlugin
Oct 24, 2012
825718a
py3k: FIX: TGA missing o8 declaration
Oct 24, 2012
5addfab
py3k: Get rid of _ParserFile; io.BytesIO does its job
Oct 24, 2012
727c068
py3k: Actually fix the EPS encoder
Oct 24, 2012
0bc8cfc
py3k: Add PCX to roundtrip test
Oct 24, 2012
5bc836b
py3k: Add Photoshop sanity check
Oct 24, 2012
348e970
py3k: Add .FLI sanity check, and fix palette bytes bug
Oct 24, 2012
152da84
py3k: Rewrite PdfImagePlugin into something more legible
Oct 24, 2012
bd957a9
py3k: Add TarIO test
Oct 25, 2012
730c6dd
py3k: Add XPM read test
Oct 25, 2012
965928c
py3k: Add Windows icon sanity test
Oct 25, 2012
2891d5a
py3k: Remove use_2to3 from setup.py
Oct 28, 2012
9ab3ed3
py3k: inquiry is now lenfunc
Oct 30, 2012
34ad0b7
py3k: Issue warnings when using the old fromstring/tostring
Nov 9, 2012
72c43ef
Fix tox test running: port selftest.py to Python 3; make it clear Pyt…
kmike Oct 29, 2012
330b26a
Update the manifest to include a lot of missing files
Nov 10, 2012
a15e762
Make pysane Python3-compatible
manisandro Dec 23, 2012
0a8d35f
Set up Sane error object as exception instead of string
Jan 10, 2013
1a1e19d
Handle Sane option strings as Unicode strings
Jan 10, 2013
97bf36d
Modified map.c to fix some MSVC10 compilation errors.
Jan 10, 2013
14162dc
fixed an error with utf-8 chars in color profile names
d-schmidt Jan 10, 2013
e87ad2a
Added possibility to save gifs with a custom palette when using color…
d-schmidt Jan 10, 2013
8fd4868
added option to load truncated image-files
d-schmidt Jan 10, 2013
78a0ab5
Added possibility to save exif information in jpeg-files
d-schmidt Jan 10, 2013
8db7169
Merge remote-tracking branch 'upstream/master'
Jan 16, 2013
1746903
py3k: Use python3 version print
Jan 17, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.o
*.pyc
build/
dist/
.DS_Store
.hg/
.hgignore
.idea/
.project/
Demos/ignores/
Pillow.egg-info/
41 changes: 41 additions & 0 deletions Demos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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 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
34 changes: 34 additions & 0 deletions Demos/access_pixels/access_pixels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python
import os
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 " ",
else:
print "1",
print

print_im(im, w, h)
18 changes: 18 additions & 0 deletions Demos/crop/crop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python
import os
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))
21 changes: 21 additions & 0 deletions Demos/cutting_pasting_and_merging/cutting_pasting_and_merging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
import os
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))
23 changes: 23 additions & 0 deletions Demos/draw_line/draw_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python
import os
import Image
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 changes: 26 additions & 0 deletions Demos/draw_points/draw_points.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python
import os
import Image
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 changes: 25 additions & 0 deletions Demos/draw_polygon/draw_polygon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python
import os
import Image
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"))
23 changes: 23 additions & 0 deletions Demos/draw_rectangle/draw_rectangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python
import os
import Image
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"))
40 changes: 40 additions & 0 deletions Demos/draw_text/draw_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python
import os
import sys

from PIL import Image, ImageDraw, ImageFont


PWD = os.path.dirname(os.path.realpath(__file__))
parent_path = os.path.dirname(PWD)

BLACK = "#ffffff"
WHITE = "#000000"

fg_color = WHITE

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is never used. Why does it exist?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has deleted in new commit.

bg_color = BLACK


canvas_w, canvas_h = 180, 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 = "hello world"

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 = 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"))
17 changes: 17 additions & 0 deletions Demos/flip/flip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python
import os
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))
19 changes: 19 additions & 0 deletions Demos/grayscale/grayscale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python
import os
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))

55 changes: 55 additions & 0 deletions Demos/histogram/histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python
import os
import Image, 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]

print "width:", w
print "high:", h


im = im.draft("L", im.size)

pixsels = im.load()


for x in range(w):
for y in range(h):
if pixsels[x, y] > 128:
pixsels[x, y] = WHITE
else:
pixsels[x, y] = BLACK


counts = []
for x in range(w):
count = len([1 for y in range(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(os.path.join(PWD, new_file_name))
23 changes: 23 additions & 0 deletions Demos/image_info/get_img_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python
import os
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
Loading