-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_imagefile.py
More file actions
executable file
·135 lines (98 loc) · 3.67 KB
/
Copy pathlib_imagefile.py
File metadata and controls
executable file
·135 lines (98 loc) · 3.67 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
import pygame
def fn_comb(filename_in,addition,newsuffix=""):
'''inserts addition right before the suffix'''
filename_split = filename_in.split(".")
if newsuffix == "":
newsuffix = filename_split[-1]
new_filename=".".join(filename_split[:-1]) +"-"+addition +"." + newsuffix
return new_filename
def image_load(input_file):
'''loads an image via pygame and returns the image object'''
print "loading \"%s\""%input_file # should use cw command
return pygame.image.load(input_file)
def image_save(img, output_file):
'''saves an image via pygame to a file'''
print "saving \"%s\""%output_file # should use cw command
pygame.image.save(img,output_file)
def image_create(size, color=(0,0,0)):
'''generates image with single color'''
img_out = pygame.Surface(size)
img_out.fill(color)
return img_out
def image_show(display, img, reloc=True, dims=(3,3)):
'''shifts images in display and adds the new one ; fifo-like'''
w,h = dims
d = display.get_size()
# scale img to display/3th
newsize = [d[0]/w,d[1]/h]
resimg = pygame.transform.scale(img,newsize)
s = resimg.get_size()
if reloc:
storepic = image_create((newsize[0],d[1]))# store right row
storepic.blit(display,(-(d[0]-newsize[0]),0))
display.blit(display,(s[0],0)) # move display one line to the right
display.blit(storepic,(0,newsize[1]))
display.blit(resimg,(0,0))
pygame.display.flip()
def image_gui(insize,maxdsize=[1024,600]):
#maxdsize = [1024,600]
#maxdsize =
if not maxdsize:
maxdsize=insize
dsize = [insize[0]*4,insize[1]*4]
inratio = float(insize[1])/float(insize[0])
if dsize[0]>maxdsize[0]:
dsize[0]=maxdsize[0]
dsize[1]=int(dsize[0]*inratio)
if dsize[1]>maxdsize[1]:
dsize[1]=maxdsize[1]
dsize[0]=int(dsize[1]/inratio)
return pygame.display.set_mode(dsize)
def image_render_paths(paths,boundaries,zoom=4,color1=(128,0,0),color2=(0,255,0)):
rz=zoom
rendersize = [boundaries[0]*rz,boundaries[1]*rz]
img_path = image_create(rendersize,(0,0,0))
for polygon in paths:
lastpoint = False
if len(polygon)>1:
for point in polygon:
if not lastpoint:
lastpoint = point
pygame.draw.circle(img_path, color1,[int(point[0]*rz),int(point[1]*rz) ], 3)
pygame.draw.line(img_path, color2, [int(lastpoint[0]*rz),int(lastpoint[1]*rz)], [int(point[0]*rz),int(point[1]*rz)], 1)
lastpoint = point
return img_path
if __name__ == "__main__": # test!
print "testing fn_comb"
a = fn_comb("../foo/bar/xd.jpg","wasd123")
b = "../foo/bar/xd-wasd123.jpg"
if a != b:
print "Error: %s %s"%(a,b)
print "ok."
print "testing image_show, image_save and image_load"
a = image_show((320,240),(255,0,255))
image_save(a, "/tmp/hatching_test.jpg")
b = image_load("/tmp/hatching_test.jpg")
print "ok."
from lib_colors import *
from time import sleep
print "testing image_show"
# prepare
pygame.init()
dsize=(640,460)
display = pygame.display.set_mode(dsize)
for keepflop in range(0,3):
for j in range(1,5,1):
d=2**j
keep = True
# clear screen
image_show(display,image_show((1,1)),True,(1,1))
for i in range(0,d*d):
if keepflop == 1 or (keepflop == 2 and keep):
img = image_show((640,480),angle_to_color(360*i/(d*d)+180))
image_show(display, img, keepflop != 1,(d,d))
sleep(2.0/(d*d))
img = image_show((640,480),angle_to_color(360*i/(d*d)))
image_show(display, img, True,(d,d))
sleep(2.0/(d*d))