Monika Choudhary
Monika Choudhary

Reputation: 11

How to split scanned image into three separate images?

Input image given here

I have a scanned image of doctor's prescription, and I want to use python code to split the image into three separate sections, as shown in image below.

Cropped1 image
Cropped2 image
Cropped3 image

What I have so far:

input_1 = ("H:/CDAC/Spyder_projects/input_images/input16.png")

outputPath = ("H:/CDAC/Spyder_projects/output/")

im = Image.open(input_1)
x_width, y_height = im.size
split = np.int(y_height / 3)
outputFileFormat = "{0}-{1}.jpg"
baseName = "cropped_1"
for i in range(0, x_width, split):
#     x = split + i
    box = (i, 0, i + split, y_height)
    a = im.crop(box)
    a.load()
    outputName = os.path.join(outputPath, outputFileFormat.format(baseName, i + 1))
    a.save(outputName, "png")

I am not getting the desired output.

result image

The Output of OCR is given below-

[[[[0.0, 7.0], [129.0, 7.0], [129.0, 48.0], [0.0, 48.0]],
  ('Firefox', 0.9971230626106262)],
 [[[1311.0, 7.0], [2466.0, 7.0], [2466.0, 44.0], [1311.0, 44.0]],
  ('http://10.10.10.54:8084/HISCitizenServices/HISPreregistration onli.',
   0.9398969411849976)],
 [[[912.0, 186.0], [1392.0, 186.0], [1392.0, 234.0], [912.0, 234.0]],
  ('191og 19hu o1bg  hB1b', 0.6247340440750122)],
 [[[1451.0, 205.0], [1706.0, 205.0], [1706.0, 241.0], [1451.0, 241.0]],
  ('0671-2414080', 0.9173547625541687)],
 [[[406.0, 252.0], [1388.0, 252.0], [1388.0, 303.0], [406.0, 303.0]],
  ('DepartmentofHealth&FamilyWelfare', 0.9483137130737305)],
 [[[1451.0, 260.0], [2001.0, 260.0], [2001.0, 296.0], [1451.0, 296.0]],
  (' email : [email protected]', 0.9192459583282471)],
 [[[1451.0, 296.0], [1831.0, 296.0], [1831.0, 344.0], [1451.0, 344.0]],
  (' Cuttack, Odisha-753007', 0.925399124622345)],
 [[[978.0, 318.0], [1396.0, 318.0], [1396.0, 366.0], [978.0, 366.0]],
  ('(Government of Odisha)', 0.9391233921051025)],
 [[[772.0, 413.0], [1732.0, 413.0], [1732.0, 461.0], [772.0, 461.0]],
  ('SCBMedicalCollege&Hospital,Cuttack', 0.9333740472793579)],
 [[[170.0, 508.0], [668.0, 508.0], [668.0, 556.0], [170.0, 556.0]],
  ('CR No.: 211022200011481', 0.9427024126052856)],
 [[[1115.0, 512.0], [1322.0, 512.0], [1322.0, 552.0], [1115.0, 552.0]],
  ('OPD CARD', 0.9275292158126831)],
 [[[1964.0, 512.0], [2271.0, 512.0], [2271.0, 559.0], [1964.0, 559.0]],
  ('www.scbmch.in', 0.9585516452789307)],
 [[[170.0, 599.0], [583.0, 599.0], [583.0, 647.0], [170.0, 647.0]],
  ('Patient Name: Monika', 0.9691153764724731)],
 [[[1251.0, 596.0], [1576.0, 592.0], [1577.0, 643.0], [1252.0, 647.0]],
  ('Age/Sex: 28 Yr /F', 0.9872424006462097)],
 [[[174.0, 665.0], [543.0, 665.0], [543.0, 702.0], [174.0, 702.0]],
  ('D/O: Monika father', 0.89133220911026)],
 [[[1255.0, 658.0], [2034.0, 658.0], [2034.0, 705.0], [1255.0, 705.0]],
  ('Date & Time: 6-September-2022 17:28:17', 0.9425970315933228)],
 [[[166.0, 720.0], [801.0, 720.0], [801.0, 771.0], [166.0, 771.0]],
  ('Department: Cardiology(cardio 2)', 0.9899667501449585)],
 [[[1255.0, 724.0], [1857.0, 724.0], [1857.0, 771.0], [1255.0, 771.0]],
  ('Appointment Date: 16-Sep-2022', 0.9323211908340454)],
 [[[155.0, 790.0], [262.0, 790.0], [262.0, 830.0], [155.0, 830.0]],
  ('Vitals:', 0.9957624077796936)],
 [[[524.0, 793.0], [794.0, 793.0], [794.0, 830.0], [524.0, 830.0]],
  ('Chief Complaints:', 0.995205819606781)],
 [[[1983.0, 793.0], [2156.0, 793.0], [2156.0, 830.0], [1983.0, 830.0]],
  ('Diagnosis/', 0.979446530342102)],
 [[[1983.0, 841.0], [2248.0, 841.0], [2248.0, 881.0], [1983.0, 881.0]],
  ('Diagnosis(Prov):', 0.9228802919387817)],
 [[[159.0, 896.0], [229.0, 896.0], [229.0, 925.0], [159.0, 925.0]],
  ('Ht....', 0.9669005274772644)],
 [[[292.0, 892.0], [414.0, 892.0], [414.0, 928.0], [292.0, 928.0]],
  ('..(Cms)', 0.9294410943984985)],
 [[[1977.0, 924.0], [2183.0, 933.0], [2181.0, 984.0], [1974.0, 975.0]],
  ('Past History:', 0.9502112865447998)],
 [[[158.0, 953.0], [242.0, 963.0], [237.0, 999.0], [153.0, 989.0]],
  ('Wt....', 0.9631617069244385)],
 [[[284.0, 961.0], [369.0, 961.0], [369.0, 1002.0], [284.0, 1002.0]],
  ('..(Kg)', 0.9298592209815979)],
 [[[1983.0, 1009.0], [2079.0, 1009.0], [2079.0, 1049.0], [1983.0, 1049.0]],
  ('Hyper', 0.998813271522522)],
 [[[159.0, 1031.0], [240.0, 1031.0], [240.0, 1071.0], [159.0, 1071.0]],
  ('Pulse', 0.9149500131607056)],
 [[[328.0, 1026.0], [419.0, 1036.0], [414.0, 1076.0], [323.0, 1066.0]],
  ('../min', 0.9576079845428467)],
 [[[1976.0, 1041.0], [2219.0, 1050.0], [2218.0, 1101.0], [1975.0, 1092.0]],
  ('Tension: (Y/N)', 0.9596797823905945)],
 [[[162.0, 1108.0], [233.0, 1108.0], [233.0, 1137.0], [162.0, 1137.0]],
  ('BP:...', 0.8659010529518127)],
 [[[284.0, 1108.0], [421.0, 1108.0], [421.0, 1148.0], [284.0, 1148.0]],
  ('..mm/Hg', 0.8771561980247498)],
 [[[1983.0, 1126.0], [2230.0, 1126.0], [2230.0, 1166.0], [1983.0, 1166.0]],
  ('Diabetes: (Y/N)', 0.9567031860351562)],
 [[[159.0, 1177.0], [255.0, 1177.0], [255.0, 1217.0], [159.0, 1217.0]],
  ('Temp:', 0.9742925763130188)],
 [[[1986.0, 1199.0], [2189.0, 1199.0], [2189.0, 1239.0], [1986.0, 1239.0]],
  ('COPD: (Y/N)', 0.9035886526107788)],
 [[[162.0, 1254.0], [214.0, 1254.0], [214.0, 1283.0], [162.0, 1283.0]],
  ('RR:', 0.9956105351448059)],
 [[[303.0, 1254.0], [377.0, 1254.0], [377.0, 1283.0], [303.0, 1283.0]],
  ('/min', 0.9487172365188599)],
 [[[1983.0, 1272.0], [2245.0, 1272.0], [2245.0, 1312.0], [1983.0, 1312.0]],
  ('Oncology: (Y/N)', 0.9581263661384583)],
 [[[159.0, 1320.0], [266.0, 1320.0], [266.0, 1356.0], [159.0, 1356.0]],
  ('SPO2:..', 0.8974727988243103)],
 [[[1983.0, 1341.0], [2171.0, 1341.0], [2171.0, 1382.0], [1983.0, 1382.0]],
  ('CAD: (Y/N)', 0.9229832887649536)],
 [[[155.0, 1393.0], [384.0, 1393.0], [384.0, 1429.0], [155.0, 1429.0]],
  ('Investigations:', 0.9258691072463989)],
 [[[1976.0, 1403.0], [2275.0, 1407.0], [2274.0, 1459.0], [1975.0, 1455.0]],
  ('Tuberculosis: (Y/N)', 0.9428665041923523)],
 [[[521.0, 1436.0], [727.0, 1436.0], [727.0, 1473.0], [521.0, 1473.0]],
  ('Examination:', 0.993964433670044)],
 [[[1976.0, 1472.0], [2227.0, 1481.0], [2225.0, 1532.0], [1975.0, 1524.0]],
  ('Thyroid: (Y/N)', 0.9343023300170898)],
 [[[159.0, 1491.0], [362.0, 1491.0], [362.0, 1528.0], [159.0, 1528.0]],
  ('CBC/LFT/KFT', 0.9619587063789368)],
 [[[150.0, 1562.0], [252.0, 1552.0], [256.0, 1596.0], [154.0, 1606.0]],
  ('Blood', 0.9827973246574402)],
 [[[1983.0, 1561.0], [2219.0, 1561.0], [2219.0, 1601.0], [1983.0, 1601.0]],
  ('Surgery: (Y/N)', 0.9660006761550903)],
 [[[155.0, 1608.0], [377.0, 1608.0], [377.0, 1648.0], [155.0, 1648.0]],
  ('Sugar(R/F/PP)', 0.9385057091712952)],
 [[[1986.0, 1630.0], [2193.0, 1630.0], [2193.0, 1670.0], [1986.0, 1670.0]],
  ('Other: (Y/N)', 0.9098208546638489)],
 [[[155.0, 1681.0], [347.0, 1681.0], [347.0, 1718.0], [155.0, 1718.0]],
  ('HB/TLC/DLC', 0.9457067251205444)],
 [[[159.0, 1751.0], [391.0, 1751.0], [391.0, 1791.0], [159.0, 1791.0]],
  ('ESR/Bood Urea', 0.9110761284828186)],
 [[[159.0, 1820.0], [436.0, 1820.0], [436.0, 1861.0], [159.0, 1861.0]],
  ('S.Creatinine/S Uric', 0.9263418316841125)],
 [[[151.0, 1868.0], [233.0, 1868.0], [233.0, 1908.0], [151.0, 1908.0]],
  ('Acid', 0.7817456126213074)],
 [[[155.0, 1945.0], [391.0, 1945.0], [391.0, 1981.0], [155.0, 1981.0]],
  ('Serum Bilirubin', 0.9463291764259338)],
 [[[155.0, 2018.0], [358.0, 2018.0], [358.0, 2054.0], [155.0, 2054.0]],
  ('SGOPT/SGPT', 0.9776813387870789)],
 [[[524.0, 2076.0], [764.0, 2076.0], [764.0, 2116.0], [524.0, 2116.0]],
  ('Treatment(Rx):', 0.9324403405189514)],
 [[[159.0, 2087.0], [343.0, 2087.0], [343.0, 2124.0], [159.0, 2124.0]],
  ('Lipid Profile', 0.9448953866958618)],
 [[[153.0, 2145.0], [359.0, 2154.0], [357.0, 2205.0], [151.0, 2196.0]],
  ('Blood Group', 0.9916942715644836)],
 [[[159.0, 2230.0], [318.0, 2230.0], [318.0, 2270.0], [159.0, 2270.0]],
  ('T3,T4,TSH', 0.9597682952880859)],
 [[[155.0, 2303.0], [318.0, 2303.0], [318.0, 2339.0], [155.0, 2339.0]],
  ('Urine R/M', 0.8282328248023987)],
 [[[156.0, 2368.0], [388.0, 2372.0], [387.0, 2413.0], [155.0, 2409.0]],
  ('X-Ray Chest PA', 0.8860154151916504)],
 [[[155.0, 2411.0], [242.0, 2421.0], [236.0, 2465.0], [150.0, 2455.0]],
  ('View', 0.9621436595916748)],
 [[[159.0, 2493.0], [377.0, 2493.0], [377.0, 2533.0], [159.0, 2533.0]],
  ('USG Abdomen', 0.9302940368652344)],
 [[[159.0, 2566.0], [380.0, 2566.0], [380.0, 2603.0], [159.0, 2603.0]],
  ('TMT/Echo,ECG', 0.9976296424865723)],
 [[[151.0, 2632.0], [270.0, 2632.0], [270.0, 2683.0], [151.0, 2683.0]],
  ('Others', 0.9317743182182312)],
 [[[1251.0, 2690.0], [1790.0, 2686.0], [1791.0, 2727.0], [1252.0, 2731.0]],
  ('Signature & Full Name of the Doctor', 0.9141523241996765)],
 [[[284.0, 2800.0], [417.0, 2800.0], [417.0, 2840.0], [284.0, 2840.0]],
  ('OSMC', 0.9636073112487793)],
 [[[233.0, 2935.0], [1802.0, 2935.0], [1802.0, 2972.0], [233.0, 2972.0]],
  ('. This OPD card is generated through online, therefore it needs validation from Doctors Stamp & Signature',
   0.9333396553993225)],
 [[[229.0, 2983.0], [1281.0, 2983.0], [1281.0, 3019.0], [229.0, 3019.0]],
  ('. This OPD card not valid for Casualty Services or Medico Legal Purpose',
   0.9448567628860474)],
 [[[233.0, 3030.0], [775.0, 3030.0], [775.0, 3067.0], [233.0, 3067.0]],
  ('. This online ticket is valid for 7 days.', 0.9347903728485107)],
 [[[1569.0, 3078.0], [2175.0, 3078.0], [2175.0, 3114.0], [1569.0, 3114.0]],
  ('counters. But patient will wait in queue at', 0.9436649680137634)],
 [[[255.0, 3122.0], [1001.0, 3122.0], [1001.0, 3169.0], [255.0, 3169.0]],
  ("respective Departments for Doctor's Consultation.", 0.9697057008743286)],
 [[[225.0, 3169.0], [1100.0, 3169.0], [1100.0, 3217.0], [225.0, 3217.0]],
  (': Doctor Consultation time (OP Timing) 9:00am to 5:00pm',
   0.9160460829734802)],
 [[[620.0, 3290.0], [1820.0, 3286.0], [1820.0, 3348.0], [620.0, 3352.0]],
  ('Super speciality & Cardiology OPD closed on Sundays', 0.9343777298927307)],
 [[[4.0, 3461.0], [96.0, 3461.0], [96.0, 3505.0], [4.0, 3505.0]],
  ('1 of 1', 0.9663963913917542)],
 [[[2108.0, 3462.0], [2425.0, 3458.0], [2426.0, 3498.0], [2108.0, 3502.0]],
  ('06-09-2022.05:28', 0.9239188432693481)]]

Upvotes: 0

Views: 226

Answers (1)

Sadra TMH
Sadra TMH

Reputation: 66

You can Use PIL library to crop the image. I suggest you hard code the dimensions (if the input image will not change in size). You can use this format:

from PIL import Image

main_image = Image.open(PATH) # open the image

# Define crop size for images
crop1_size = (495, 780, 1949, 1400)
crop2_size = (495,1400, 1949, 1995)
crop3_size = (495, 2050, 1949, 2600)

# Apply crop and save them
main_image.crop(crop1_size).save('cropped1.jpg')
main_image.crop(crop2_size).save('cropped2.jpg')
main_image.crop(crop3_size).save('cropped3.jpg')

Upvotes: 0

Related Questions