直接用open打开后read读取
A =open('testbmp','rb')B=open('test2bmp','w')
c = Aread()
Bwrite(c)
Aclose()
Bclose()
Python可以使用队列来存储,这样可以实现的有序存储和访问。可以使用Python的Queue模块来实现,它提供了一系列的队列 *** 作,如入队、出队、获取队列长度等。此外,还可以使用Python的PIL模块来读取,将转换为字节流,然后将字节流存入队列中。
用image模块更直接,可以用getpixel获得像素值,给你个例子吧。
01#!/usr/bin/env python
02import Image
03import sys
04im = Imageopen(sysargv[1])
05width = imsize[0]
06height = imsize[1]
07print "/ width:%d /"%(width)
08print "/ height:%d /"%(height)
09count = 0
10for h in range(0, height):
11 for w in range(0, width):
12 pixel = imgetpixel((w, h))
13 for i in range(0,3):
14 count = (count+1)%16
15 if (count == 0):
16 print "0x%02x,/n"%(pixel[i]),
17 else:
18 print "0x%02x,"%(pixel[i]),
通过opencv,识别出两张不同部分。俗称大家一起来找茬
效果图
源代码 image_detect_03py
import cv2import numpy as np
from matplotlib import pyplot as plt
import argparse
def matchAB(fileA, fileB):
# 读取图像数据
imgA = cv2imread(fileA)
imgB = cv2imread(fileB)
# 转换成灰色
grayA = cv2cvtColor(imgA, cv2COLOR_BGR2GRAY)
grayB = cv2cvtColor(imgB, cv2COLOR_BGR2GRAY)
# 获取A的大小
height, width = grayAshape
# 取局部图像,寻找匹配位置
result_window = npzeros((height, width), dtype=imgAdtype)
for start_y in range(0, height-100, 10):
for start_x in range(0, width-100, 10):
window = grayA[start_y:start_y+100, start_x:start_x+100]
match = cv2matchTemplate(grayB, window, cv2TM_CCOEFF_NORMED)
_, _, _, max_loc = cv2minMaxLoc(match)
matched_window = grayB[max_loc[1]:max_loc[1]+100, max_loc[0]:max_loc[0]+100]
result = cv2absdiff(window, matched_window)
result_window[start_y:start_y+100, start_x:start_x+100] = result
# 用四边形圈出不同部分
_, result_window_bin = cv2threshold(result_window, 30, 255, cv2THRESH_BINARY)
_, contours, _ = cv2findContours(result_window_bin, cv2RETR_EXTERNAL, cv2CHAIN_APPROX_SIMPLE)
imgC = imgAcopy()
for contour in contours:
min = npnanmin(contour, 0)
max = npnanmax(contour, 0)
loc1 = (min[0][0], min[0][1])
loc2 = (max[0][0], max[0][1])
cv2rectangle(imgC, loc1, loc2, 255, 2)
pltsubplot(1, 3, 1), pltimshow(cv2cvtColor(imgA, cv2COLOR_BGR2RGB)), plttitle('A'), pltxticks([]), pltyticks([])
pltsubplot(1, 3, 2), pltimshow(cv2cvtColor(imgB, cv2COLOR_BGR2RGB)), plttitle('B'), pltxticks([]), pltyticks([])
pltsubplot(1, 3, 3), pltimshow(cv2cvtColor(imgC, cv2COLOR_BGR2RGB)), plttitle('Answer'), pltxticks([]), pltyticks([])
pltshow()
if __name__ == '__main__':
parser = argparseArgumentParser()
parseradd_argument(
'--source_image',
type=str,
default='img/image01-0png',
help='source image'
)
parseradd_argument(
'--target_image',
type=str,
default='img/image01-1png',
help='target image'
)
FLAGS, unparsed = parserparse_known_args()
matchAB(FLAGSsource_image, FLAGStarget_image)
以上就是关于如何在python中读取bmp格式图片文件全部的内容,包括:如何在python中读取bmp格式图片文件、python使用队列存图片、使用python PIL处理图片。怎么获取图片的像素数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)