8
8
from system .localization import locale
9
9
10
10
11
- def join_image (img , pixels ):
11
+ def load_image_from_buffer (img ):
12
12
width , height = img .size
13
- loaded_img = img .load ()
14
- pixel_index = 0
15
- chunk_size = 32
13
+ img_loaded = img .load ()
16
14
17
- x_chunks_count = width // chunk_size
18
- y_chunks_count = height // chunk_size
19
- x_rest = width % chunk_size
20
- y_rest = height % chunk_size
15
+ with open ('pixel_buffer' , 'rb' ) as pixel_buffer :
16
+ channels_count = int .from_bytes (pixel_buffer .read (1 ), 'little' )
17
+ print (channels_count )
18
+
19
+ for y in range (height ):
20
+ for x in range (width ):
21
+ img_loaded [x , y ] = tuple (pixel_buffer .read (channels_count ))
22
+
23
+
24
+ def join_image (img ):
25
+ with open ('pixel_buffer' , 'rb' ) as pixel_buffer :
26
+ channels_count = int .from_bytes (pixel_buffer .read (1 ), 'little' )
27
+
28
+ width , height = img .size
29
+ loaded_img = img .load ()
30
+ chunk_size = 32
31
+
32
+ x_chunks_count = width // chunk_size
33
+ y_chunks_count = height // chunk_size
34
+ x_rest = width % chunk_size
35
+ y_rest = height % chunk_size
36
+
37
+ for y_chunk in range (y_chunks_count ):
38
+ for x_chunk in range (x_chunks_count ):
39
+ for y in range (chunk_size ):
40
+ for x in range (chunk_size ):
41
+ loaded_img [x_chunk * chunk_size + x , y_chunk * chunk_size + y ] = tuple (
42
+ pixel_buffer .read (channels_count )
43
+ )
21
44
22
- for y_chunk in range (y_chunks_count ):
23
- for x_chunk in range (x_chunks_count ):
24
45
for y in range (chunk_size ):
46
+ for x in range (x_rest ):
47
+ loaded_img [(width - x_rest ) + x , y_chunk * chunk_size + y ] = tuple (
48
+ pixel_buffer .read (channels_count )
49
+ )
50
+ Console .progress_bar (locale .join_pic , y_chunk , y_chunks_count )
51
+ for x_chunk in range (x_chunks_count ):
52
+ for y in range (y_rest ):
25
53
for x in range (chunk_size ):
26
- loaded_img [x_chunk * chunk_size + x , y_chunk * chunk_size + y ] = pixels [pixel_index ]
27
- pixel_index += 1
54
+ loaded_img [x_chunk * chunk_size + x , (height - y_rest ) + y ] = tuple (
55
+ pixel_buffer .read (channels_count )
56
+ )
28
57
29
- for y in range (chunk_size ):
30
- for x in range (x_rest ):
31
- loaded_img [(width - x_rest ) + x , y_chunk * chunk_size + y ] = pixels [pixel_index ]
32
- pixel_index += 1
33
- Console .progress_bar (locale .join_pic , y_chunk , y_chunks_count )
34
- for x_chunk in range (x_chunks_count ):
35
58
for y in range (y_rest ):
36
- for x in range (chunk_size ):
37
- loaded_img [x_chunk * chunk_size + x , (height - y_rest ) + y ] = pixels [pixel_index ]
38
- pixel_index += 1
59
+ for x in range (x_rest ):
60
+ loaded_img [x + (width - x_rest ), y + (height - y_rest )] = tuple (pixel_buffer .read (channels_count ))
39
61
40
- for y in range (y_rest ):
41
- for x in range (x_rest ):
42
- loaded_img [x + (width - x_rest ), y + (height - y_rest )] = pixels [pixel_index ]
43
- pixel_index += 1
44
62
63
+ def split_image (img : Image ):
64
+ def add_pixel (pixel : tuple ):
65
+ loaded_image [pixel_index % width , int (pixel_index / width )] = pixel
45
66
46
- def split_image (img ):
47
- pixels = []
48
67
width , height = img .size
49
- loaded_img = img .load ()
68
+ loaded_image = img .load ()
69
+ loaded_clone = img .copy ().load ()
50
70
chunk_size = 32
51
71
52
72
x_chunks_count = width // chunk_size
53
73
y_chunks_count = height // chunk_size
54
74
x_rest = width % chunk_size
55
75
y_rest = height % chunk_size
56
76
77
+ pixel_index = 0
78
+
57
79
for y_chunk in range (y_chunks_count ):
58
80
for x_chunk in range (x_chunks_count ):
59
81
for y in range (chunk_size ):
60
82
for x in range (chunk_size ):
61
- pixels .append (loaded_img [x + (x_chunk * chunk_size ), y + (y_chunk * chunk_size )])
83
+ add_pixel (loaded_clone [x + (x_chunk * chunk_size ), y + (y_chunk * chunk_size )])
84
+ pixel_index += 1
62
85
63
86
for y in range (chunk_size ):
64
87
for x in range (x_rest ):
65
- pixels .append (loaded_img [x + (width - x_rest ), y + (y_chunk * chunk_size )])
88
+ add_pixel (loaded_clone [x + (width - x_rest ), y + (y_chunk * chunk_size )])
89
+ pixel_index += 1
66
90
Console .progress_bar (locale .split_pic , y_chunk , y_chunks_count )
67
91
68
92
for x_chunk in range (width // chunk_size ):
69
93
for y in range (y_rest ):
70
94
for x in range (chunk_size ):
71
- pixels .append (loaded_img [x + (x_chunk * chunk_size ), y + (height - y_rest )])
95
+ add_pixel (loaded_clone [x + (x_chunk * chunk_size ), y + (height - y_rest )])
96
+ pixel_index += 1
72
97
73
98
for y in range (y_rest ):
74
99
for x in range (x_rest ):
75
- pixels . append ( loaded_img [x + (width - x_rest ), y + (height - y_rest )])
76
- img . putdata ( pixels )
100
+ add_pixel ( loaded_clone [x + (width - x_rest ), y + (height - y_rest )])
101
+ pixel_index += 1
77
102
78
103
79
104
def get_pixel_size (_type ):
@@ -89,17 +114,19 @@ def get_pixel_size(_type):
89
114
def pixel_type2str (_type ):
90
115
if _type in range (4 ):
91
116
return 'RGBA'
92
- if _type in ( 4 ,) :
117
+ elif _type == 4 :
93
118
return 'RGB'
94
- if _type in ( 6 ,) :
119
+ elif _type == 6 :
95
120
return 'LA'
96
- if _type in ( 10 ,) :
121
+ elif _type == 10 :
97
122
return 'L'
123
+
98
124
raise Exception (locale .unk_type % _type )
99
125
100
126
101
- def bytes2rgba (data : Reader , _type , img , pix ):
127
+ def bytes2rgba (data : Reader , _type , img ):
102
128
read_pixel = None
129
+ channels_count = 4
103
130
if _type in (0 , 1 ):
104
131
def read_pixel ():
105
132
return data .read_ubyte (), data .read_ubyte (), data .read_ubyte (), data .read_ubyte ()
@@ -112,30 +139,41 @@ def read_pixel():
112
139
p = data .read_uint16 ()
113
140
return (p >> 11 & 31 ) << 3 , (p >> 6 & 31 ) << 3 , (p >> 1 & 31 ) << 3 , (p & 255 ) << 7
114
141
elif _type == 4 :
142
+ channels_count = 3
143
+
115
144
def read_pixel ():
116
145
p = data .read_uint16 ()
117
146
return (p >> 11 & 31 ) << 3 , (p >> 5 & 63 ) << 2 , (p & 31 ) << 3
118
147
elif _type == 6 :
148
+ channels_count = 2
149
+
119
150
def read_pixel ():
120
151
return (data .read_ubyte (), data .read_ubyte ())[::- 1 ]
121
152
elif _type == 10 :
153
+ channels_count = 1
154
+
122
155
def read_pixel ():
123
156
return data .read_ubyte ()
124
157
125
- if read_pixel is not None :
158
+ if read_pixel is None :
159
+ return
160
+
161
+ with open ('pixel_buffer' , 'wb' ) as pixel_buffer :
162
+ pixel_buffer .write (channels_count .to_bytes (1 , 'little' ))
163
+
126
164
width , height = img .size
127
165
point = - 1
128
166
for y in range (height ):
129
167
for x in range (width ):
130
- pix .append (read_pixel ())
168
+ pixel = read_pixel ()
169
+ for channel in pixel :
170
+ pixel_buffer .write (channel .to_bytes (1 , 'little' ))
131
171
132
172
curr = Console .percent (y , height )
133
173
if curr > point :
134
174
Console .progress_bar (locale .crt_pic , y , height )
135
175
point = curr
136
- print ()
137
-
138
- img .putdata (pix )
176
+ print ()
139
177
140
178
141
179
def rgba2bytes (sc , img , _type ):
0 commit comments