imagetools.py 1.6 KB
Newer Older
xiebaiyuan's avatar
xiebaiyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
# coding=utf-8
import cv2
from array import array


def resize_take_rgbs(path, shape_h_w):
    print '--------------resize_take_rgbs-----------------begin'
    image = cv2.imread(path)
    # print image.shape
    cv2.imshow("before", image)

    print_rgb(image[0, 0])
    # image len may be for .just check it
    # image.resize(shape_h_w)

    image = cv2.resize(image, (shape_h_w[0], shape_h_w[1]))

    cv2.imshow("after", image)
    print image.shape
    height = shape_h_w[0]
    width = shape_h_w[1]

    rs_ = []
    gs_ = []
    bs_ = []
    for h in range(0, height):
        for w in range(0, width):
            bs_.append(image[h, w, 0])
            gs_.append(image[h, w, 1])
            rs_.append(image[h, w, 2])

    # print image[2, 2, 0]/255.
    print len(bs_)
    print len(gs_)
    print len(rs_)
    print '--------------resize_take_rgbs-----------------end'
    return bs_, gs_, rs_


def print_rgb((b, g, r)):
    print "像素 - R:%d,G:%d,B:%d" % (r, g, b)  # 显示像素值
    #
    # image[0, 0] = (100, 150, 200)  # 更改位置(0,0)处的像素
    #
    # (b, g, r) = image[0, 0]  # 再次读取(0,0)像素
    # print "位置(0,0)处的像素 - 红:%d,绿:%d,蓝:%d" % (r, g, b)  # 显示更改后的像素值
    #
    # corner = image[0:100, 0:100]  # 读取像素块
    # cv2.imshow("Corner", corner)  # 显示读取的像素块
    #
    # image[0:100, 0:100] = (0, 255, 0);  # 更改读取的像素块
    #
    # cv2.imshow("Updated", image)  # 显示图像
    #
    # cv2.waitKey(0)  # 程序暂停


def save_to_file(to_file_name, array):
    to_file = open(to_file_name, "wb")
    array.tofile(to_file)
    to_file.close()