Skip to content

Instantly share code, notes, and snippets.

@ivanlen
Last active February 9, 2020 22:37
Show Gist options
  • Save ivanlen/8e1dfb31cc72ab1eed700269eb1f9602 to your computer and use it in GitHub Desktop.
Save ivanlen/8e1dfb31cc72ab1eed700269eb1f9602 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"folder_to_scan = './datasets/raw/with_borders/'\n",
"files = os.listdir(folder_to_scan)\n",
"saving_folder_name = '.datasets/curated/'\n",
"valid_file_types = ['.jpg']"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"133"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(files)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"valid_files = []\n",
"for file in files:\n",
" f, fe = os.path.splitext(file)\n",
" if fe in valid_file_types:\n",
" valid_files.append(file)\n",
"\n",
"full_path_valid_files = [folder_to_scan + file for file in valid_files]\n",
"for file_path in full_path_valid_files:\n",
"\n",
" file_name = os.path.basename(file_path)\n",
" base_name, file_ext = os.path.splitext(file_name)\n",
" im = Image.open(file_path)\n",
"\n",
" im_w = im.size[0]\n",
" im_h = im.size[1]\n",
" # delete superior border\n",
"\n",
" imc = im.crop((0,40,im_w,im_h)) #left bottom right upper\n",
"\n",
" f_name = saving_folder_name + base_name + file_ext\n",
" imc.save(f_name)\n",
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "testing-kernel",
"language": "python",
"name": "testing-kernel"
},
"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.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment