ЛКШ.2014.Зима.C'+.07.Изображения

Назад, архив изображений целиком: im.zip im.7z.
Примеры изображений Пример загрузки/сохранения изображения в формате BMP, 24 бита на точку
m.bmp 3x2.bmp image-processing.py
# Translate list of bytes to number
def bytes_to_number(L):
  x = 0
  for b in reversed(L):
    x = x * 256 + b
  return x


# Read Image
def read_image(file_name):
  with open(file_name, 'rb') as f:
    code = list(f.read())                # list of file bytes

  offset = bytes_to_number(code[10:14])  # where colors data begins
  w = bytes_to_number(code[18:22])       # decode width
  h = bytes_to_number(code[22:26])       # decode height
  line_size = (3 * w + 3) // 4 * 4       # line size in bytes, must be aligned by 4 bytes
  return (code, w, h, line_size, offset)
  

# Put one new color in image bytes
# color is in (b, g, r) format
def put_pixel(image, x, y, color):
  offset = image[4] + image[3] * (image[2] - 1 - y) + x * 3
  image[0][offset:offset + 3] = color


# Get color of a pixel with (x, y) coordinates
# color is in (b, g, r) format
def get_pixel(image, x, y):
  offset = image[4] + image[3] * (image[2] - 1 - y) + x * 3
  return image[0][offset:offset + 3]


im = read_image('sample.bmp')
w = im[1]
h = im[2]

# Image processing
for y in range(h):
  for x in range(w):
    c = get_pixel(im, x, y)
    # ... doing something with color 'c'
    put_pixel(im, x, y, new_color)

# Save result
with open('result.bmp', 'wb') as f:
  f.write(bytes(im[0]))
m.bmp m.bmp
pj.bmp pj.bmp
anton.bmp anton.bmp
roma.bmp roma.bmp
vm.bmp vm.bmp