Files
2025-12-08 20:28:56 +05:00

75 lines
3.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PIL import Image
import sys
import os
def split_and_convert(img_path, tile_size=(64, 256)):
# Открываем изображение
img = Image.open(img_path)
width, height = img.size
# Получаем имя файла без расширения
base_name = os.path.splitext(os.path.basename(img_path))[0]
counter = 0
for i in range(0, width, tile_size[0]):
for j in range(0, height, tile_size[1]):
box = (i, j, i+tile_size[0], j+tile_size[1])
tile = img.crop(box)
# Конвертируем в 256 цветов
tile = tile.convert('P', palette=Image.Palette.ADAPTIVE, colors=256)
# Сохраняем тайл с чётным индексом
tile.save(f'{base_name}_{counter:02d}_00_00.png', optimize=True)
# Извлекаем палитру
palette = tile.getpalette()
if palette:
# Создаём изображение палитры 256x1
# Каждый пиксель - один цвет из палитры
palette_rgb = []
for k in range(0, len(palette), 3):
r, g, b = palette[k], palette[k+1], palette[k+2]
palette_rgb.append((r, g, b))
# Обрезаем до 256 цветов (на всякий случай)
palette_rgb = palette_rgb[:256]
# Создаём изображение палитры 256x10 (можно сделать 256x1, но так нагляднее)
palette_img = Image.new('RGB', (256, 1))
# Заполняем каждый столбец своим цветом
for x in range(256):
color = palette_rgb[x] if x < len(palette_rgb) else (0, 0, 0)
for y in range(1):
palette_img.putpixel((x, y), color)
# Сохраняем палитру с нечётным индексом как 16-bit PNG
palette_img.save(
f'{base_name}_{counter+1:02d}_00_00.png',
optimize=True,
bits=16 # 16-битное сохранение
)
counter += 2
def main():
# Проверяем аргументы командной строки
if len(sys.argv) < 2:
print("Использование: python script.py <input_image>")
print("Пример: python script.py TEST.png")
sys.exit(1)
input_image = sys.argv[1]
# Проверяем существование файла
if not os.path.exists(input_image):
print(f"Ошибка: Файл '{input_image}' не найден")
sys.exit(1)
# Запускаем обработку
split_and_convert(input_image)
if __name__ == "__main__":
main()