python Image库使用入门

先上代码

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

再说目的:

    其实就是为了把一张单色图里面的有效区域重新截取出来,当作练手吧

上三张图片

002446_CVLS_1000353.png

002523_po2X_1000353.png

第三张挂了

顺手吐槽下百度图片 毛都搜不到、、 用谷哥秒秒钟搜出原图

21003035_LUhW.jpg


转载于:https://my.oschina.net/mummy108/blog/644229

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值