{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":20270,"databundleVersionId":1222630,"sourceType":"competition"}],"dockerImageVersionId":29926,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import zipfile\nfrom PIL import Image\nfrom pathlib import Path\nimport io","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:11:39.317709Z","iopub.execute_input":"2025-01-26T16:11:39.318134Z","iopub.status.idle":"2025-01-26T16:11:39.323692Z","shell.execute_reply.started":"2025-01-26T16:11:39.318102Z","shell.execute_reply":"2025-01-26T16:11:39.321911Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_list = Path('../input/siim-isic-melanoma-classification/jpeg').glob('**/*.jpg')\nWIDTH = 256","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:11:33.405696Z","iopub.execute_input":"2025-01-26T16:11:33.406023Z","iopub.status.idle":"2025-01-26T16:11:33.411642Z","shell.execute_reply.started":"2025-01-26T16:11:33.405995Z","shell.execute_reply":"2025-01-26T16:11:33.410536Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with zipfile.ZipFile('train.zip', 'w') as trainzip:\n    for i, img_fn in enumerate(img_list):\n        small_img = Image.open(img_fn).resize((224, 224))\n        image_file = io.BytesIO()\n        small_img.save(image_file, 'PNG')\n        zipped_filename = img_fn.parts[-2] + '/' + img_fn.stem + '.png'\n        trainzip.writestr(zipped_filename, image_file.getvalue())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image\n\ndef resize_image_by_smaller_side(input_path, output_path, target_size):\n    # Mở ảnh\n    img = Image.open(input_path)\n    \n    # Lấy kích thước ảnh gốc\n    width, height = img.size\n    \n    # Xác định tỉ lệ resize dựa trên cạnh nhỏ hơn\n    if width < height:\n        ratio = target_size / width\n        new_width = target_size\n        new_height = int(height * ratio)\n    else:\n        ratio = target_size / height\n        new_width = int(width * ratio)\n        new_height = target_size\n    \n    # Resize ảnh\n    resized_img = img.resize((new_width, new_height), Image.ANTIALIAS)\n    \n    # Lưu ảnh đã resize\n    resized_img.save(output_path)\n# Sử dụng hàm\ninput_path = \"input_image.jpg\"  # Đường dẫn ảnh gốc\noutput_path = \"output_image.jpg\"  # Đường dẫn lưu ảnh resize\ntarget_size = 500  # Kích thước cạnh nhỏ hơn mong muốn\nresize_image_by_smaller_side(\"/kaggle/input/siim-isic-melanoma-classification\", \"/kaggle/working/\", 256)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_paths=os.listdir('../input/siim-isic-melanoma-classification/jpeg/train')\nimage_paths= [\"../input/siim-isic-melanoma-classification/jpeg/train/\" + str(x) for x in image_paths]","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}