diff --git a/.ipynb_checkpoints/openCV_scanner-checkpoint.ipynb b/.ipynb_checkpoints/openCV_scanner-checkpoint.ipynb index 0078e221a9b5fdb46c4f542687f327c57698b6fc..49bad8c5544aa1624eb340a518d9ebe7e59f2d60 100644 --- a/.ipynb_checkpoints/openCV_scanner-checkpoint.ipynb +++ b/.ipynb_checkpoints/openCV_scanner-checkpoint.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 176, + "execution_count": 1, "metadata": { "scrolled": true }, @@ -15,12 +15,15 @@ "import tkinter as tk\n", "from PIL import Image\n", "from PIL import ImageTk\n", + "import os\n", + "from fpdf import FPDF\n", + "from PIL import Image\n", "import os" ] }, { "cell_type": "code", - "execution_count": 177, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -37,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 178, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -57,8 +60,7 @@ " # CASE 1 - 마우스가 움직이고\n", " if event == cv2.EVENT_MOUSEMOVE: \n", " # 눌린상태일때\n", - " if mouse_is_pressing == True:\n", - " points[i][0] = x \n", + " if mouse_is_pressing == True: \n", " #마우스의 좌표와, 포인트(꼭지점) 의 거리가 가깝다면 꼭지점을 이동한다\n", " for i,point in enumerate(points):\n", " if distance((x,y), point) < 15: \n", @@ -67,7 +69,7 @@ " break \n", " # CASE 2 - 마우스 왼쪽 버튼이 눌린 상태일 때, \n", " elif event == cv2.EVENT_LBUTTONDOWN:\n", - " # 꼭짓점과의 거리가 가깝다면 mouse_is_pressing을 True로 바꾼다. ##press 를 Ture로 바꾼다.\n", + " # 꼭짓점과의 거리가 가깝다면 mouse_is_pressing을 True로 바꾼다.\n", " for point in points:\n", " if distance((x,y), point) < 10:\n", " mouse_is_pressing = True\n", @@ -80,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": 179, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -103,7 +105,7 @@ }, { "cell_type": "code", - "execution_count": 180, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -145,7 +147,7 @@ }, { "cell_type": "code", - "execution_count": 181, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -157,9 +159,9 @@ " # top left, top right, bottom right, bottom left\n", " points = sort_points(points)\n", " topLeft, topRight, bottomRight, bottomLeft = points\n", - " print(topLeft, topRight, bottomRight, bottomLeft)\n", - " print(topLeft[0] + topLeft[1], topRight[0]+topRight[1],\n", - " bottomRight[0]+bottomRight[1], bottomLeft[0]+bottomLeft[1])\n", + " #print(topLeft, topRight, bottomRight, bottomLeft)\n", + " #print(topLeft[0] + topLeft[1], topRight[0]+topRight[1],\n", + " #bottomRight[0]+bottomRight[1], bottomLeft[0]+bottomLeft[1])\n", " \n", " # 변환 후 책의 너비와 높이를 결정한다.\n", " topWidth = distance(bottomLeft, bottomRight)\n", @@ -182,7 +184,7 @@ }, { "cell_type": "code", - "execution_count": 182, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -213,7 +215,7 @@ }, { "cell_type": "code", - "execution_count": 183, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -232,11 +234,14 @@ }, { "cell_type": "code", - "execution_count": 184, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def scanning(image):\n", + " global contours\n", + " global max_index\n", + " global max_contour\n", " #grabcut , 배경 제거\n", " points = []\n", "\n", @@ -252,77 +257,45 @@ " image_mask = np.where((image_mask==2)|(image_mask==0), 0, 1).astype('uint8')\n", " image_grabcut = image*image_mask[:,:,np.newaxis]\n", "\n", - " #cv2.imshow('grabCut', image_grabcut)\n", - " #cv2.waitKey(0)\n", - " \n", " #케니에지 디텍션을 이용하여 에지 검출\n", " image_gray = cv2.cvtColor(image_grabcut, cv2.COLOR_BGR2GRAY);\n", " image_canny = cv2.Canny(image_gray, 30, 90);\n", "\n", - "\n", - " #cv2.imshow('Canny', image_canny)\n", - " #cv2.waitKey(0)\n", - "\n", " #모폴로지 클로즈 연산\n", "\n", " kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))\n", " image_canny = cv2.morphologyEx(image_canny, cv2.MORPH_CLOSE, kernel, 1)\n", - " #cv2.imwrite('./modified/' +i, image_canny)\n", - " #print(i + \"save image_canny!\")\n", - " global contours\n", - " global max_index\n", - " global max_contour\n", "\n", - " #cv2.imshow('morphology', image_canny)\n", - " #cv2.waitKey(0)\n", " # 캐니 에지 결과에서 컨투어를 검출하여 가장 큰 영역을 차지하는 컨투어를 찾는다.\n", " contours, hierarchy = cv2.findContours(image_canny, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\n", " max_index = findMaxArea(contours) \n", " \n", - " #################이부분이 잘 이해가 안감.\n", - " #print(\"contours \", contours)\n", - " #print(\"max_index = \",max_index)\n", " image_fixed = image.copy()\n", " if max_index < 0:\n", - " print(\"max_index < 0, return points \")\n", - " points = np.array([[10,10], [290, 10], [290, 390], [290, 390]])\n", + " ## 꼭짓점을 찾지 못한경우, 마우스 입력을 받기위해 임의로 네개의 꼭짓점을 이미지 가장자리로 배정한다.\n", + " points = np.array([[10,10], [width-10, 10], [width-10, height-10], [10, height-10]])\n", " return points\n", "\n", - " \n", " max_contour = contours[max_index]\n", "\n", - "\n", - " #image_contour = image.copy()\n", - " #cv2.drawContours(image_contour, [max_contour], 0, (0, 0, 255), 3)\n", - " #cv2.imshow('Contour', image_contour)\n", - " #cv2.waitKey(0)\n", - "\n", - " # approxPolyDP 함수를 사용하여 컨투어를 근사화하고 convex hull을 구한다.\n", + " ## approxPolyDP 함수를 사용하여 convex hull을 구한다.\n", " max_contour = cv2.approxPolyDP(max_contour,0.02*cv2.arcLength(max_contour,True),True)\n", " hull = cv2.convexHull(max_contour)\n", "\n", - "\n", - " #image_convexhull = image.copy()\n", - " #cv2.drawContours(image_convexhull, [hull], 0, (255,255,0), 5)\n", - " #cv2.imwrite('./modified/' +i, image_convexhull) \n", - " #print(i + \"save image_convexhull!\")\n", - " #cv2.imshow('convexHull', image_convexhull)\n", - " #cv2.waitKey(0)\n", " size = len(max_contour) \n", - " print(\"len of max_contour\" ,size)\n", - " if size == 4: # 책의 꼭지점을 구한다.\n", + " if size == 4: \n", + " ## 책의 꼭지점을 구한다.\n", " for c in hull:\n", " points.append(c[0])\n", " points = np.array(points)\n", - " #print(points)\n", - "\n", - " else: # 꼭지점이 5개 이상인 경우 경계 박스를 이용한다.\n", "\n", + " else: \n", + " ## 꼭지점이 5개 이상인 경우 경계 박스를 이용한다.\n", " rect = cv2.minAreaRect(hull)\n", " box = cv2.boxPoints(rect)\n", " points = np.int0(box.tolist())\n", "\n", - " # 검출된 꼭지점이 이미지 범위를 벗어난 경우 책 꼭지점 처리\n", + " ## 검출된 꼭지점이 이미지 범위를 벗어난 경우 책 꼭지점 처리\n", " found = False\n", " for p in points:\n", " if p[0] < 0 or p[0] > width-1 or p[1] < 0 or p[1] > height -1:\n", @@ -339,10 +312,43 @@ }, { "cell_type": "code", - "execution_count": 185, + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "#pdf로 이미지 저장\n", + "def save_pdf():\n", + " pdf = FPDF()\n", + " imagelist = [] \n", + "\n", + " # ------------- ADD ALL THE IMAGES IN A LIST ------------- #\n", + "\n", + " folder = './modified/'\n", + " for dirpath, dirnames, filenames in os.walk(folder):\n", + " for filename in [f for f in filenames if f.endswith(\".jpg\")]:\n", + " full_path = os.path.join(dirpath, filename)\n", + " imagelist.append(full_path)\n", + "\n", + " imagelist.sort() # Sort the images by name.\n", + " for i in range(0, len(imagelist)):\n", + " print(imagelist[i])\n", + "\n", + " # -------------- CONVERT TO PDF ------------ #\n", + " for image in imagelist:\n", + " pdf.add_page()\n", + " pdf.image(image, 0, 0, 210, 297) # 210 and 297 are the dimensions of an A4 size sheet.\n", + " pdf.output(folder + \"image_list\" + '.pdf', \"F\")\n", + "\n", + " print(\"PDF generated successfully!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ + "# o 버튼(마우스 사용)을 눌렀을 때, \n", "def b1event():\n", " global step\n", " if sizes == 4:\n", @@ -364,13 +370,13 @@ " \n", " cv2.imshow('input', image_result)\n", " cv2.imshow('result', image_final)\n", - " cv2.imwrite(modified_path +i, image_final)\n", - " print(i + \" i got 4 edges!!\")\n", + " cv2.imwrite(modified_path + filename, image_final)\n", + " #print(filename + \" i got 4 edges!!\")\n", " \n", " else:\n", " cv2.imshow('input', image)\n", - " cv2.imwrite(modified_path +i, image)\n", - " print(i + \" i don't got 4 edges!\")\n", + " cv2.imwrite(modified_path + filename, image)\n", + " #print(filename + \" i don't got 4 edges!\")\n", " cv2.waitKey(0)\n", " cv2.destroyAllWindows()\n", " win.destroy()" @@ -378,20 +384,24 @@ }, { "cell_type": "code", - "execution_count": 186, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ + "#x 버튼(마우스입력을 안함)을 눌렀을 때\n", "def b2event():\n", + " \n", + " ## 꼭지점 4개를 찾은경우\n", " if sizes == 4:\n", " image_final = transform(image, points)\n", " cv2.imshow('result', image_final)\n", - " cv2.imwrite(modified_path +i, image_final)\n", - " print(i + \" i got 4 edges!!/no button\")\n", + " cv2.imwrite(modified_path +filename, image_final)\n", + " #print(i + \" i got 4 edges!!/no button\")\n", + " \n", " else:\n", " cv2.imshow('input', image)\n", - " cv2.imwrite(modified_path +i, image)\n", - " print(i + \" i don't got 4 edges!/no button\")\n", + " cv2.imwrite(modified_path +filename, image)\n", + " #print(i + \" i don't got 4 edges!/no button\")\n", " cv2.waitKey(0)\n", " cv2.destroyAllWindows()\n", " win.destroy()" @@ -399,53 +409,23 @@ }, { "cell_type": "code", - "execution_count": 195, - "metadata": {}, + "execution_count": 14, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "len of max_contour 4\n", - "size 4\n", - "[20. 39.] [213. 14.] [277. 322.] [ 76. 303.]\n", - "59.0 227.0 599.0 379.0\n", - "document.jpg i got 4 edges!!\n", - "len of max_contour 4\n", - "size 4\n", - "[36. 43.] [265. 40.] [275. 377.] [ 21. 375.]\n", - "79.0 305.0 652.0 396.0\n", - "example.jpg i got 4 edges!!\n", - "len of max_contour 4\n", - "size 4\n", - "[54. 42.] [256. 57.] [266. 347.] [ 44. 349.]\n", - "96.0 313.0 613.0 393.0\n", - "example2.jpg i got 4 edges!!\n", - "len of max_contour 4\n", - "size 4\n", - "[53. 24.] [267. 58.] [267. 379.] [ 10. 318.]\n", - "77.0 325.0 646.0 328.0\n", - "example2.png i got 4 edges!!\n", - "len of max_contour 4\n", - "size 4\n", - "[43. 30.] [263. 27.] [272. 360.] [ 52. 365.]\n", - "73.0 290.0 632.0 417.0\n", - "example3.jpg.png i got 4 edges!!\n", - "len of max_contour 5\n", - "size 4\n", - "[75. 8.] [237. 11.] [229. 381.] [ 68. 377.]\n", - "83.0 248.0 610.0 445.0\n", - "receipt.jpg i got 4 edges!!\n", - "len of max_contour 4\n", - "size 4\n", - "[ 20. 100.] [152. 27.] [279. 290.] [131. 371.]\n", - "120.0 179.0 569.0 502.0\n", - "receipt2.jpg i got 4 edges!!\n", - "len of max_contour 8\n", - "size 4\n", - "[25. 73.] [208. 51.] [242. 333.] [ 60. 355.]\n", - "98.0 259.0 575.0 415.0\n", - "tilted receipt.jpg i got 4 edges!!\n" + "./modified/bookcover.jpg\n", + "./modified/example.jpg\n", + "./modified/example2.jpg\n", + "./modified/example3.jpg\n", + "./modified/framework1821.jpg\n", + "./modified/receipt.jpg\n", + "./modified/tilted_document.jpg\n", + "PDF generated successfully!\n" ] } ], @@ -455,108 +435,62 @@ "if not os.path.isdir(image_path): \n", " os.mkdir(image_path)\n", "\n", - "for i in os.listdir(image_path): \n", - " path = image_path + i \n", + "## image 폴더안에 있는 이미지파일들을 하나씩 탐색.\n", + "for filename in os.listdir(image_path): \n", + " path = image_path + filename \n", " count = len(os.listdir(image_path))\n", " \n", + " ##이미지 파일이 하나도 없는경우\n", " if count == 0:\n", " print(\"there are no images\")\n", + " \n", " image = cv2.imread(path, cv2.IMREAD_COLOR)\n", - " image=cv2.resize(image,(300,400))\n", + " \n", + " ##이미지가 너무 큰경우, 적당한 크기로 줄임\n", + " height, width, channel = image.shape\n", + " if height > 720 or width > 540:\n", + " height,width = 720,540\n", + " image=cv2.resize(image,(width,height))\n", + " #print(height, width , channel)\n", + " \n", " points = scanning(image)\n", + " \n", + " #scanning 결과 나온 contour 이미지를 만듬.\n", + " image_contour = image.copy()\n", + " cv2.drawContours(image_contour, [max_contour], 0, (0, 0, 255), 3)\n", " sizes = len(points)\n", - " print(\"size\",sizes)\n", " \n", + " #UI 창 띄우기\n", " win = tk.Tk()\n", " win.title(\"Ask\")\n", - " win.geometry(\"600x600+50+50\")\n", + " win.geometry(\"600x800+50+100\")\n", " win.resizable(False, False)\n", - "\n", " lbl = tk.Label(win, text=\"마우스 입력 O/X?\")\n", - " #lbl.grid(row=1, column=1)\n", " lbl.pack()\n", - " image_contour = image.copy()\n", - " cv2.drawContours(image_contour, [max_contour], 0, (0, 0, 255), 3)\n", " \n", + " ## contour 이미지 파일을 저장하기. 폴더가 없을경우 폴더를 만듬\n", " contour_path = './contour/'\n", " if not os.path.isdir(contour_path): \n", " os.mkdir(contour_path)\n", " \n", - " cv2.imwrite(contour_path +i, image_contour)\n", + " cv2.imwrite(contour_path + filename, image_contour)\n", "\n", - " imgtk = ImageTk.PhotoImage(file = contour_path +i, master=win)\n", + " imgtk = ImageTk.PhotoImage(file = contour_path + filename, master=win)\n", " label = tk.Label(win, image=imgtk)\n", " label.pack()\n", "\n", + " ## 최종 수정된 이미지를 저장하기. 폴더가 없을경우 폴더 생성\n", " modified_path = './modified/'\n", " if not os.path.isdir(modified_path): \n", " os.mkdir(modified_path)\n", + " \n", " b1 = tk.Button(win, text='마우스 입력O', command = b1event)\n", " b2 = tk.Button(win, text='마우스 입력X', command = b2event)\n", - " #b1.grid(row=3, column=1)\n", - " #b2.grid(row=3, column=2)\n", " b1.pack()\n", " b2.pack()\n", "\n", - " win.mainloop()" - ] - }, - { - "cell_type": "code", - "execution_count": 150, - "metadata": {}, - "outputs": [ - { - "ename": "KeyboardInterrupt", - "evalue": "Interrupted by user", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m<ipython-input-150-0a916bbbf96e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;31m# ------------- ADD ALL THE IMAGES IN A LIST ------------- #\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 18\u001b[1;33m \u001b[0mfolder\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Enter the path of the folder containing the images : '\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 19\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mdirpath\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdirnames\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfilenames\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwalk\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfolder\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mfilename\u001b[0m \u001b[1;32min\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mf\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mf\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mfilenames\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mendswith\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\".jpg\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m 858\u001b[0m \u001b[1;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 859\u001b[0m )\n\u001b[1;32m--> 860\u001b[1;33m return self._input_request(str(prompt),\n\u001b[0m\u001b[0;32m 861\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 862\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m 902\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 903\u001b[0m \u001b[1;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 904\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Interrupted by user\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 905\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 906\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Invalid Message:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user" - ] - } - ], - "source": [ - "#이미지 저장\n", - "##################아직 안합침################################\n", - "from fpdf import FPDF\n", - "from PIL import Image\n", - "import os\n", - "\n", - "pdf = FPDF()\n", - "imagelist = [] # Contains the list of all images to be converted to PDF.\n", - "\n", - "\n", - "# --------------- USER INPUT -------------------- #\n", - " \n", - "folder = \"\" # Folder containing all the images.\n", - "name = \"\" # Name of the output PDF file.\n", - "\n", - "# ------------- ADD ALL THE IMAGES IN A LIST ------------- #\n", - "\n", - "folder = input('Enter the path of the folder containing the images : ')\n", - "for dirpath, dirnames, filenames in os.walk(folder):\n", - " for filename in [f for f in filenames if f.endswith(\".jpg\")]:\n", - " full_path = os.path.join(dirpath, filename)\n", - " imagelist.append(full_path)\n", - "\n", - "imagelist.sort() # Sort the images by name.\n", - "for i in range(0, len(imagelist)):\n", - " print(imagelist[i])\n", - "\n", - "# -------------- CONVERT TO PDF ------------ #\n", - "\n", - "name = input('Enter the PDF file name : ')\n", - "for image in imagelist:\n", - " pdf.add_page()\n", - " pdf.image(image, 0, 0, 210, 297) # 210 and 297 are the dimensions of an A4 size sheet.\n", - "pdf.output(folder + name + '.pdf', \"F\")\n", - " \n", - "print(\"PDF generated successfully!\")" + " win.mainloop()\n", + "save_pdf()" ] }, { diff --git a/contour/bookcover.jpg b/contour/bookcover.jpg new file mode 100644 index 0000000000000000000000000000000000000000..c4cc4c2e4cc068588404352a3f4551b5b42c8888 Binary files /dev/null and b/contour/bookcover.jpg differ diff --git a/contour/document.jpg b/contour/document.jpg deleted file mode 100644 index 92c30a556c2928f3e4efd2c978b81e7b55820778..0000000000000000000000000000000000000000 Binary files a/contour/document.jpg and /dev/null differ diff --git a/contour/example.jpg b/contour/example.jpg index 72c150f8d6fa32a7fbb84a078d845af0741b9d86..cd601f4593715aa11819a0ca14b60f2980e99631 100644 Binary files a/contour/example.jpg and b/contour/example.jpg differ diff --git a/contour/example2.jpg b/contour/example2.jpg index 9ccdd3839f35e2d836584f89f77a180489bfe387..5b54d82ca789f532a9fbb50a5f1361c189619f29 100644 Binary files a/contour/example2.jpg and b/contour/example2.jpg differ diff --git a/contour/example2.png b/contour/example2.png deleted file mode 100644 index 7d4b700132fc604faf561ab86c6287b6df49aa75..0000000000000000000000000000000000000000 Binary files a/contour/example2.png and /dev/null differ diff --git a/contour/example3.jpg b/contour/example3.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1144233d0bdef507f1ec10947369f102a2911446 Binary files /dev/null and b/contour/example3.jpg differ diff --git a/contour/example3.jpg.png b/contour/example3.jpg.png deleted file mode 100644 index 2e015cb702f4f421399e8468c88633401a2a4b07..0000000000000000000000000000000000000000 Binary files a/contour/example3.jpg.png and /dev/null differ diff --git a/contour/framework1821.jpg b/contour/framework1821.jpg new file mode 100644 index 0000000000000000000000000000000000000000..6f5fb4cf045942e8664282bf95f45ca4a8457b85 Binary files /dev/null and b/contour/framework1821.jpg differ diff --git a/contour/receipt.jpg b/contour/receipt.jpg index 4646e851cf72d81472874698561f5fafeed6996b..4278be820e9a4b0f3e0c11e8eb89200c2fea6e85 100644 Binary files a/contour/receipt.jpg and b/contour/receipt.jpg differ diff --git a/contour/receipt2.jpg b/contour/receipt2.jpg deleted file mode 100644 index 2ea1f5f946889225eb3e207f0323d9e8b75878d6..0000000000000000000000000000000000000000 Binary files a/contour/receipt2.jpg and /dev/null differ diff --git a/contour/tilted receipt.jpg b/contour/tilted receipt.jpg deleted file mode 100644 index 3321c2e5f09b2bc1ba4df4465f954b433449f5c3..0000000000000000000000000000000000000000 Binary files a/contour/tilted receipt.jpg and /dev/null differ diff --git a/contour/tilted_document.jpg b/contour/tilted_document.jpg new file mode 100644 index 0000000000000000000000000000000000000000..d492d9c1d8d163e46eeebc48bf8c854b65f2252f Binary files /dev/null and b/contour/tilted_document.jpg differ diff --git a/images/bookcover.jpg b/images/bookcover.jpg new file mode 100644 index 0000000000000000000000000000000000000000..c7a3ecff3790c181d031968e6b2ba9db03bcc1b2 Binary files /dev/null and b/images/bookcover.jpg differ diff --git a/images/document.jpg b/images/document.jpg deleted file mode 100644 index 2f482058f6353cbc9b80d3df3affa79a05cc8030..0000000000000000000000000000000000000000 Binary files a/images/document.jpg and /dev/null differ diff --git a/images/example2.jpg b/images/example2.jpg index bf59c76564dc3a276adb2d4ad61c4be7b154e75d..1e951df84b28ca2565b268c7f977389813036035 100644 Binary files a/images/example2.jpg and b/images/example2.jpg differ diff --git a/images/example2.png b/images/example2.png deleted file mode 100644 index 1e951df84b28ca2565b268c7f977389813036035..0000000000000000000000000000000000000000 Binary files a/images/example2.png and /dev/null differ diff --git a/images/example3.jpg.png b/images/example3.jpg similarity index 100% rename from images/example3.jpg.png rename to images/example3.jpg diff --git a/images/framework1821.jpg b/images/framework1821.jpg new file mode 100644 index 0000000000000000000000000000000000000000..752301f2b4844efc2ac876865ffb58f28086fcda Binary files /dev/null and b/images/framework1821.jpg differ diff --git a/images/receipt2.jpg b/images/receipt2.jpg deleted file mode 100644 index 03929d831c03842d6375aa051dd91b1362379008..0000000000000000000000000000000000000000 Binary files a/images/receipt2.jpg and /dev/null differ diff --git a/images/tilted receipt.jpg b/images/tilted receipt.jpg deleted file mode 100644 index b10b102d128b88d7252b1d0f3c1f44db26118fcc..0000000000000000000000000000000000000000 Binary files a/images/tilted receipt.jpg and /dev/null differ diff --git a/images/tilted_document.jpg b/images/tilted_document.jpg new file mode 100644 index 0000000000000000000000000000000000000000..b4387c5eec9de1f2b6f59c6a058995bac4ac1ce4 Binary files /dev/null and b/images/tilted_document.jpg differ diff --git a/modified/bookcover.jpg b/modified/bookcover.jpg new file mode 100644 index 0000000000000000000000000000000000000000..dccd80c5c865e70169236e02e8875daa5ca6b1da Binary files /dev/null and b/modified/bookcover.jpg differ diff --git a/modified/document.jpg b/modified/document.jpg deleted file mode 100644 index 51673e912e0540172bf572568c4169f2dff79228..0000000000000000000000000000000000000000 Binary files a/modified/document.jpg and /dev/null differ diff --git a/modified/example.jpg b/modified/example.jpg index 8e7fff7894648a08ec4b58e73ce0000f843dd347..adc2f6bf8be9ea3e345641283657cfd3acf166ff 100644 Binary files a/modified/example.jpg and b/modified/example.jpg differ diff --git a/modified/example2.jpg b/modified/example2.jpg index 2b0e61cf4bb7543c38d467264fd23697acfbb7ec..f7d26fdead4ec87660b76208f6ec22bbb4e85277 100644 Binary files a/modified/example2.jpg and b/modified/example2.jpg differ diff --git a/modified/example2.png b/modified/example2.png deleted file mode 100644 index 146a0e9682d0ce726516b2f18890d374dcb60b14..0000000000000000000000000000000000000000 Binary files a/modified/example2.png and /dev/null differ diff --git a/modified/example3.jpg b/modified/example3.jpg new file mode 100644 index 0000000000000000000000000000000000000000..f4bd39bab51debd28fbd4ec146d86023e1231b5a Binary files /dev/null and b/modified/example3.jpg differ diff --git a/modified/example3.jpg.png b/modified/example3.jpg.png deleted file mode 100644 index 9623dabf890feb6069b4096af081c76703b876e5..0000000000000000000000000000000000000000 Binary files a/modified/example3.jpg.png and /dev/null differ diff --git a/modified/framework1821.jpg b/modified/framework1821.jpg new file mode 100644 index 0000000000000000000000000000000000000000..d079af505384e327061a17e77227b75e63a4ae5e Binary files /dev/null and b/modified/framework1821.jpg differ diff --git a/modified/image_list.pdf b/modified/image_list.pdf new file mode 100644 index 0000000000000000000000000000000000000000..20b9d8be93d26aa97cfa58c636ccb6665a7e0051 Binary files /dev/null and b/modified/image_list.pdf differ diff --git a/modified/receipt.jpg b/modified/receipt.jpg index 1d014e9b2178d3c0539364a4be42e445f1685a41..33b4f919743b956fc90c21649a2af2a678a10ea9 100644 Binary files a/modified/receipt.jpg and b/modified/receipt.jpg differ diff --git a/modified/receipt2.jpg b/modified/receipt2.jpg deleted file mode 100644 index 7c9f8bde8e4f34e43ed1efeeb668df79b2a1bf33..0000000000000000000000000000000000000000 Binary files a/modified/receipt2.jpg and /dev/null differ diff --git a/modified/tilted receipt.jpg b/modified/tilted receipt.jpg deleted file mode 100644 index 1df19c2cdf48f76c7a02c46a0d9b04942c3b9c0b..0000000000000000000000000000000000000000 Binary files a/modified/tilted receipt.jpg and /dev/null differ diff --git a/modified/tilted_document.jpg b/modified/tilted_document.jpg new file mode 100644 index 0000000000000000000000000000000000000000..6a164b9cba2a1b96b25abe479c1bdd9a3a043a5a Binary files /dev/null and b/modified/tilted_document.jpg differ diff --git a/openCV_scanner.ipynb b/openCV_scanner.ipynb index 43b055ca01b0690b1c8052e92c204966b5c0a468..49bad8c5544aa1624eb340a518d9ebe7e59f2d60 100644 --- a/openCV_scanner.ipynb +++ b/openCV_scanner.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 196, + "execution_count": 1, "metadata": { "scrolled": true }, @@ -15,12 +15,15 @@ "import tkinter as tk\n", "from PIL import Image\n", "from PIL import ImageTk\n", + "import os\n", + "from fpdf import FPDF\n", + "from PIL import Image\n", "import os" ] }, { "cell_type": "code", - "execution_count": 197, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -37,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 203, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -57,8 +60,7 @@ " # CASE 1 - 마우스가 움직이고\n", " if event == cv2.EVENT_MOUSEMOVE: \n", " # 눌린상태일때\n", - " if mouse_is_pressing == True:\n", - " points[i][0] = x \n", + " if mouse_is_pressing == True: \n", " #마우스의 좌표와, 포인트(꼭지점) 의 거리가 가깝다면 꼭지점을 이동한다\n", " for i,point in enumerate(points):\n", " if distance((x,y), point) < 15: \n", @@ -67,7 +69,7 @@ " break \n", " # CASE 2 - 마우스 왼쪽 버튼이 눌린 상태일 때, \n", " elif event == cv2.EVENT_LBUTTONDOWN:\n", - " # 꼭짓점과의 거리가 가깝다면 mouse_is_pressing을 True로 바꾼다. ##press 를 Ture로 바꾼다.\n", + " # 꼭짓점과의 거리가 가깝다면 mouse_is_pressing을 True로 바꾼다.\n", " for point in points:\n", " if distance((x,y), point) < 10:\n", " mouse_is_pressing = True\n", @@ -80,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": 204, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -103,7 +105,7 @@ }, { "cell_type": "code", - "execution_count": 205, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -145,7 +147,7 @@ }, { "cell_type": "code", - "execution_count": 206, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -157,9 +159,9 @@ " # top left, top right, bottom right, bottom left\n", " points = sort_points(points)\n", " topLeft, topRight, bottomRight, bottomLeft = points\n", - " print(topLeft, topRight, bottomRight, bottomLeft)\n", - " print(topLeft[0] + topLeft[1], topRight[0]+topRight[1],\n", - " bottomRight[0]+bottomRight[1], bottomLeft[0]+bottomLeft[1])\n", + " #print(topLeft, topRight, bottomRight, bottomLeft)\n", + " #print(topLeft[0] + topLeft[1], topRight[0]+topRight[1],\n", + " #bottomRight[0]+bottomRight[1], bottomLeft[0]+bottomLeft[1])\n", " \n", " # 변환 후 책의 너비와 높이를 결정한다.\n", " topWidth = distance(bottomLeft, bottomRight)\n", @@ -182,7 +184,7 @@ }, { "cell_type": "code", - "execution_count": 207, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -213,7 +215,7 @@ }, { "cell_type": "code", - "execution_count": 208, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -232,11 +234,14 @@ }, { "cell_type": "code", - "execution_count": 209, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def scanning(image):\n", + " global contours\n", + " global max_index\n", + " global max_contour\n", " #grabcut , 배경 제거\n", " points = []\n", "\n", @@ -252,77 +257,45 @@ " image_mask = np.where((image_mask==2)|(image_mask==0), 0, 1).astype('uint8')\n", " image_grabcut = image*image_mask[:,:,np.newaxis]\n", "\n", - " #cv2.imshow('grabCut', image_grabcut)\n", - " #cv2.waitKey(0)\n", - " \n", " #케니에지 디텍션을 이용하여 에지 검출\n", " image_gray = cv2.cvtColor(image_grabcut, cv2.COLOR_BGR2GRAY);\n", " image_canny = cv2.Canny(image_gray, 30, 90);\n", "\n", - "\n", - " #cv2.imshow('Canny', image_canny)\n", - " #cv2.waitKey(0)\n", - "\n", " #모폴로지 클로즈 연산\n", "\n", " kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))\n", " image_canny = cv2.morphologyEx(image_canny, cv2.MORPH_CLOSE, kernel, 1)\n", - " #cv2.imwrite('./modified/' +i, image_canny)\n", - " #print(i + \"save image_canny!\")\n", - " global contours\n", - " global max_index\n", - " global max_contour\n", "\n", - " #cv2.imshow('morphology', image_canny)\n", - " #cv2.waitKey(0)\n", " # 캐니 에지 결과에서 컨투어를 검출하여 가장 큰 영역을 차지하는 컨투어를 찾는다.\n", " contours, hierarchy = cv2.findContours(image_canny, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\n", " max_index = findMaxArea(contours) \n", " \n", - " #################이부분이 잘 이해가 안감.\n", - " #print(\"contours \", contours)\n", - " #print(\"max_index = \",max_index)\n", " image_fixed = image.copy()\n", " if max_index < 0:\n", - " print(\"max_index < 0, return points \")\n", - " points = np.array([[10,10], [290, 10], [290, 390], [290, 390]])\n", + " ## 꼭짓점을 찾지 못한경우, 마우스 입력을 받기위해 임의로 네개의 꼭짓점을 이미지 가장자리로 배정한다.\n", + " points = np.array([[10,10], [width-10, 10], [width-10, height-10], [10, height-10]])\n", " return points\n", "\n", - " \n", " max_contour = contours[max_index]\n", "\n", - "\n", - " #image_contour = image.copy()\n", - " #cv2.drawContours(image_contour, [max_contour], 0, (0, 0, 255), 3)\n", - " #cv2.imshow('Contour', image_contour)\n", - " #cv2.waitKey(0)\n", - "\n", - " # approxPolyDP 함수를 사용하여 컨투어를 근사화하고 convex hull을 구한다.\n", + " ## approxPolyDP 함수를 사용하여 convex hull을 구한다.\n", " max_contour = cv2.approxPolyDP(max_contour,0.02*cv2.arcLength(max_contour,True),True)\n", " hull = cv2.convexHull(max_contour)\n", "\n", - "\n", - " #image_convexhull = image.copy()\n", - " #cv2.drawContours(image_convexhull, [hull], 0, (255,255,0), 5)\n", - " #cv2.imwrite('./modified/' +i, image_convexhull) \n", - " #print(i + \"save image_convexhull!\")\n", - " #cv2.imshow('convexHull', image_convexhull)\n", - " #cv2.waitKey(0)\n", " size = len(max_contour) \n", - " print(\"len of max_contour\" ,size)\n", - " if size == 4: # 책의 꼭지점을 구한다.\n", + " if size == 4: \n", + " ## 책의 꼭지점을 구한다.\n", " for c in hull:\n", " points.append(c[0])\n", " points = np.array(points)\n", - " #print(points)\n", - "\n", - " else: # 꼭지점이 5개 이상인 경우 경계 박스를 이용한다.\n", "\n", + " else: \n", + " ## 꼭지점이 5개 이상인 경우 경계 박스를 이용한다.\n", " rect = cv2.minAreaRect(hull)\n", " box = cv2.boxPoints(rect)\n", " points = np.int0(box.tolist())\n", "\n", - " # 검출된 꼭지점이 이미지 범위를 벗어난 경우 책 꼭지점 처리\n", + " ## 검출된 꼭지점이 이미지 범위를 벗어난 경우 책 꼭지점 처리\n", " found = False\n", " for p in points:\n", " if p[0] < 0 or p[0] > width-1 or p[1] < 0 or p[1] > height -1:\n", @@ -339,10 +312,43 @@ }, { "cell_type": "code", - "execution_count": 210, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ + "#pdf로 이미지 저장\n", + "def save_pdf():\n", + " pdf = FPDF()\n", + " imagelist = [] \n", + "\n", + " # ------------- ADD ALL THE IMAGES IN A LIST ------------- #\n", + "\n", + " folder = './modified/'\n", + " for dirpath, dirnames, filenames in os.walk(folder):\n", + " for filename in [f for f in filenames if f.endswith(\".jpg\")]:\n", + " full_path = os.path.join(dirpath, filename)\n", + " imagelist.append(full_path)\n", + "\n", + " imagelist.sort() # Sort the images by name.\n", + " for i in range(0, len(imagelist)):\n", + " print(imagelist[i])\n", + "\n", + " # -------------- CONVERT TO PDF ------------ #\n", + " for image in imagelist:\n", + " pdf.add_page()\n", + " pdf.image(image, 0, 0, 210, 297) # 210 and 297 are the dimensions of an A4 size sheet.\n", + " pdf.output(folder + \"image_list\" + '.pdf', \"F\")\n", + "\n", + " print(\"PDF generated successfully!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "# o 버튼(마우스 사용)을 눌렀을 때, \n", "def b1event():\n", " global step\n", " if sizes == 4:\n", @@ -364,13 +370,13 @@ " \n", " cv2.imshow('input', image_result)\n", " cv2.imshow('result', image_final)\n", - " cv2.imwrite(modified_path +i, image_final)\n", - " print(i + \" i got 4 edges!!\")\n", + " cv2.imwrite(modified_path + filename, image_final)\n", + " #print(filename + \" i got 4 edges!!\")\n", " \n", " else:\n", " cv2.imshow('input', image)\n", - " cv2.imwrite(modified_path +i, image)\n", - " print(i + \" i don't got 4 edges!\")\n", + " cv2.imwrite(modified_path + filename, image)\n", + " #print(filename + \" i don't got 4 edges!\")\n", " cv2.waitKey(0)\n", " cv2.destroyAllWindows()\n", " win.destroy()" @@ -378,20 +384,24 @@ }, { "cell_type": "code", - "execution_count": 211, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ + "#x 버튼(마우스입력을 안함)을 눌렀을 때\n", "def b2event():\n", + " \n", + " ## 꼭지점 4개를 찾은경우\n", " if sizes == 4:\n", " image_final = transform(image, points)\n", " cv2.imshow('result', image_final)\n", - " cv2.imwrite(modified_path +i, image_final)\n", - " print(i + \" i got 4 edges!!/no button\")\n", + " cv2.imwrite(modified_path +filename, image_final)\n", + " #print(i + \" i got 4 edges!!/no button\")\n", + " \n", " else:\n", " cv2.imshow('input', image)\n", - " cv2.imwrite(modified_path +i, image)\n", - " print(i + \" i don't got 4 edges!/no button\")\n", + " cv2.imwrite(modified_path +filename, image)\n", + " #print(i + \" i don't got 4 edges!/no button\")\n", " cv2.waitKey(0)\n", " cv2.destroyAllWindows()\n", " win.destroy()" @@ -399,53 +409,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 14, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "len of max_contour 4\n", - "size 4\n", - "[20. 39.] [213. 14.] [277. 322.] [ 76. 303.]\n", - "59.0 227.0 599.0 379.0\n", - "document.jpg i got 4 edges!!\n", - "len of max_contour 4\n", - "size 4\n", - "[36. 43.] [265. 40.] [274. 369.] [ 21. 375.]\n", - "79.0 305.0 643.0 396.0\n", - "example.jpg i got 4 edges!!\n", - "len of max_contour 4\n", - "size 4\n", - "[54. 42.] [256. 57.] [266. 347.] [ 44. 349.]\n", - "96.0 313.0 613.0 393.0\n", - "example2.jpg i got 4 edges!!\n", - "len of max_contour 4\n", - "size 4\n", - "[53. 24.] [267. 58.] [267. 379.] [ 10. 318.]\n", - "77.0 325.0 646.0 328.0\n", - "example2.png i got 4 edges!!\n", - "len of max_contour 4\n", - "size 4\n", - "[43. 30.] [263. 27.] [272. 360.] [ 52. 365.]\n", - "73.0 290.0 632.0 417.0\n", - "example3.jpg.png i got 4 edges!!\n", - "len of max_contour 7\n", - "size 4\n", - "[74. 9.] [273. 8.] [275. 376.] [ 77. 378.]\n", - "83.0 281.0 651.0 455.0\n", - "receipt.jpg i got 4 edges!!\n", - "len of max_contour 4\n", - "size 4\n", - "[ 20. 100.] [152. 27.] [279. 290.] [131. 371.]\n", - "120.0 179.0 569.0 502.0\n", - "receipt2.jpg i got 4 edges!!\n", - "len of max_contour 8\n", - "size 4\n", - "[25. 74.] [208. 51.] [243. 334.] [ 60. 357.]\n", - "99.0 259.0 577.0 417.0\n", - "tilted receipt.jpg i got 4 edges!!\n" + "./modified/bookcover.jpg\n", + "./modified/example.jpg\n", + "./modified/example2.jpg\n", + "./modified/example3.jpg\n", + "./modified/framework1821.jpg\n", + "./modified/receipt.jpg\n", + "./modified/tilted_document.jpg\n", + "PDF generated successfully!\n" ] } ], @@ -455,108 +435,62 @@ "if not os.path.isdir(image_path): \n", " os.mkdir(image_path)\n", "\n", - "for i in os.listdir(image_path): \n", - " path = image_path + i \n", + "## image 폴더안에 있는 이미지파일들을 하나씩 탐색.\n", + "for filename in os.listdir(image_path): \n", + " path = image_path + filename \n", " count = len(os.listdir(image_path))\n", " \n", + " ##이미지 파일이 하나도 없는경우\n", " if count == 0:\n", " print(\"there are no images\")\n", + " \n", " image = cv2.imread(path, cv2.IMREAD_COLOR)\n", - " image=cv2.resize(image,(300,400))\n", + " \n", + " ##이미지가 너무 큰경우, 적당한 크기로 줄임\n", + " height, width, channel = image.shape\n", + " if height > 720 or width > 540:\n", + " height,width = 720,540\n", + " image=cv2.resize(image,(width,height))\n", + " #print(height, width , channel)\n", + " \n", " points = scanning(image)\n", + " \n", + " #scanning 결과 나온 contour 이미지를 만듬.\n", + " image_contour = image.copy()\n", + " cv2.drawContours(image_contour, [max_contour], 0, (0, 0, 255), 3)\n", " sizes = len(points)\n", - " print(\"size\",sizes)\n", " \n", + " #UI 창 띄우기\n", " win = tk.Tk()\n", " win.title(\"Ask\")\n", - " win.geometry(\"600x600+50+50\")\n", + " win.geometry(\"600x800+50+100\")\n", " win.resizable(False, False)\n", - "\n", " lbl = tk.Label(win, text=\"마우스 입력 O/X?\")\n", - " #lbl.grid(row=1, column=1)\n", " lbl.pack()\n", - " image_contour = image.copy()\n", - " cv2.drawContours(image_contour, [max_contour], 0, (0, 0, 255), 3)\n", " \n", + " ## contour 이미지 파일을 저장하기. 폴더가 없을경우 폴더를 만듬\n", " contour_path = './contour/'\n", " if not os.path.isdir(contour_path): \n", " os.mkdir(contour_path)\n", " \n", - " cv2.imwrite(contour_path +i, image_contour)\n", + " cv2.imwrite(contour_path + filename, image_contour)\n", "\n", - " imgtk = ImageTk.PhotoImage(file = contour_path +i, master=win)\n", + " imgtk = ImageTk.PhotoImage(file = contour_path + filename, master=win)\n", " label = tk.Label(win, image=imgtk)\n", " label.pack()\n", "\n", + " ## 최종 수정된 이미지를 저장하기. 폴더가 없을경우 폴더 생성\n", " modified_path = './modified/'\n", " if not os.path.isdir(modified_path): \n", " os.mkdir(modified_path)\n", + " \n", " b1 = tk.Button(win, text='마우스 입력O', command = b1event)\n", " b2 = tk.Button(win, text='마우스 입력X', command = b2event)\n", - " #b1.grid(row=3, column=1)\n", - " #b2.grid(row=3, column=2)\n", " b1.pack()\n", " b2.pack()\n", "\n", - " win.mainloop()" - ] - }, - { - "cell_type": "code", - "execution_count": 150, - "metadata": {}, - "outputs": [ - { - "ename": "KeyboardInterrupt", - "evalue": "Interrupted by user", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m<ipython-input-150-0a916bbbf96e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;31m# ------------- ADD ALL THE IMAGES IN A LIST ------------- #\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 18\u001b[1;33m \u001b[0mfolder\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Enter the path of the folder containing the images : '\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 19\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mdirpath\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdirnames\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfilenames\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwalk\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfolder\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mfilename\u001b[0m \u001b[1;32min\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mf\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mf\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mfilenames\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mendswith\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\".jpg\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m 858\u001b[0m \u001b[1;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 859\u001b[0m )\n\u001b[1;32m--> 860\u001b[1;33m return self._input_request(str(prompt),\n\u001b[0m\u001b[0;32m 861\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 862\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m 902\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 903\u001b[0m \u001b[1;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 904\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Interrupted by user\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 905\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 906\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Invalid Message:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user" - ] - } - ], - "source": [ - "#이미지 저장\n", - "##################아직 안합침################################\n", - "from fpdf import FPDF\n", - "from PIL import Image\n", - "import os\n", - "\n", - "pdf = FPDF()\n", - "imagelist = [] # Contains the list of all images to be converted to PDF.\n", - "\n", - "\n", - "# --------------- USER INPUT -------------------- #\n", - " \n", - "folder = \"\" # Folder containing all the images.\n", - "name = \"\" # Name of the output PDF file.\n", - "\n", - "# ------------- ADD ALL THE IMAGES IN A LIST ------------- #\n", - "\n", - "folder = input('Enter the path of the folder containing the images : ')\n", - "for dirpath, dirnames, filenames in os.walk(folder):\n", - " for filename in [f for f in filenames if f.endswith(\".jpg\")]:\n", - " full_path = os.path.join(dirpath, filename)\n", - " imagelist.append(full_path)\n", - "\n", - "imagelist.sort() # Sort the images by name.\n", - "for i in range(0, len(imagelist)):\n", - " print(imagelist[i])\n", - "\n", - "# -------------- CONVERT TO PDF ------------ #\n", - "\n", - "name = input('Enter the PDF file name : ')\n", - "for image in imagelist:\n", - " pdf.add_page()\n", - " pdf.image(image, 0, 0, 210, 297) # 210 and 297 are the dimensions of an A4 size sheet.\n", - "pdf.output(folder + name + '.pdf', \"F\")\n", - " \n", - "print(\"PDF generated successfully!\")" + " win.mainloop()\n", + "save_pdf()" ] }, {