async def imgscan(event): ok = await event.get_reply_message() if not (ok and (ok.media)): await event.eor("`Reply The pdf u Want to Download..`") return ultt = await ok.download_media() if not ultt.endswith(("png", "jpg", "jpeg", "webp")): await event.eor("`Reply to a Image only...`") os.remove(ultt) return xx = await event.eor(get_string("com_1")) image = cv2.imread(ultt) original_image = image.copy() ratio = image.shape[0] / 500.0 hi, wid = image.shape[:2] ra = 500 / float(hi) dmes = (int(wid * ra), 500) image = cv2.resize(image, dmes, interpolation=cv2.INTER_AREA) image_yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV) image_y = np.zeros(image_yuv.shape[:2], np.uint8) image_y[:, :] = image_yuv[:, :, 0] image_blurred = cv2.GaussianBlur(image_y, (3, 3), 0) edges = cv2.Canny(image_blurred, 50, 200, apertureSize=3) contours, hierarchy = cv2.findContours( edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE, ) polygons = [] for cnt in contours: hull = cv2.convexHull(cnt) polygons.append( cv2.approxPolyDP(hull, 0.01 * cv2.arcLength(hull, True), False)) sortedPoly = sorted(polygons, key=cv2.contourArea, reverse=True) cv2.drawContours(image, sortedPoly[0], -1, (0, 0, 255), 5) simplified_cnt = sortedPoly[0] if len(simplified_cnt) == 4: cropped_image = four_point_transform( original_image, simplified_cnt.reshape(4, 2) * ratio, ) gray_image = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2GRAY) T = threshold_local(gray_image, 11, offset=10, method="gaussian") ok = (gray_image > T).astype("uint8") * 255 if len(simplified_cnt) != 4: ok = cv2.detailEnhance(original_image, sigma_s=10, sigma_r=0.15) cv2.imwrite("o.png", ok) image1 = Image.open("o.png") im1 = image1.convert("RGB") scann = f"Scanned {ultt.split('.')[0]}.pdf" im1.save(scann) await event.client.send_file(event.chat_id, scann, reply_to=event.reply_to_msg_id) await xx.delete() os.remove(ultt) os.remove("o.png") os.remove(scann)
async def savepdf(event): ok = await event.get_reply_message() if not (ok and (ok.media)): await eor( event, "`Reply to Images/pdf which u want to merge as a single pdf..`", ) return ultt = await ok.download_media() if ultt.endswith(("png", "jpg", "jpeg", "webp")): xx = await event.eor(get_string("com_1")) image = cv2.imread(ultt) original_image = image.copy() ratio = image.shape[0] / 500.0 h_, _v = image.shape[:2] m_ = 500 / float(h_) image = cv2.resize(image, (int(_v * m_), 500), interpolation=cv2.INTER_AREA) image_yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV) image_y = np.zeros(image_yuv.shape[:2], np.uint8) image_y[:, :] = image_yuv[:, :, 0] image_blurred = cv2.GaussianBlur(image_y, (3, 3), 0) edges = cv2.Canny(image_blurred, 50, 200, apertureSize=3) contours, hierarchy = cv2.findContours( edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE, ) polygons = [] for cnt in contours: hull = cv2.convexHull(cnt) polygons.append( cv2.approxPolyDP(hull, 0.01 * cv2.arcLength(hull, True), False), ) sortedPoly = sorted(polygons, key=cv2.contourArea, reverse=True) cv2.drawContours(image, sortedPoly[0], -1, (0, 0, 255), 5) simplified_cnt = sortedPoly[0] if len(simplified_cnt) == 4: cropped_image = four_point_transform( original_image, simplified_cnt.reshape(4, 2) * ratio, ) gray_image = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2GRAY) T = threshold_local(gray_image, 11, offset=10, method="gaussian") ok = (gray_image > T).astype("uint8") * 255 if len(simplified_cnt) != 4: ok = cv2.detailEnhance(original_image, sigma_s=10, sigma_r=0.15) cv2.imwrite("o.png", ok) image1 = Image.open("o.png") im1 = image1.convert("RGB") a = check_filename("pdf/scan.pdf") im1.save(a) await xx.edit( f"Done, Now Reply Another Image/pdf if completed then use {HNDLR}pdsend to merge nd send all as pdf", ) os.remove("o.png") elif ultt.endswith(".pdf"): a = check_filename("pdf/scan.pdf") await event.client.download_media(ok, a) await eor( event, f"Done, Now Reply Another Image/pdf if completed then use {HNDLR}pdsend to merge nd send all as pdf", ) else: await event.eor("`Reply to a Image/pdf only...`") os.remove(ultt)