File size: 2,819 Bytes
879577c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "f1614722-84e7-4494-87ec-3a36867fd694",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import cv2\n",
"import numpy as np\n",
"import shutil\n",
"\n",
"def classify_images_by_brightness(directory):\n",
" brightness_dict = {}\n",
" \n",
" # 遍历文件夹中的每个图像\n",
" for filename in os.listdir(directory):\n",
" if filename.endswith(\".jpg\") or filename.endswith(\".png\"):\n",
" # 读取图像\n",
" img = cv2.imread(os.path.join(directory, filename))\n",
" if img is not None:\n",
" # 计算图像的平均亮度\n",
" avg_brightness = np.average(img)\n",
" brightness_dict[filename] = avg_brightness\n",
" \n",
" # 将图像按亮度排序\n",
" sorted_images = sorted(brightness_dict.items(), key=lambda item: item[1])\n",
" \n",
" # 创建子文件夹\n",
" if not os.path.exists(directory + '/low'):\n",
" os.makedirs(directory + '/low')\n",
" if not os.path.exists(directory + '/medium'):\n",
" os.makedirs(directory + '/medium')\n",
" if not os.path.exists(directory + '/high'):\n",
" os.makedirs(directory + '/high')\n",
" \n",
" # 将图像归类到对应的子文件夹\n",
" for i in range(len(sorted_images)):\n",
" if i < len(sorted_images) / 3:\n",
" shutil.move(directory + '/' + sorted_images[i][0], directory + '/low/' + sorted_images[i][0])\n",
" elif i < 2 * len(sorted_images) / 3:\n",
" shutil.move(directory + '/' + sorted_images[i][0], directory + '/medium/' + sorted_images[i][0])\n",
" else:\n",
" shutil.move(directory + '/' + sorted_images[i][0], directory + '/high/' + sorted_images[i][0])\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "fbd8f248-6c31-4842-a18f-c73e77f5cc9b",
"metadata": {},
"outputs": [],
"source": [
"classify_images_by_brightness('928\\\\humansubd')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b7fa525-ce8c-4b2b-a4a6-b5751cb53873",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.10.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|