0

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|?
James Z
  • 12,209
  • 10
  • 24
  • 44
Aks
  • 33
  • 1
  • 8
  • Can you explain the data in csv file? Do you want to use SERIAL_NO for crop or the others also? – Garvita Tiwari Apr 26 '19 at 09:36
  • @GarvitaTiwari here basically SERIAL_NO,CUST_ID is going to be file name of cropped image ROI coordinates which is 1385,45,871,104(i.e. x,y,w,h) other than that everything is useless one have to ignore those.which basically means we are going to use first and third columns only. – Aks Apr 26 '19 at 10:27
  • @GarvitaTiwari In Order to explain problem more clearly i have specified the problem which i am facing more clearly.... •Crop the snippets - take the ROI from csv file •save all the snippets in respective folders •Draw all the region coordinates available in csv file and save the file with naming conventions. Field name as the file name . Field name is available in respective csv file. – Aks Apr 26 '19 at 11:00
  • @GarvitaTiwari ???????????Any Update...... – Aks Apr 29 '19 at 11:54

1 Answers1

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
trigonom
  • 528
  • 4
  • 9