Created
November 23, 2017 04:28
-
-
Save mulderu/c28b9dc03689f185fb076d82495924fd to your computer and use it in GitHub Desktop.
python, pillow, pil, image processing demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import numpy as np | |
import cv2 | |
from PIL import Image | |
from PIL import ImageDraw | |
from PIL import ImageFilter | |
''' | |
ref : http://pillow.readthedocs.io/en/3.4.x/index.html | |
ref : basic : https://www.youtube.com/watch?v=Fd_0ns8bmCY | |
video tutorial : https://www.youtube.com/watch?v=j6tXKS2Pl8Q and more... | |
''' | |
demo_python = '/Users/mulder/Desktop/demo-python.png' | |
demo_file = '/Users/mulder/Desktop/demo.png' | |
demo2_file = '/Users/mulder/Desktop/demo2.png' | |
def demo_new(): | |
size = width, height = 320, 240 | |
#image = Image.new("RGB", size, "rgb(100,200,220)") | |
image = Image.new("RGB", size, "hsl(0, 100%, 20%)") | |
image.show() | |
del image | |
def demo_getpixel(): | |
image = Image.open(demo_file) | |
size = width, height = image.size | |
coordinate = x, y = 180, 69 | |
print image.getpixel( coordinate ) | |
# Returns: The pixel value. If the image is a multi-layer image, this method returns a tuple. # (R,G,B,A) | |
image.show(); | |
del image | |
def demo_blend(): | |
image1 = Image.open(demo_file) | |
image1_size = width, height = image1.size | |
image2 = Image.open(demo2_file).resize(image1_size, Image.ANTIALIAS) | |
Image.blend(image1, image2, 0.5).show() | |
del image1, image2 | |
def demo_composite(): | |
image1 = Image.open(demo_file) | |
image1_size = width, height = image1.size | |
image2 = Image.open(demo2_file).resize(image1_size, Image.ANTIALIAS) | |
Image.composite(image1, image2, image1).show() | |
del image1, image2 | |
def demo_convert(): | |
image = Image.open(demo_file) | |
image.convert("L").show(); | |
del image | |
def demo_crop(): | |
image = Image.open(demo_file) | |
image.crop( (10, 10, 400, 200) ).show(); | |
del image | |
def demo_filter(): | |
image = Image.open(demo_file) | |
image.filter( ImageFilter.BLUR ).show(); | |
raw_input() | |
image.filter( ImageFilter.CONTOUR ).show(); | |
raw_input() | |
image.filter( ImageFilter.EDGE_ENHANCE ).show(); | |
raw_input() | |
image.filter( ImageFilter.EDGE_ENHANCE_MORE ).show(); | |
raw_input() | |
image.filter( ImageFilter.EMBOSS ).show(); | |
raw_input() | |
image.filter( ImageFilter.FIND_EDGES ).show(); | |
raw_input() | |
image.filter( ImageFilter.SMOOTH ).show(); | |
raw_input() | |
image.filter( ImageFilter.SMOOTH_MORE ).show(); | |
raw_input() | |
image.filter( ImageFilter.SHARPEN ).show(); | |
del image | |
def demo_getcolors(): | |
image = Image.open(demo_python) | |
size = width, height = image.size | |
image.show() | |
print image.getcolors(width * height) | |
# Returns: An unsorted list of (count, pixel) values. | |
del image | |
def demo_getdata(): | |
image = Image.open(demo_python) | |
size = width, height = image.size | |
image.show() | |
print list(image.getdata()) | |
# A sequence-like object. | |
del image | |
def demo_paste(): | |
image = Image.open(demo_python) | |
size = width, height = image.size | |
image.paste('blue', (10,10, 50, 200)) | |
image.show() | |
# | |
image1 = Image.open(demo_file) | |
image2 = Image.open(demo2_file).crop( (0, 0, 400, 200) ) | |
image1.paste(image2, (200, 200, 600, 400)) | |
image1.show() | |
del image, image1, image2 | |
def demo_rotate_and_putdata_and_save(): | |
image = Image.open(demo_python) | |
size = width, height = image.size | |
rotated_img = image.rotate(56) | |
rotated_img.show() | |
color_to_find = (0, 0, 0, 0) | |
color_to_replace = (255, 255, 255, 255) | |
new_image_data = [] | |
for color in list(rotated_img.getdata()): | |
if (color == color_to_find): | |
new_image_data += [ color_to_replace ] | |
else: | |
new_image_data += [ color ] | |
rotated_img.putdata( new_image_data ) | |
rotated_img.show() | |
rotated_img.save('rotated_img.png') | |
del image, rotated_img | |
def demo_putpixel_basic(): | |
image = Image.open(demo_python) | |
size = width, height = image.size | |
x = 100 | |
for y in range(100): | |
image.putpixel( (x,y), (0,0,0, 255) ) | |
image.show() | |
del image | |
def demo_get_pixel_object(): | |
image = Image.open(demo_python) | |
size = width, height = image.size | |
pixel_access_object = image.load() | |
y=100 | |
for x in range(width): | |
print str(x)+":"+str(y)+"=>", pixel_access_object[x,y] | |
del image | |
def demo_transpose(): | |
image = Image.open(demo_python) | |
size = width, height = image.size | |
tpImage = image.transpose(Image.FLIP_LEFT_RIGHT) | |
# FLIP_TOP_BOTTOM,ROTATE_90,ROTATE_180,ROTATE_270,TRANSPOSE | |
tpImage.show() | |
del image, tpImage | |
def demo_load_split_merge_mode(): | |
image = Image.open(demo_python) | |
size = width, height = image.size | |
print image.mode | |
image.load() | |
r,g,b, a = image.split() | |
#image = Image.merge("RGB", (r,g,b)) | |
image = Image.merge("RGB", (b,g,r)) | |
print image.mode | |
#for color in image.getdata(): | |
# print color | |
image.show() | |
del image | |
def demo_print_image_fnames_in_dir(): | |
for f in os.listdir("/Users/mulder/Desktop"): | |
if (f.endswith(".png")): | |
#i = Image.open(f) | |
fn, ext = os.path.splitext(f) | |
print('png => {} : {}'.format(fn, ext)) | |
# print like : png => spine-8point-mark-01 : .png | |
def demo_paste_and_make_multi_pic(): | |
original = Image.open(demo_python) | |
original.show('original'); | |
x_size, y_size = original.size | |
x_half, y_half = x_size/2, y_size/2 | |
half = original.resize((x_half, y_half)) | |
x_double, y_double = x_size*2, y_size*2 | |
double = Image.new("RGB", (x_double, y_double)) | |
for x in range(4): | |
for y in range(4): | |
box = (x*x_half, y*y_half, (x+1)*x_half, (y+1)*y_half) | |
double.paste(half, box) | |
double.show('double'); | |
del original, double | |
def demo_draw_frame(): | |
BORDER = 10 | |
GRAY = (128,128,128) | |
pic = Image.open(demo_python) | |
x_size, y_size = pic.size | |
draw = ImageDraw.Draw(pic) | |
draw.rectangle((0,0, x_size, BORDER), fill=GRAY) | |
draw.rectangle((0,0, BORDER, y_size), fill=GRAY) | |
draw.rectangle((0,y_size-BORDER, x_size, y_size), fill=GRAY) | |
draw.rectangle((x_size-BORDER, 0, x_size, y_size), fill=GRAY) | |
pic.show('framed'); | |
del pic | |
def demo_split_and_merge(): | |
R, G, B, = 0, 1, 2 | |
SCALE = 0.5 | |
def decrease(x): return x * SCALE | |
pic = Image.open(demo_python) | |
pic.load() | |
bands = pic.split() | |
bands = (bands[R].point(decrease), bands[G], bands[B]) | |
more_red = Image.merge("RGB", bands) | |
more_red.show('more-red') | |
del pic, more_red | |
def demo_blend_paste_image_overlap(): | |
def get(f): | |
pic = Image.open(f) | |
x_size, y_size = pic.size | |
return (pic, x_size, y_size) | |
BLEND = 0.8 | |
major_name, highlight_name = demo_file, demo_python | |
major, major_x, major_y = get(major_name) | |
highlight, hl_x, hl_y = get(highlight_name) | |
box = (50, 50, 50+hl_x, 50+hl_y) | |
middle = major.crop(box) | |
middle = Image.blend(middle, highlight, BLEND) | |
major.paste(middle, box) | |
major.show() | |
del major, highlight | |
def demo_img_init_gradation_effect(): | |
img = Image.new("RGB", (640,400), (0,0,255)) | |
for x in xrange(640): | |
for y in xrange(400): | |
img.putpixel( (x,y), (x/3,(x+y)/6, y/2) ) | |
img.show() | |
del img | |
def demo_last(): | |
img = Image.open(demo_file) | |
for x in xrange(img.size[0]): | |
for y in xrange(img.size[1]): | |
r,g,b, a = img.getpixel((x,y)) | |
img.putpixel( (x,y), (b,g,r) ) | |
img.show() | |
del img | |
demo_last() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment