gif分离

from PIL import Image
import os
  
def split_gif(gif_path, output_folder):
    os.makedirs(output_folder, exist_ok=True)
    with Image.open(gif_path) as im:
        frame = 0
        try:
            while True:
                frame_path = os.path.join(output_folder, f"frame_{frame:03d}.png")
                im.save(frame_path, format="PNG")
                print(f"保存: {frame_path}")
                frame += 1
                im.seek(frame)
        except EOFError:
            print("GIF 分离完成!")
 
if __name__ == "__main__":
    gif_file = "a.gif"
    output_dir = "frames"
    split_gif(gif_file, output_dir)

读取像素

from PIL import Image
 
def get_pixel_color(image_path, x, y):
    # 打开图片
    with Image.open(image_path) as img:
        img = img.convert("RGBA")
        pixel = img.getpixel((x, y))
        return pixel
 
if __name__ == "__main__":
    img_path = "frames/frame_000.png"
    x, y = 0, 1
    color = get_pixel_color(img_path, x, y)
    print(f"像素({x}, {y})的颜色是: {color} (R, G, B, A)")

填充画布

from PIL import Image
 
 
def create_filled_canvas(width, height, color):
    img = Image.new("RGBA", (width, height), (0, 0, 0, 0))
    for y in range(height):
        for x in range(width):
            img.putpixel((x, y), color)
    return img
 
if __name__ == "__main__":
    w, h = 33, 33
    black = (0, 0, 0, 255)
    canvas = create_filled_canvas(w, h, black)
    canvas.save("canvas.png")

文件倒置

with open('1','rb') as f:   
	with open('2','wb') as g:      
		g.write(f.read()[::-1])