diff --git a/cut_img.ipynb b/cut_img.ipynb new file mode 100644 index 0000000..c97b574 --- /dev/null +++ b/cut_img.ipynb @@ -0,0 +1,99 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "dab1dfd6-30a7-4330-8812-38fe567ea627", + "metadata": {}, + "outputs": [], + "source": [ + "import PIL\n", + "from PIL import Image\n", + "from torchvision import transforms\n", + "import torch\n", + "import matplotlib.pyplot as plt\n", + "import torchvision" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "477ea19c-e79f-45b0-bb57-3a2448e2ec60", + "metadata": {}, + "outputs": [], + "source": [ + "to_tensor = transforms.ToTensor()\n", + "def load_img(img_path):\n", + " img= PIL.Image.open(img_path)\n", + " img = to_tensor(img)\n", + " return img[:3, :, :]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ffd2097f-c2da-415c-b6a2-dc28e4f357aa", + "metadata": {}, + "outputs": [], + "source": [ + "# 가장 많이 겹치는 부분 찾기\n", + "def find_same(origin,change):\n", + " min_num=1e9\n", + " new_img=change\n", + " for i in range(len(change[0])-len(origin[0])+1):\n", + " for j in range(len(change[0][0])-len(origin[0][0])+1):\n", + " new_change=change[:,i:i+origin.size()[1],j:j+origin.size()[2]]\n", + " pred=torch.sum(abs(new_change[0]-origin[0]))\n", + " if min_num>pred:\n", + " min_num=pred\n", + " new_img=new_change\n", + " print(new_img.size())\n", + " return new_img" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6669b3af-8006-480e-b6fc-a4aa8bb8d7e9", + "metadata": {}, + "outputs": [], + "source": [ + "test_img=load_img('/opt/ml/input/test1.jpg')\n", + "change_img=load_img('/opt/ml/input/test1_change.jpg')\n", + "sketch_img=find_same(test_img,change_img)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "331142ce-eaf4-4f40-8317-b00e8adf322f", + "metadata": {}, + "outputs": [], + "source": [ + "# 이미지 저장\n", + "torchvision.utils.save_image(sketch_img, \"/opt/ml/input/sketch_img.jpg\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}