diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/Demos/README.md b/Demos/README.md new file mode 100644 index 00000000000..2617677caf6 --- /dev/null +++ b/Demos/README.md @@ -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 diff --git a/Demos/access_pixels/access_pixels.py b/Demos/access_pixels/access_pixels.py new file mode 100644 index 00000000000..fb9ccfcae7a --- /dev/null +++ b/Demos/access_pixels/access_pixels.py @@ -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) \ No newline at end of file diff --git a/Demos/crop/crop.py b/Demos/crop/crop.py new file mode 100644 index 00000000000..c736da757db --- /dev/null +++ b/Demos/crop/crop.py @@ -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)) diff --git a/Demos/cutting_pasting_and_merging/cutting_pasting_and_merging.py b/Demos/cutting_pasting_and_merging/cutting_pasting_and_merging.py new file mode 100644 index 00000000000..32b7df8f087 --- /dev/null +++ b/Demos/cutting_pasting_and_merging/cutting_pasting_and_merging.py @@ -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)) diff --git a/Demos/draw_line/draw_line.py b/Demos/draw_line/draw_line.py new file mode 100644 index 00000000000..f73aae6b64a --- /dev/null +++ b/Demos/draw_line/draw_line.py @@ -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")) diff --git a/Demos/draw_points/draw_points.py b/Demos/draw_points/draw_points.py new file mode 100644 index 00000000000..a9689408cf3 --- /dev/null +++ b/Demos/draw_points/draw_points.py @@ -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")) diff --git a/Demos/draw_polygon/draw_polygon.py b/Demos/draw_polygon/draw_polygon.py new file mode 100644 index 00000000000..6fa81c02341 --- /dev/null +++ b/Demos/draw_polygon/draw_polygon.py @@ -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")) diff --git a/Demos/draw_rectangle/draw_rectangle.py b/Demos/draw_rectangle/draw_rectangle.py new file mode 100644 index 00000000000..8e2011311b4 --- /dev/null +++ b/Demos/draw_rectangle/draw_rectangle.py @@ -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")) diff --git a/Demos/draw_text/draw_text.py b/Demos/draw_text/draw_text.py new file mode 100644 index 00000000000..b63c914a610 --- /dev/null +++ b/Demos/draw_text/draw_text.py @@ -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")) diff --git a/Demos/flip/flip.py b/Demos/flip/flip.py new file mode 100644 index 00000000000..0903b776e7f --- /dev/null +++ b/Demos/flip/flip.py @@ -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)) diff --git a/Demos/grayscale/grayscale.py b/Demos/grayscale/grayscale.py new file mode 100644 index 00000000000..ad88ef3c98c --- /dev/null +++ b/Demos/grayscale/grayscale.py @@ -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)) + diff --git a/Demos/histogram/histogram.py b/Demos/histogram/histogram.py new file mode 100644 index 00000000000..90a00b5e5e0 --- /dev/null +++ b/Demos/histogram/histogram.py @@ -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)) diff --git a/Demos/image_info/get_img_info.py b/Demos/image_info/get_img_info.py new file mode 100644 index 00000000000..fc59b025c4b --- /dev/null +++ b/Demos/image_info/get_img_info.py @@ -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 diff --git a/Demos/rotate/rotate.py b/Demos/rotate/rotate.py new file mode 100644 index 00000000000..c070d6652c7 --- /dev/null +++ b/Demos/rotate/rotate.py @@ -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)) diff --git a/Demos/thumbnail/thumbnail.py b/Demos/thumbnail/thumbnail.py new file mode 100644 index 00000000000..fbb9640de6d --- /dev/null +++ b/Demos/thumbnail/thumbnail.py @@ -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))