Small chagned, clean up code

This commit is contained in:
Shuge Lee 2013-01-15 11:03:52 +08:00
parent 108228d84b
commit cb743697fc
12 changed files with 29 additions and 29 deletions

View File

@ -16,7 +16,7 @@ print "width:", w
print "high:", h print "high:", h
print "white(255) ~ black(0):", pixsels[0, 0] print "white(255) ~ black(0):", pixsels[0, 0]
def print_im(im, w = None, h = None): def print_im(im, w=None, h=None):
if isinstance(im, Image.Image): if isinstance(im, Image.Image):
w, h = im.size[0], im.size[1] w, h = im.size[0], im.size[1]
pixsels = im.load() pixsels = im.load()

View File

@ -9,14 +9,14 @@ fg_color = BLACK
bg_color = WHITE bg_color = WHITE
canvas_w, canvas_h = 100, 100 canvas_w, canvas_h = 100, 100
im = Image.new(mode = "RGB", size = (canvas_w, canvas_h), color = bg_color) im = Image.new(mode="RGB", size=(canvas_w, canvas_h), color=bg_color)
draw = ImageDraw.Draw(im = im) draw = ImageDraw.Draw(im=im)
left_top_x, left_top_y = 10, 10 left_top_x, left_top_y = 10, 10
right_bottom_x, right_bottom_y = 30, 100 right_bottom_x, right_bottom_y = 30, 100
box = (left_top_x, left_top_y, right_bottom_x, right_bottom_y) box = (left_top_x, left_top_y, right_bottom_x, right_bottom_y)
draw.line(xy = box, fill = fg_color, width = 1) draw.line(xy=box, fill=fg_color, width=1)
im.save("draw_line.jpeg") im.save("draw_line.jpeg")

View File

@ -11,9 +11,9 @@ bg_color = WHITE
canvas_w, canvas_h = 100, 100 canvas_w, canvas_h = 100, 100
im = Image.new(mode = "RGB", size = (canvas_w, canvas_h), color = bg_color) im = Image.new(mode="RGB", size=(canvas_w, canvas_h), color=bg_color)
draw = ImageDraw.Draw(im = im) draw = ImageDraw.Draw(im=im)
#xy = (p1_x, p1_y) #xy = (p1_x, p1_y)
#or #or
@ -21,6 +21,6 @@ draw = ImageDraw.Draw(im = im)
points = ((10, 10), (40, 10), (55, 35), (40,50)) points = ((10, 10), (40, 10), (55, 35), (40,50))
for point in points: for point in points:
draw.point(xy = point, fill = fg_color) draw.point(xy=point, fill=fg_color)
im.save("draw_points.jpeg") im.save("draw_points.jpeg")

View File

@ -9,14 +9,14 @@ bg_color = WHITE
canvas_w, canvas_h = 100, 100 canvas_w, canvas_h = 100, 100
im = Image.new(mode = "RGB", size = (canvas_w, canvas_h), color = bg_color) im = Image.new(mode="RGB", size=(canvas_w, canvas_h), color=bg_color)
draw = ImageDraw.Draw(im = im) draw = ImageDraw.Draw(im=im)
#xy = (p1_x, p1_y) #xy = (p1_x, p1_y)
#or #or
#xy = (p1_x, p1_y, p2_x, p2_y, p3_x, p3_y) #xy = (p1_x, p1_y, p2_x, p2_y, p3_x, p3_y)
xy = ((10, 10), (40, 10), (55, 35), (40,50)) xy = ((10, 10), (40, 10), (55, 35), (40,50))
draw.polygon(xy = xy, fill = RED, outline = BLACK) draw.polygon(xy=xy, fill=RED, outline=BLACK)
im.save("draw_polygon.jpeg") im.save("draw_polygon.jpeg")

View File

@ -10,14 +10,14 @@ bg_color = WHITE
canvas_w, canvas_h = 100, 100 canvas_w, canvas_h = 100, 100
im = Image.new(mode = "RGB", size = (canvas_w, canvas_h), color = bg_color) im = Image.new(mode="RGB", size=(canvas_w, canvas_h), color=bg_color)
draw = ImageDraw.Draw(im = im) draw = ImageDraw.Draw(im=im)
left_top_x, left_top_y = 10, 10 left_top_x, left_top_y = 10, 10
right_bottom_x, right_bottom_y = 30, 100 right_bottom_x, right_bottom_y = 30, 100
box = (left_top_x, left_top_y, right_bottom_x, right_bottom_y) box = (left_top_x, left_top_y, right_bottom_x, right_bottom_y)
draw.rectangle(xy = box, fill = fg_color, outline = None) draw.rectangle(xy=box, fill=fg_color, outline=None)
im.save("draw_rectangle.jpeg") im.save("draw_rectangle.jpeg")

View File

@ -10,9 +10,9 @@ bg_color = BLACK
canvas_w, canvas_h = 180, 50 canvas_w, canvas_h = 180, 50
im = Image.new(mode = "RGB", size = (canvas_w, canvas_h), color = bg_color) im = Image.new(mode="RGB", size=(canvas_w, canvas_h), color=bg_color)
draw = ImageDraw.Draw(im = im) draw = ImageDraw.Draw(im=im)
left_top_x, left_top_y = 10, 10 left_top_x, left_top_y = 10, 10
begin = left_top_x, left_top_y begin = left_top_x, left_top_y
@ -27,8 +27,8 @@ elif sys.platform == "win32":
else: else:
raise Exception raise Exception
font_size = 26 font_size = 26
font = ImageFont.truetype(filename = filename, size = font_size) font = ImageFont.truetype(filename=filename, size=font_size)
draw.text(xy = begin, text = text, fill = fg_color, font = font) draw.text(xy=begin, text=text, fill=fg_color, font=font)
im.save("draw_text.bmp") im.save("draw_text.bmp")

