/
IntProg2_images.py
52 lines (39 loc) · 1.39 KB
/
IntProg2_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
# 1521x1818 pixel map of native American language
# source - Gutenberg project
image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/gutenberg.jpg")
# Image dimensions
MAP_WIDTH = 1521
MAP_HEIGHT = 1818
# Scaling factor
SCALE = 3
# Canvas size
CAN_WIDTH = MAP_WIDTH // SCALE
CAN_HEIGHT = MAP_HEIGHT // SCALE
# Size of magnifier pane and initial center
MAG_SIZE = 120
mag_pos = [CAN_WIDTH // 2, CAN_HEIGHT // 2]
# Event handlers
# Move magnifier to clicked position
def click(pos):
global mag_pos
mag_pos = list(pos)
# Draw map and magnified region
def draw(canvas):
# Draw map
canvas.draw_image(image,
[MAP_WIDTH // 2, MAP_HEIGHT // 2], [MAP_WIDTH, MAP_HEIGHT],
[CAN_WIDTH // 2, CAN_HEIGHT // 2], [CAN_WIDTH, CAN_HEIGHT])
# Draw magnifier
map_center = [SCALE * mag_pos[0], SCALE * mag_pos[1]]
map_rectangle = [MAG_SIZE, MAG_SIZE]
mag_center = mag_pos
mag_rectangle = [MAG_SIZE, MAG_SIZE]
canvas.draw_image(image, map_center, map_rectangle, mag_center, mag_rectangle)
# Create frame for scaled map
frame = simplegui.create_frame("Map magnifier", CAN_WIDTH, CAN_HEIGHT)
# register even handlers
frame.set_mouseclick_handler(click)
frame.set_draw_handler(draw)
# Start frame
frame.start()