我能够使用pil库中的作物分别裁剪图像,但是我不确定如何从图像中提取多个单元格的坐标数据,以及如何从图像中提取该roi。
def func3():
try:
for i in range():
for j in range():
do_something()
except:
print(error)
def func4():
for i in range():
for j in range():
try:
do_something()
except:
print(error)
在上面的代码中,我手动设置了坐标,但是我想编写一个程序,在该程序中,从csv文件中提取坐标,并根据图像中的roi进行裁剪。
这是csv文件内容的示例-
import glob,os,sys
from PIL import Image
class ROIExtraction:
def readImages(inputFolder):
ext = ['.png', '.jpg', '.gif', '.jpeg', '.tif', '.tiff']
files = []
path = inputFolder + "/*.*"
files = glob.glob(path)
imageFiles=[]
for i in files:
exten=os.path.splitext(i)[1]
if exten in ext:
imageFiles.append(i)
return imageFiles
def processRoi(imageFiles):
for imagePath in imageFiles:
img_name = os.path.splitext(os.path.basename(imagePath))[0]
output_folder = sys.argv[2]+'/'+img_name+'/'
os.makedirs(output_folder, exist_ok=True)
# import image
image = Image.open(imagePath)
img2 = image.crop((1385,45,2256,149))
img2.show()
img2.save(output_folder+'{}.png')
imageFiles = ROIExtraction.readImages(sys.argv[1])
ROIExtraction.processRoi(imageFiles)
答案 0 :(得分:0)
path ="csv.csv"
file = open(path)
for line in file.readlines():
tmp = line.split("|")
name = tmp[0]
cord = tmp[3]
all_snp_cord = cord.split("-")
for snp_cord in all_snp_cord:
snippet = snp_cord.split(",")
#do somthing with cordinats