{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":21755,"databundleVersionId":1475600,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-21T07:21:11.026547Z","iopub.execute_input":"2024-12-21T07:21:11.026977Z","iopub.status.idle":"2024-12-21T07:21:14.361392Z","shell.execute_reply.started":"2024-12-21T07:21:11.026929Z","shell.execute_reply":"2024-12-21T07:21:14.360293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Clone repository CycleGAN dan pix2pix\n!git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix\n%cd pytorch-CycleGAN-and-pix2pix\n\n# Unduh model yang sudah dilatih sebelumnya\n!bash ./scripts/download_cyclegan_model.sh style_monet\n\n# Install dependensi yang dibutuhkan\n!pip install -r requirements.txt\n!pip install Pillow\n\n# Proses awal: resize dan salin foto\nimport os\nimport glob\nfrom PIL import Image\nimport zipfile\n\n# Buat direktori untuk menyimpan gambar yang sudah diproses\noutput_dir = '/kaggle/working/preprocessed_images'\nos.makedirs(output_dir, exist_ok=True)\n\n# Lokasi input gambar\ninput_dir = '/kaggle/input/gan-getting-started/photo_jpg'\nresize_dim = (256, 256)\n\n# Resize semua gambar menjadi 256x256 dan ubah ke format RGB\nfor file_path in glob.glob(os.path.join(input_dir, '*')):\n    with Image.open(file_path) as img:\n        processed_img = img.resize(resize_dim).convert('RGB')\n        processed_img.save(os.path.join(output_dir, os.path.basename(file_path)))\n\n# Tampilkan jumlah gambar yang telah diproses\nprint(f\"Jumlah gambar yang telah diproses: {len(os.listdir(output_dir))}\")\n\n# Gunakan model CycleGAN untuk mengonversi gambar ke gaya Monet\n!python test.py --dataroot /kaggle/working/preprocessed_images --name style_monet_pretrained --model test --no_dropout --gpu_ids -1 --num_test 7035\n\n# Kompres gambar hasil ke dalam file ZIP\nwith zipfile.ZipFile('/kaggle/working/images.zip', 'w') as zip_archive:\n    results_dir = './results/style_monet_pretrained/test_latest/images'\n    img_count = 0\n\n    # Tambahkan semua gambar \"_fake.png\" ke dalam file ZIP setelah dikonversi ke JPEG\n    for fake_img_path in glob.glob(os.path.join(results_dir, '*_fake.png')):\n        with Image.open(fake_img_path).convert('RGB') as fake_img:\n            jpeg_path = fake_img_path.replace('.png', '.jpg')\n            fake_img.save(jpeg_path, 'JPEG')\n            zip_archive.write(jpeg_path, os.path.basename(jpeg_path))\n            img_count += 1\n\n    print(f\"Total gambar yang dimasukkan ke ZIP: {img_count}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-21T07:21:14.363548Z","iopub.execute_input":"2024-12-21T07:21:14.364007Z","iopub.status.idle":"2024-12-21T09:31:28.574323Z","shell.execute_reply.started":"2024-12-21T07:21:14.363959Z","shell.execute_reply":"2024-12-21T09:31:28.572859Z"}},"outputs":[],"execution_count":null}]}