View File

@ -6,7 +6,7 @@ parent_path = os.path.dirname(PWD)
import Image import Image
file_path = os.path.join(parent_path, "image_resources", "captcha.jpg") file_path = os.path.join(parent_path, "image_resources", "captcha.jpg")
im = Image.open(fp = file_path) im = Image.open(fp=file_path)
#new_im = im.transpose(Image.FLIP_LEFT_RIGHT) #new_im = im.transpose(Image.FLIP_LEFT_RIGHT)
#new_filename = os.path.splitext(filepath)[0] + "flip_left_right" + ".jpg" #new_filename = os.path.splitext(filepath)[0] + "flip_left_right" + ".jpg"
@ -14,4 +14,4 @@ im = Image.open(fp = file_path)
new_im = im.transpose(Image.FLIP_TOP_BOTTOM) new_im = im.transpose(Image.FLIP_TOP_BOTTOM)
new_filename = os.path.splitext(os.path.basename(file_path))[0] + '-' + "flip_top_bottom" + ".jpg" new_filename = os.path.splitext(os.path.basename(file_path))[0] + '-' + "flip_top_bottom" + ".jpg"
new_im.save(new_filename) new_im.save(new_filename)

View File

@ -7,7 +7,7 @@ import Image
file_path = os.path.join(parent_path, "image_resources", "l_hires.jpg") file_path = os.path.join(parent_path, "image_resources", "l_hires.jpg")
im = Image.open(fp = file_path) im = Image.open(fp=file_path)
# "L" (8-bit pixels, black and white) # "L" (8-bit pixels, black and white)
# http://www.pythonware.com/library/pil/handbook/concepts.htm # http://www.pythonware.com/library/pil/handbook/concepts.htm

View File

@ -39,7 +39,7 @@ for x in xrange(w):
counts.append(count) counts.append(count)
hist_im = Image.new(mode = "L", size = (w, h), color = WHITE) hist_im = Image.new(mode="L", size=(w, h), color=WHITE)
draw = ImageDraw.Draw(hist_im) draw = ImageDraw.Draw(hist_im)
h_step = h / max(counts) h_step = h / max(counts)
@ -47,9 +47,9 @@ for x in xrange(w):
left_top_x, left_top_y = x, h - counts[x] * h_step left_top_x, left_top_y = x, h - counts[x] * h_step
right_bottom_x, right_bottom_y = x + 1, h right_bottom_x, right_bottom_y = x + 1, h
box = (left_top_x, left_top_y, right_bottom_x, right_bottom_y) box = (left_top_x, left_top_y, right_bottom_x, right_bottom_y)
draw.rectangle(xy = box, fill = BLACK) draw.rectangle(xy=box, fill=BLACK)
new_file_name = os.path.splitext(os.path.basename(file_path))[0] new_file_name = os.path.splitext(os.path.basename(file_path))[0]
new_file_name = new_file_name + '-' + 'histogram' + '.bmp' new_file_name = new_file_name + '-' + 'histogram' + '.bmp'
hist_im.save(new_file_name) hist_im.save(new_file_name)

View File

@ -7,7 +7,7 @@ import Image
file_path = os.path.join(parent_path, "image_resources", "captcha.jpg") file_path = os.path.join(parent_path, "image_resources", "captcha.jpg")
im = Image.open(fp = file_path) im = Image.open(fp=file_path)
w, h = im.size[0], im.size[1] w, h = im.size[0], im.size[1]
print "format:", type(im.format), im.format print "format:", type(im.format), im.format
@ -19,4 +19,4 @@ print "histogram:", type(im.histogram())
data = im.getdata() data = im.getdata()
print "getdata:", type(data) print "getdata:", type(data)
assert len(im.getdata()) == w * h assert len(im.getdata()) == w * h

View File

@ -7,9 +7,9 @@ import Image
file_path = os.path.join(parent_path, "image_resources", "captcha.jpg") file_path = os.path.join(parent_path, "image_resources", "captcha.jpg")
im = Image.open(fp = file_path) im = Image.open(fp=file_path)
degress = 90 degress = 90
new_im = im.rotate(degress) new_im = im.rotate(degress)
new_filename = os.path.splitext(os.path.basename(file_path))[0] + "-rotate-" + str(degress) + ".jpg" new_filename = os.path.splitext(os.path.basename(file_path))[0] + "-rotate-" + str(degress) + ".jpg"
new_im.save(new_filename) new_im.save(new_filename)

View File

@ -6,7 +6,7 @@ parent_path = os.path.dirname(PWD)
import Image import Image
file_path = os.path.join(parent_path, "image_resources", 'l_hires.jpg') file_path = os.path.join(parent_path, "image_resources", 'l_hires.jpg')
im = Image.open(fp = file_path) im = Image.open(fp=file_path)
width, height = im.size[0], im.size[1] width, height = im.size[0], im.size[1]
new_size = (width/8, height/8) new_size = (width/8, height/8)
@ -17,4 +17,4 @@ 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_filename = os.path.splitext(os.path.basename(file_path))[0] + '-' + new_filename + ".jpg"
new_file_path = os.path.join(PWD, new_filename) new_file_path = os.path.join(PWD, new_filename)
im.save(new_filename) im.save(new_filename)