例子

这是一个非常简单的图像处理和计算机视觉 Python 练习系列,旨在介绍这些主题,几乎没有实用性。对于任何新手的错误,我很抱歉,它仍处于开发阶段。在本系列中,为了简单起见,我将限制我们可以用于 PNG 文件的数字图像,我还将讨论图像压缩的主题。

如果你还没有这样做,请克隆存储库,或者你可以通过 Github 在此处下载它 :

git clone https://github.com/Skorkmaz88/compvis101

你可以使用两个文件,其中一个是 tutorial0.py,另一个是 readingImages.ipynb,第二个是 ipython 笔记本,如果你喜欢使用它你可以这样做。但是两个文件正在做同样的事情。

代码在评论中有解释,我是

 # libs
import png

# We create a greyscale image as described in our text.
# To do that simply, we create a 2D array in python. 
# x and y, x being horizontal and y being vertical directions.

x  = []
y = []
# Play around with these pixels values to get different grayscale images, they shoud be 
# in range of 0 - 255. 
white = 255
gray = 128
black = 0
width  = 100
height = 300

# Add 100 x 100 rectangle as just white(255) valued pixels
for i in range(0, 100):
    for j in range(0,100):
        y.append(white); # Pixel (i,j) is being set to a value, rest is coding trick to nest two lists 
    x.append(y)
    y = []
    
# Add 100 x 100 rectangle as just mid-gray(128) valued pixels
for i in range(0, 100):
    for j in range(0,100):
        y.append(gray);
    x.append(y)
    y = []

# Add 100 x 100 rectangle as just black(0) valued pixels
for i in range(0, 100):
    for j in range(0,100):
        y.append(black);
    x.append(y)
    y = []

# output image file 
f = open('out.png', 'wb')
w = png.Writer(width, height , greyscale=True, bitdepth=8)
w.write(f, x)
f.close()
# If everything went well, you should have 3 vertically aligned rectangles white, gray and black 
# Check your working folder

# PART 2
# Read a grayscale image and convert it to binary

# This time we will binarize a grayscale image, to do that we will read pixels and according to threshold we set
# we will decide if that pixel should be white or black 

# This file is originally 8 bit png image, can be found in github repository, you should use only this type of
# images if you want to change the image.
f = open('./img/lenaG.png', 'r')

r=png.Reader(file=f)
# You will the details about the image, for now pay attention to size and bitdepth only.
img = r.read()

width = img[0]
height = img[1]
# Threshold value for binarizing images, 
threshold = 128
print "Input image size is: "+ str(width)+ " pixels as  width, " + str(height) + " pixels as height"

f_out = open('lenaBinary.png', 'wb')
w = png.Writer(width, height , greyscale=True, bitdepth=1)

pixels = img[2]
 
x = []
y = []

# Let's traverse the Lena image 
for row in pixels:
    for pixel in row:
        p_value =  pixel
        # Now here we binarize image in pixel level
        if p_value > threshold:
            p_value = 1
        else:
            p_value = 0
            
        y.append(p_value);
    x.append(y)
    y = []

w.write(f_out, x)
f_out.close()

如果一切顺利,恭喜! 你从头开始创建图像,并在现有图像上执行第一个像素级转换。检查你的工作文件夹以查看新图像