python图片取模心得,源码附送——碰瓷PCtoLCD2002-编程之家

print()技巧

print()自带回车,加上 end=''取消回车

print('1')
print('2')print('1',end='')
print('2')结果:
1
2
12Process finished with exit code 0

直接打印

python图片取模心得,源码附送——碰瓷PCtoLCD2002-编程之家

图像模式:print(img_file.mode)

modes Description
1 1位像素,黑白图像,存成8位像素     {0,255}
L 8位像素,黑白    {0,1,2,….,255}
P 9位像素,使用调色板映射到任何其他模式
RGB 3*8位像素,真彩
RGBA 4*8位像素,真彩+透明通道
CMYK 4*8位像素,印刷四色模式或彩色印刷模式
YCbCr 3*8位像素,色彩视频格式
I 32位整型像素
F 33位浮点型像素

        对于彩色图像,不管其图像格式是PNG,还是BMP,或者JPG,在PIL中,使用Image模块的open()函数打开后,返回的图像对象的模式都是“RGB”。而对于灰度图像,不管其图像格式是PNG,还是BMP,或者JPG,打开后,其模式为“L”。

from PIL import Image#调用库,包含图像类img1=Image.open('晴.png')#打开彩色图片
print(img1.format,img1.mode,img1.size,img1.palette)
img2=Image.open('huidu.jpg')#打开灰色图片
print(img2.format,img2.mode,img2.size,img2.palette)结果:
PNG RGBA (48, 48) None
JPEG L (48, 48) NoneProcess finished with exit code 0

模式转换:  convert('L')

img_L=img1.convert('L')

常用模板:

#函数
Image.open('晴.png')
Image.fromarray(255 * array_np_16)
#方法
img1.show()
img1.save('生成.bmp')
img1.resize((PIX_W, PIX_H))
img_L=img1.convert('L')

1、彩色图像  ——-》   (0~255)或者(False/True)的数组:对数组 img_array[i][k] 访问

from PIL import Image#调用库,包含图像类
import numpy as np
print("结果:>>")
# iiiiiiiiiiiiiiiiuuiiiiiiiiiiiii
img1=Image.open('晴.png')#打开彩色图片
img_L=img1.convert('L')#0_255的图
#img_L=img1.convert('1')#0/1的图
img_array=np.array(img_L)

2、(0~255)或者(False/True)的数组  ——-》彩色图像:注意int(True)=1,但是转化为255最白色

new_im = Image.fromarray(img_array)#注意int(True)=1,但是转化为255最白色

源码

源码使用说明:

基本上输入原图的路径就会有正常输出,

将会得到灰度图、裁剪后的二值化图、根据数组的检查图(校验),在控制台输出数组(这是最有用的)

const unsigned char BMP_mai_32X30[] U8X8_PROGMEM= {
0x00,0x7e,0x00,0x00,0x80,0xff,0x00,0x00,0xe0,0xff,0x03,0x00,0xf0,0x80,0x07,0x00,
0x78,0x00,0x0f,0x00,0x3c,0x00,0x1c,0x00,0x1c,0x00,0xf8,0x00,0x0e,0x00,0xf8,0x07,
0x06,0x00,0xf0,0x0f,0x07,0x00,0x10,0x1e,0x07,0x00,0x00,0x38,0x07,0x00,0x00,0x70,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0xff,0xff,0x7f,
0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xfe,0xff,0xff,0x7f,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0x7f,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0x7f,
0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0x7f,};//arduino图片数组

  1. 支持png透明填充
  2. 支持0-255阈值设定
  3. 支持反转
  4. 支持大小缩放
  5. 支持矩形裁剪
  6. 有两种扫描方式:水平扫描和垂直扫描,对应变量MODE='W'/'H'
  7. 自定义输出格式

聪明的小机灵鬼还可以自行封装处理多张图片,甚至是视频哦

python图片取模心得,源码附送——碰瓷PCtoLCD2002-编程之家

