Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: origin 이미지와 sketch 이미지의 크기가 다른 경우 비교해서 자르기 #49

Merged
merged 2 commits into from
Jun 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions cut_img.ipynb
Original file line number Diff line number Diff line change
@@ -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
}