In number 166 I want first entry 61 and second entry 54 which is by mistake miss typed
~/
Yes edit the the image and make corrections
~/attachments/7081d679-af40-4263-b6d7-632ec7d9dac9
mkdir -p "/tmp/workspace/image-correction/output" && python - <<'PY' from PIL import Image im=Image.open('/tmp/workspace/attachments/7081d679-af40-4263-b6d7-632ec7d9dac9/1000072741.png') print(im.size) # save a crop for coordinate verification im.crop((935,500,1135,570)).save('/tmp/workspace/image-correction/output/row166_before.png') PY
python - <<'PY' import pytesseract from PIL import Image p='/tmp/workspace/attachments/7081d679-af40-4263-b6d7-632ec7d9dac9/1000072741.png' im=Image.open(p) d=pytesseract.image_to_data(im, output_type=pytesseract.Output.DICT, config='--psm 6') for i,t in enumerate(d['text']): if t.strip() and 470 < d['top'][i] < 650: print(repr(t),d['left'][i],d['top'][i],d['width'][i],d['height'][i]) PY
python - <<'PY' import cv2 from PIL import Image, ImageDraw, ImageFont src='/tmp/workspace/attachments/7081d679-af40-4263-b6d7-632ec7d9dac9/1000072741.png' out='/tmp/workspace/image-correction/output/corrected_entry_166.png' im=cv2.imread(src) # Row 166 is between the horizontal rules at approximately y=520 and y=565. # Remove only the erroneous right-hand-cell value, leaving gridlines intact. mask=__import__('numpy').zeros(im.shape[:2], dtype='uint8') cv2.rectangle(mask,(1015,527),(1054,553),255,-1) fixed=cv2.inpaint(im,mask,5,cv2.INPAINT_TELEA) rgb=cv2.cvtColor(fixed,cv2.COLOR_BGR2RGB) pil=Image.fromarray(rgb) draw=ImageDraw.Draw(pil) # Use a bold sans font close to the original printed numerals. font=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',20) text='54' # Center it within the original numeral position in the row. b=draw.textbbox((0,0),text,font=font) w,h=b[2]-b[0],b[3]-b[1] draw.text((1034-w/2,541-h/2-2),text,font=font,fill=(28,28,28)) pil.save(out,quality=95) # visible verification crop pil.crop((970,515,1135,570)).resize((660,220)).save('/tmp/workspace/image-correction/output/verification_crop.png') print(out) PY
fc-match DejaVuSans-Bold.ttf | sed -n '1p' && python - <<'PY' from PIL import Image im=Image.open('/tmp/workspace/attachments/7081d679-af40-4263-b6d7-632ec7d9dac9/1000072741.png') print(im.mode, im.size) PY
fc-match -f '%{file}\n' 'Noto Sans:style=Bold' | sed -n '1p'
python - <<'PY' import cv2, numpy as np from PIL import Image, ImageDraw, ImageFont src='/tmp/workspace/attachments/7081d679-af40-4263-b6d7-632ec7d9dac9/1000072741.png' out='/tmp/workspace/image-correction/output/corrected_entry_166.png' im=cv2.imread(src) # The erroneous '94' lies in row 166 in the rightmost numerical column. mask=np.zeros(im.shape[:2], dtype=np.uint8) cv2.rectangle(mask,(1011,526),(1058,555),255,-1) fixed=cv2.inpaint(im,mask,5,cv2.INPAINT_TELEA) pil=Image.fromarray(cv2.cvtColor(fixed,cv2.COLOR_BGR2RGB)) d=ImageDraw.Draw(pil) font=ImageFont.truetype('/usr/share/fonts/google-noto-vf/NotoSans[wght].ttf',20) text='54' box=d.textbbox((0,0), text, font=font, stroke_width=0) w,h=box[2]-box[0], box[3]-box[1] # Position and weight matched to the original printed digits. d.text((1034-w/2,540-h/2-1),text,font=font,fill=(29,29,29)) pil.save(out, quality=95) pil.crop((980,512,1125,572)).resize((725,300)).save('/tmp/workspace/image-correction/output/verification_crop.png',quality=95) print(out) PY
corrected entry 166
Image · PNG