#time:2022/3/12  17:13
#author:
#
from PIL import Image
import numpy as np
from typing import List
#用户操作区###############################
IMG_PATH='雾.png'
YU_ZHI=20          #阈值越大亮度越低
FAN_ZHUAN=1         #黑白反转
#图像大小,裁剪前
PIX_H=30            #图像高
PIX_W=32            #图像宽
#裁剪属性
W_START=0           #裁剪x开始位置
W_END= PIX_W - 0       #裁剪x结束位置
H_START=0           #裁剪y开始位置
H_END= PIX_H - 0       #裁剪y结束位置
#输出格式
MODE='W'            #H/W,纵向扫描/横向扫描
NAME='mai'          #const变量命名
NUM_W=16            #每行数据个数def Print_C51(C51_list,name,height_t,width_t):count = 0print('const unsigned char BMP_' + name + '_' + str(width_t) + 'X' + str(height_t) + '[] U8X8_PROGMEM= { ')for i in C51_list:print('0x{:02x},'.format(i),end='')count += 1if (count % NUM_W == 0):print('')print('};\n')
def CkeckBMP(bmp_list,width_t,height_t):if MODE=='W':if width_t % 8 != 0:width_t = width_t + (8 - width_t % 8)ck_array = np.empty(shape=(height_t, width_t), dtype=np.uint8)width_t=int(width_t/8)for i in range(0,height_t):for k in range(0,width_t):for p in range(0,8):ck_array[i][k*8+p]=bmp_list[i*width_t+k]%2bmp_list[i * width_t + k] /= 2elif MODE=='H':if height_t%8!=0:height_t = height_t + (8 - height_t % 8)ck_array = np.empty(shape=(height_t, width_t), dtype=np.uint8)height_t = int(height_t /8)print(height_t)for i in range(0,height_t):for k in range(0,width_t):for p in range(0,8):ck_array[i*8+p][k]=bmp_list[i*width_t+k]%2bmp_list[i * width_t + k] /= 2ck_img = Image.fromarray(255 * ck_array)ck_img.save('CkeckBMP.bmp')  # 预览def jinzhi_16(pp:List[int]):temp111=np.uint8(0);for i in range(7,-1,-1):temp111=temp111<<1if i<(len(pp)):temp111+=pp[i]else:temp111+=1-FAN_ZHUAN# print(temp111)return np.uint8(temp111)# 创建大小缩放图片
img_file=Image.open(IMG_PATH)
if img_file.format=='PNG':for yh in range(img_file.size[1]):for xw in range(img_file.size[0]):dot=(xw,yh)color_d=img_file.getpixel(dot)if(color_d[3]==0):color_d=(255,255,255,255)img_file.putpixel(dot,color_d)
img_file=img_file.resize((PIX_W, PIX_H))
print('img_file:    ',img_file.format, img_file.mode, img_file.size, img_file.palette)
#创建灰度图
img_L=img_file.convert('L')
# img_L.show()
img_L.save('huidu.bmp')
print('img_L:       ',img_L.mode,img_L.size)
#创建图像转数组
img_array=np.array(img_L)
print(img_array.shape, W_START, W_END, H_START, H_END)
img_array= img_array[H_START:H_END, W_START:W_END]
print(img_array.shape)
#创建%8扩容存储二值化的数组np.empty
height,width=img_array.shape
height_2=height%8
height_8=int(height/8)+(1 if height_2!=0 else 0)
width_2=width%8
width_8=int(width/8)+(1 if width_2!=0 else 0)#二值化数组,反转
for i in range(0, height):for k in range(0,width):if(img_array[i][k]>YU_ZHI):img_array[i][k]= 1 - FAN_ZHUANelse:img_array[i][k]=FAN_ZHUANimg_array[i][k] = img_array[i][k]print(img_array[i][k], end='')print('')new_img = Image.fromarray(255 * img_array)
new_img.save('生成.bmp')#预览#输出C51格式
out_list=[]
if(MODE=='W'):for i in range(0, height):# print(i)for k in range(0,width_8):out_list.append(int(jinzhi_16(img_array[i][k * 8:k * 8 + 8 if k * 8 + 8<width else width] )))
elif(MODE=='H'):for i in range(0,height_8):for k in range(0,width):fw_list = []for p in range(i*8,(i*8+8 if i*8+8<height else height)):fw_list.append(img_array[p][k])out_list.append(int(jinzhi_16(fw_list)))Print_C51(out_list,NAME,height,width)
CkeckBMP(out_list,width,height)img_file.close()