先上代码
import Image
img = Image.open("56.bmp");
data = img.load()
(x,y) = img.size
min_x = 0
min_y = 0
max_x = x
max_y = y
image_arr = []
s = 0;
for _x in range(x):
line = [0,y]
for _y in range(y):
if data[_x, _y] != 0:
if line[0] == 0:
line[0] = _y
else:
line[1] = _y
image_arr.append(line)
row = [0,x]
for i in range(len(image_arr)):
if image_arr[i][0] != 0:
if row[0] == 0:
row[0] = i
else:
row[1] = i
colon = [y,0]
for i in range(row[0], row[1]):
start = image_arr[i][0]
end = image_arr[i][1]
if start == 0:
start = y
if end == y:
end = 0
if colon[0] > start:
colon[0] = start
if colon[1] < end:
colon[1] = end
print "start line %s end line %s" % (row[0] , row[1])
print "start colon %s end colon %s" % (colon[0], colon[1])
new_x = row[1] - row[0]
new_y = colon[1]-colon[0]
new_size = (new_x, new_y)
new_image = Image.new("L", new_size)
new_image_data = new_image.load()
for _x in range(new_x):
for _y in range(new_y):
# new_image_data[_x, _y] = 255;
new_image_data[_x, _y] = data[_x+row[0],_y+colon[0]]
new_image.save("cat.bmp")
# new_image.show()
# print image_arr
再说目的:
其实就是为了把一张单色图里面的有效区域重新截取出来,当作练手吧
上三张图片
第三张挂了
顺手吐槽下百度图片 毛都搜不到、、 用谷哥秒秒钟搜出原图