{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyN6ESRfDgvYnJzywmcLcLFU"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"SJ9W40ImzbYJ","executionInfo":{"status":"ok","timestamp":1724843450436,"user_tz":-60,"elapsed":21629,"user":{"displayName":"Jinwei","userId":"14655726557162511328"}},"outputId":"b680de29-7a71-499b-e788-689326cc5c8a"},"outputs":[{"output_type":"stream","name":"stdout","text":["Mounted at /content/gdrive\n"]}],"source":["from google.colab import drive\n","\n","\n","drive.mount('/content/gdrive', force_remount=True)"]},{"cell_type":"code","source":["import os\n","import numpy as np\n","from PIL import Image\n","import math\n","\n","def read_binary_file_to_rgb_image(file_path):\n","\n","    with open(file_path, 'rb') as file:\n","\n","        byte_array = file.read()\n","\n","\n","    uint8_array = np.frombuffer(byte_array, dtype=np.uint8)\n","\n","\n","    filtered_array = uint8_array[uint8_array != 0]\n","\n","\n","    length = len(filtered_array)\n","    if length % 3 != 0:\n","        padding = 3 - (length % 3)\n","        filtered_array = np.pad(filtered_array, (0, padding), 'constant')\n","\n","\n","    num_pixels = len(filtered_array) // 3\n","    side_length = math.ceil(math.sqrt(num_pixels))\n","    total_length = side_length * side_length * 3\n","\n","\n","    if len(filtered_array) < total_length:\n","        filtered_array = np.pad(filtered_array, (0, total_length - len(filtered_array)), 'constant')\n","\n","\n","    rgb_array = filtered_array.reshape((side_length, side_length, 3))\n","\n","\n","    image = Image.fromarray(rgb_array, 'RGB')\n","\n","    return image\n","\n","def process_files_in_directory(input_dir, output_dir):\n","\n","    files = sorted(os.listdir(input_dir))\n","\n","\n","    for i in range(0, min(len(files), 6000)):\n","        file_name = files[i]\n","        file_path = os.path.join(input_dir, file_name)\n","\n","\n","        image = read_binary_file_to_rgb_image(file_path)\n","\n","\n","        output_file_name = os.path.splitext(file_name)[0] + '.png'\n","        output_file_path = os.path.join(output_dir, output_file_name)\n","\n","\n","        image.save(output_file_path)\n","        print(f\"Processed and saved: {output_file_path}\")\n","\n","\n","input_dir = './pe/'\n","output_dir = './MalImage/'\n","process_files_in_directory(input_dir, output_dir)\n"],"metadata":{"id":"dEKYNp7cdiSq"},"execution_count":null,"outputs":[]}]}