I have image which comprises of several cells (like any general form). I was provided with coordinates in csv file which have roi of part of image which i need to crop and save it as a new file with the image file name as folder name and the coordinate cell name as image name in the image folder.
I was able to crop image individually using crop from pil library but i am not sure how to extract coordinate data of several cells from image and on the basis of that crop roi from image.
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)
In the above code I had put coordinates manually but i want to write a program where the coordinates are extracted from csv file and on the basis of that cropping of roi from image is done.
here is the example of csv file content-
SERIAL_NO|N|1385,45,871,104|1|?
CUST_ID|N|1704,211,552,71|1|?
PROD_TYPE|A|367,286,1167,74|1|?
BRANCH_CODE|N|1892,429,254,74|1|?
CONSTITUION|C|279,1355,85,62-539,1355,75,59-757,1352,72,62-884,1352,52,55-998,1352,68,55-1310,1352,68,49-1596,1349,65,55-1762,1352,68,49-1905,1352,69,52-2113,1352,62,49-78,1423,61,49-282,1410,78,65-432,1417,68,58-656,1417,78,55-969,1414,71,61-1222,1414,72,55-1391,1414,78,55|1|Sole proprietor?Partnership?LLP?HUF?Private Ltd Company?Public Ltd Company?Society?Trust?NGO/NPO?SHG?Association?Club?University?Government Body?Financial Inst?Bank?JLG?
ACTIVITY|C|221,1498,71,52-400,1498,71,52-552,1495,59,45-702,1498,65,49-881,1495,62,49-1043,1495,69,52-1252,1495,58,42-1473,1495,75,49-1671,1495,65,52-1935,1485,74,59-224,1563,65,49-734,1553,59,52-1056,1557,62,45|1|Agri?Mfg?Trade?Finance?Bank?Transport?Services?Govt?Real Estate?Stock Broker?Jewels/Gems/Precious Metal dealer?Money Services?Others (specify)?
NAME_AUTH_SIG1|A|97,1713,1327,65|1|?