# -*- coding:utf-8 -*- # @Author len # @Create 2023/10/26 9:18 from PIL import Image import numpy as np import os def convert_jpg_to_bin(input_file, output_file, max_width, max_height): # 打开JPEG图像 image = Image.open(input_file) print(input_file) # 限制图像尺寸 image.thumbnail((max_width, max_height)) # 将图像转换为RGB模式 image = image.convert("RGB") # 创建一个空白的16位彩色图像 result_image = np.zeros((image.size[1], image.size[0]), dtype=np.uint16) # 遍历图像的每个像素 for y in range(image.size[1]): for x in range(image.size[0]): # 获取RGB值 r, g, b = image.getpixel((x, y)) # 将RGB值转换为RGB565格式 pixel_value = ((r & 0b11111000) << 8) | ((g & 0b11111100) << 3) | (b >> 3) # 将RGB565值存储到16位彩色图像中 result_image[y, x] = pixel_value # 将三维数组展平为一维数组 flattened_array = result_image.flatten() # 保存为BIN文件 with open(output_file, "wb") as file: file.write(flattened_array.tobytes()) def load_images_from_folder(folder_path): images = [] for filename in os.listdir(folder_path): if filename.endswith(".jpg") or filename.endswith(".png"): image_path = os.path.join(folder_path, filename) image = Image.open(image_path).convert("RGBA") images.append(image) return images # 使用示例 input_file = r"F:\img" # 图片地址 # output_file = r"E:\\" # 目标地址 max_width = 800 max_height = 480 files= os.listdir(input_file) for file in files: print(file) if file in ["System Volume Information"] or ".bin" in file: continue if not os.path.isdir(file): # 判断是否是文件夹,不是文件夹才打开 # print(input_file + "/" + file) portion = os.path.splitext(file) input001 = input_file + "/" + file # out001 = output_file + "/" + portion[0]+".bin" out001 = input_file + "/" + portion[0]+".bin" # portion = os.path.splitext(file) print(out001) convert_jpg_to_bin(input001, out001, max_width, max_height)