Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save InfiniteSynthesis/007cfe7f90cf7b2c888691a3a3b9fa77 to your computer and use it in GitHub Desktop.
Save InfiniteSynthesis/007cfe7f90cf7b2c888691a3a3b9fa77 to your computer and use it in GitHub Desktop.
Scale up wallpaper image to 4K resolution by google colab and waifu2x-chainer.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "upscale4k.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "lUWpoANv8CBR"
},
"source": [
"# @brief: waifu2x-scaleup4k\n",
"# @By: github/InfiniteSynthesis\n",
"# This notebook required GPU runtime, so please activate GPU.\n",
"\n",
"import os\n",
"from google.colab import files\n",
"from google.colab import drive\n",
"from PIL import Image\n",
"!git clone https://github.com/tsurumeso/waifu2x-chainer\n",
"\n",
"try:\n",
" drive.mount('/content/drive/')\n",
"except:\n",
" exit()\n",
"\n",
"%cd /content/waifu2x-chainer\n",
"\n",
"SUPPORTED_FORMAT = ['.jpg', '.png', '.jpeg', '.tif', '.tiff' , '.bmp', '.tga']\n",
"SUITABLE_COUNT = 0\n",
"PROCESSED_COUNT = 0\n",
"FAILED_SET = []\n",
"MARK_COUNT = 0\n",
"\n",
"def call_waifu2x(file_path, scale):\n",
" global FAILED_SET\n",
" fname, ext = os.path.splitext(file_path)\n",
" target_name = fname + '_' + str(scale) + 'x.png'\n",
"\n",
" try:\n",
" result = !python /content/waifu2x-chainer/waifu2x.py -g 0 -s {scale} -i {file_path} --output {target_name}\n",
" print(result)\n",
" except:\n",
" FAILED_SET.append(file_path)\n",
" else:\n",
" if os.path.exists(target_name):\n",
" os.remove(file_path)\n",
" else:\n",
" FAILED_SET.append(file_path)\n",
"\n",
"\n",
"def process_image(file_path):\n",
" global SUITABLE_COUNT, PROCESSED_COUNT, MARK_COUNT\n",
" img = Image.open(file_path)\n",
" imgWidth, imgHeight = img.size\n",
" img.close()\n",
" idealLength = 3840 # 4K resolution\n",
" PROCESSED_COUNT += 1\n",
" scale = (idealLength / imgWidth) if (imgWidth > imgHeight) else (idealLength / imgHeight)\n",
" if (scale <= 1):\n",
" SUITABLE_COUNT += 1\n",
" PROCESSED_COUNT -= 1\n",
" MARK_COUNT += 1\n",
" if MARK_COUNT == 80:\n",
" print(\">\")\n",
" MARK_COUNT = 0\n",
" else:\n",
" print(\">\", end = \"\")\n",
" elif (scale <= 1.5):\n",
" call_waifu2x(file_path, 1.5)\n",
" elif (scale <= 2):\n",
" call_waifu2x(file_path, 2)\n",
" elif scale <= 3:\n",
" call_waifu2x(file_path, 3)\n",
" else:\n",
" call_waifu2x(file_path, 4)\n",
"\n",
"def main():\n",
" for folder in ['vertical/', 'horizontal/']:\n",
" for root, dirs, files in os.walk(\"/content/drive/MyDrive/\" + folder):\n",
" for file in files:\n",
" if (os.path.splitext(file)[1] in SUPPORTED_FORMAT):\n",
" process_image(root + file)\n",
"\n",
" print(\"\\nsuitable image count = \\033[1;32m\" + str(SUITABLE_COUNT) + \\\n",
" \"\\033[0m, processed image count = \\033[1;34m\" + str(PROCESSED_COUNT) + \"\\033[0m.\")\n",
" print('failed image set:', FAILED_SET)\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n",
"\n"
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment