Код: Выделить всё
def __detect_shapes(image):
img = Image.open(image)
img = img.convert("L")
img = img.point(lambda p: p > 128 and 255)
img = img.convert("RGB")
# get a list of every black pixel
black_pixels = []
for x in range(img.width):
for y in range(img.height):
if img.getpixel((x, y)) == (0, 0, 0):
black_pixels.append((x, y))
# separate the black pixels into shapes
black_pixels = set(black_pixels)
shapes = []
while black_pixels:
shape = []
stack = [black_pixels.pop()]
while stack:
x, y = stack.pop()
shape.append((x, y))
for dx in range(-1, 2):
for dy in range(-1, 2):
if (dx, dy) == (0, 0):
continue
neighbor = (x + dx, y + dy)
if neighbor in black_pixels:
stack.append(neighbor)
black_pixels.remove(neighbor)
shapes.append(shape)
return shapes
def __move_point(point, width, height, dx, dy):
lambda_0 = width / 2
phi_1 = height / 2
map_point = __convert_to_map_space(point, width, height)
spherical_point = __convert_to_spherical(map_point, lambda_0, phi_1)
new_point = __rotate_point(spherical_point, dx, -dy)
equirectangular_point = __convert_to_equirectangular(new_point, lambda_0, phi_1)
x, y = __convert_to_image_space(equirectangular_point, width, height)
return (int(x), int(y))
def __convert_to_map_space(point, width, height, map_width=360, map_height=180):
x, y = point
_x = (x / width) * map_width - (map_width / 2)
_y = (y / height) * map_height - (map_height / 2)
return (_x, _y)
def __convert_to_spherical(point, lambda_0, phi_1):
x, y = point # point is in degrees
x = np.deg2rad(x)
y = np.deg2rad(y)
cos_phi_1 = np.cos(np.deg2rad(phi_1))
lambda_0 = np.deg2rad(lambda_0)
r = 1
_lambda = (x / cos_phi_1) + lambda_0
_phi = y
return (r, _phi, _lambda)
# dtheta and dphi are in DREGREES
def __rotate_point(point, dphi, dtheta):
r, theta, phi = point # point is in radians
phi += np.deg2rad(dphi)
theta += np.deg2rad(dtheta)
return (r, theta, phi)
def __convert_to_equirectangular(point, lambda_0, phi_1):
_, _phi, _lambda = point # point is in radians
lambda_0 = np.deg2rad(lambda_0)
x = (_lambda - lambda_0) * np.cos(phi_1)
y = _phi
return (x, y)
def __convert_to_image_space(point, width, height, map_width=360, map_height=180):
_x, _y = point # point is in radians
_x = np.rad2deg(_x)
_y = np.rad2deg(_y)
x = _x + (map_width / 2) # recenter
x = (x / map_width) * width # scale
y = _y + (map_height / 2)
y = (y / map_height) * height
return (x, y)
output = Image.new("RGB", (512, 256), (255, 255, 255))
draw = ImageDraw.Draw(output)
shapes = __detect_shapes("import/test.png")
for s in shapes:
for p in s:
draw.point(p, fill="black")
for p in shapes[1]:
moved_point = __move_point(p, 512, 256, -50, 90)
draw.point(moved_point, fill="red")
output.save("export/test.png")
output.show()
Три черные фигуры

Вот результат после запуска программы:
те же три черные фигуры, но самая правая передвинута вверх

И вот чего я (примерно) ожидал:
Те же три черные фигуры, но самая правая была перемещено вверх и искажено

Почему нет искажений в результате работы программы?
Подробнее здесь: https://stackoverflow.com/questions/787 ... onto-a-equ