赞
踩
目录
你一定尝试过了:
1 更改setting 选项 2 # -*- coding: utf-8 -*- 3 .encode( ) ....... 的方案了吧,然后,没好使。。。
cv2.imwrite(filename, img)
print 出 filename 明明是正常的中文,可是输出的文件名却乱码?
前方高能,终于找到解决方法,开森
- #cv2.imwrite(filename, img) 改为下句
-
- cv2.imencode('.jpg', img)[1].tofile(filename)
哦吼完美!
下面再来解决读取中文路径:
img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1)
cv2.puttext 是不可以的。
- r1= ['黑', 'A', '0', 'S', 'K', '0', '5']
- r2= ['黑', 'A', '0', 'S', 'K', '0', '5']
- r3= ['黑', 'A', '0', 'S', 'K', '0', '5']
- img=cv2.resize(img,(410,90))
- imgh, imgw, _ = img.shape
- back = 255*np.ones((2*imgh, imgw, 3), np.uint8)
- back[0:imgh, 0:imgw ] = img
- img_PIL = Image.fromarray(cv2.cvtColor(back, cv2.COLOR_BGR2RGB))
- draw = ImageDraw.Draw(img_PIL)
- for i in range(len(r0)):
- draw.text((80+40*i, 115), ''.join(r0[i]), (0, 70, 255), ImageFont.truetype("simhei.ttf", 22, encoding="utf-8"))
- for i in range(len(r1)):
- draw.text((80+40*i, 135), ''.join(r1[i]), (0, 70, 255), ImageFont.truetype("simhei.ttf", 22, encoding="utf-8"))
- for i in range(len(r2)):
- draw.text((80+40*i, 155), ''.join(r2[i]),(0, 70, 255), ImageFont.truetype("simhei.ttf", 22, encoding="utf-8"))
- for i in range(len(res)):
- draw.text((80+40*i, 95), ''.join(res[i]), (0, 0, 0), ImageFont.truetype("simhei.ttf", 22, encoding="utf-8"))
- back = cv2.cvtColor(np.asarray(img_PIL), cv2.COLOR_RGB2BGR)
-
- cv2.imencode('.jpg', back)[1].tofile("./k/"+''.join(res)+"_{}.jpg".format(index))
- index+=1
- cv2.imshow("T",back)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
灰度世界(白平衡)
- def grey_world(nimg):
- nimg = nimg.transpose(2, 0, 1).astype(np.uint32)
- avgB = np.average(nimg[0])
- avgG = np.average(nimg[1])
- avgR = np.average(nimg[2])
- avg = (avgB + avgG + avgR) / 3
- nimg[0] = np.minimum(nimg[0] * (avg / avgB), 255)
- nimg[1] = np.minimum(nimg[1] * (avg / avgG), 255)
- nimg[2] = np.minimum(nimg[2] * (avg / avgR), 255)
- return nimg.transpose(1, 2, 0).astype(np.uint8)
'运行
- '''
- 绘制BGR彩图的统计直方图
- '''
- from matplotlib import pyplot as plt
- import numpy as np
- import cv2
- import os
- path = "pp"
- cc = 0
- for root, dirs, files in os.walk(path):
- for file in files:
- if file[-5] is 's':
- continue
- # 读入图片
- img = cv2.imdecode(np.fromfile(path + "/" + file, dtype=np.uint8), cv2.IMREAD_COLOR)
- if img is None:
- print("图片读入失败, 请检查图片路径及文件名")
- exit()
-
- # 创建画布
- fig, ax = plt.subplots()
-
- # Matplotlib预设的颜色字符
- bgrColor = ('b', 'g', 'r')
-
- # 统计窗口间隔 , 设置小了锯齿状较为明显 最小为1 最好可以被256整除
- bin_win = 16
- # 设定统计窗口bins的总数
- bin_num = int(256 / bin_win)
-
-
- for cidx, color in enumerate(bgrColor):
- # cidx channel 序号
- # color r / g / b
- cHist = cv2.calcHist([img], [cidx], None, [bin_num], [0, 256])
- # 绘制折线图
- ax.plot(cHist, color=color)
- #绘制线段
-
- # 控制画布的窗口x坐标的稀疏程度. 最密集就设定xticks_win=1
- xticks_win = 4
- # 设定画布的范围
- ax.set_xlim([0, bin_num])
- # 设定x轴方向标注的位置
- ax.set_xticks(np.arange(0, bin_num, xticks_win))
- # 设定x轴方向标注的内容
- ax.set_xticklabels(list(range(0, 256, bin_win * xticks_win)), rotation=45)
-
- # 显示画面
- plt.show()
- #plt.savefig("p2/" + file[:-4] + "s.jpg")
- #cv2.imwrite("p2/" + file,img)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- x=[0,255]
- y=[30,30]
- ax.plot(x,y, color=color)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。