@TangWill
2019-07-21T19:00:16.000000Z
字数 2058
阅读 486
大创
# -*- coding: utf-8 -*-
from PIL import Image
import os
def de_bruijn(k, n):
"""
de Bruijn sequence for alphabet k
and subsequences of length n.
"""
try:
# let's see if k can be cast to an integer;
# if so, make our alphabet a list
_ = int(k)
alphabet = list(map(str, range(k)))
except (ValueError, TypeError):
alphabet = k
k = len(k)
a = [0] * k * n
sequence = []
def db(t, p):
if t > n:
if n % p == 0:
sequence.extend(a[1:p + 1])
else:
a[t] = a[t - p]
db(t + 1, p)
for j in range(a[t - p] + 1, k):
a[t] = j
db(t + 1, t)
db(1, 1)
return "".join(alphabet[i] for i in sequence)
if __name__ == "__main__":
DB = de_bruijn(3, 3)
DB_list = list(DB)
print(DB_list)
x = 912
y = 1140
c = Image.new("RGB", (x, y))
skip = False
i = 0
while i < x:
for k in range(0, 15):
rgb = 255 - (5*(k - 7) ** 2)
temp = int(i / 20)
if temp < 27:
for j in range(0, y):
if i % 20 == 0:
for m in range(0, 5):
c.putpixel([i + m + 186, j], (0, 0, 0))
i = i + 5
skip = False
elif DB_list[temp] == "0":
c.putpixel([i + k + 186, j], (rgb, 0, 0))
skip = True
elif DB_list[temp] == "1":
c.putpixel([i + k + 186, j], (0, rgb, 0))
skip = True
elif DB_list[temp] == "2":
c.putpixel([i + k + 186, j], (0, 0, rgb))
skip = True
if skip:
i = i + 15
else:
i = i + 1
c.show()
c.save("structured_light .bmp")
#!/usr/bin/python
# -*- coding:utf-8 -*-
from PIL import Image
img = Image.open("structured_light .bmp")
# 获取图片尺寸的大小(600,600)
print(img.size)
# 获取图片的格式 png
print(img.format)
# 获取图片的图像类型 RGBA
print(img.mode)
# 显示图片
img.show()
# 获取某个像素位置的值
print(img.getpixel((475, 52)))
# 生成缩略图
# img.thumbnail((128, 128))
# 保存图片
# img.save('image_thumb.jpg', 'JPEG')
"""
# 获取图片指定像素点的像素
def getPngPix(pngPath = "test.png",x = 1,y = 1):
img_src = Image.open(pngPath)
img_src = img_src.convert('RGBA')
img_array = img_src.load()
data = img_array[x,y]
img_src.close()
return data
# 输入当前位置像素 (207,75,90,255)
print(getPngPix("test.png",126,52))
"""
# 判断某像素值是否在图像中
# 查找目标色值 (1,1,1,255)
# 查找目标色值 (2,2,2,255)
# 查找目标色值 (255,250,250,255)
i = 1
j = 1
result = 0
break_flag = False
width = img.size[0] # 长度
height = img.size[1] # 宽度
for i in range(0, width): # 遍历所有长度的点
for j in range(0, height): # 遍历所有宽度的点
# 每个像素点的颜色RGBA的值(r,g,b,alpha)
data = (img.getpixel((i, j))) # 打印该图片的所有点
# 判断RGBA的值
if (data[0] ==255 and data[1] == 0 and data[2] == 0):
# if (data[0]==207 and data[1]==75 and data[2]==90):
result = 1
print("图片中存在该像素!")
break_flag = True
break
if break_flag == True:
break
if break_flag == True:
break
if (result == 0):
print("图片中不存在该像素!")