Related
I have an 2-D array that I want to find the max value per row and then find the next max-value that is not within +/- n of the previous value. For example I have the following matrix:
results =
array([[ 33, 108, 208, 96, 96, 112, 18, 208, 33, 323, 60, 42],
[ 51, 6, 39, 112, 160, 144, 342, 195, 27, 136, 42, 54],
[ 12, 176, 266, 162, 45, 70, 156, 198, 143, 56, 342, 130],
[ 22, 288, 304, 162, 21, 238, 156, 126, 165, 91, 144, 130],
[342, 120, 36, 51, 10, 128, 156, 272, 32, 98, 192, 288]])
row_max_index = results.argmax(1)
row_max_index #show max index
array([ 9, 6, 10, 2, 0])
Now I'd like to get the next max value not within say +/- 2 of the current max.
Here is what I have but it feels sloppy:
maskIndx = np.c_[row_max_index-2, row_max_index-1, row_max_index, row_max_index+1, row_max_index+2,]%12
maskIndx #show windowed index
array([[ 7, 8, 9, 10, 11],
[ 4, 5, 6, 7, 8],
[ 8, 9, 10, 11, 0],
[ 0, 1, 2, 3, 4],
[10, 11, 0, 1, 2]])
results[np.meshgrid(np.arange(5), np.arange(5))[1], maskIndx] = 0 #uses array indexing
results #show results
array([[ 33, 108, 208, 96, 96, 112, 18, 0, 0, 0, 0, 0],
[ 51, 6, 39, 112, 0, 0, 0, 0, 0, 136, 42, 54],
[ 0, 176, 266, 162, 45, 70, 156, 198, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 238, 156, 126, 165, 91, 144, 130],
[ 0, 0, 0, 51, 10, 128, 156, 272, 32, 98, 0, 0]])
next_max_index = results.argmax(1)
array([2, 9, 2, 5, 7])
Any ideas on doing this faster through indexing/windowing?
You can create a mask around the indices you compute for the max by taking an array of indices and subtracting the relevant max_indices, and then use the masked api to recompute the argmax:
import numpy as np
result = ... # Result here
row_max_index = results.argmax(axis=1, keepdims=True)
indices = np.arange(results.shape[1])
mask = np.abs(indices - row_max_index) <= 2
out = np.ma.array(results, mask=mask).argmax(axis=1)
I'm looking how to create a matrix of 20x20 using Numpy broadcasting, result should look like:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, 102, 108, 114, 120, 0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108, 117, 126, 135, 144, 153, 162, 171, 180, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 0, 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132, 143, 154, 165, 176, 187, 198, 209, 220, 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 168, 180, 192, 204, 216, 228, 240, 0, 13, 26, 39, 52, 65, 78, 91, 104, 117, 130, 143, 156, 169, 182, 195, 208, 221, 234, 247, 260, 0, 14, 28, 42, 56, 70, 84, 98, 112, 126, 140, 154, 168, 182, 196, 210, 224, 238, 252, 266, 280, 0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 256, 272, 288, 304, 320, 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 272, 289, 306, 323, 340, 0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 180, 198, 216, 234, 252, 270, 288, 306, 324, 342, 360, 0, 19, 38, 57, 76, 95, 114, 133, 152, 171, 190, 209, 228, 247, 266, 285, 304, 323, 342, 361, 380, 0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 360, 380, 400]
I tried the following:
broadcast = np.arange(0, 21) *(20)
print(broadcast)
Got:
[0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400]
When providing a broadcast of a range to multiply by, such as: (0, 21) * (0, 21) it results in the below error:
ValueError: operands could not be broadcast together with shapes (21,) (2,)
Is it possible to broadcast by a range ?
Let's take your calculations step by step:
First make an array with arange:
In [166]: x = np.arange(5)
In [167]: x
Out[167]: array([0, 1, 2, 3, 4])
We can multiply it by a scalar:
In [168]: x * (5)
Out[168]: array([ 0, 5, 10, 15, 20])
The () add nothing here.
Then you try to multiply by a tuple:
In [169]: x * (0, 5)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-169-b6f23a3901bc> in <module>
----> 1 x * (0, 5)
ValueError: operands could not be broadcast together with shapes (5,) (2,)
I suspect you expected to somehow multiply the (0,5) 'range' by (0,5) 'range'. That's not how it works. (0,5) is a tuple. In the multiplication expression, the array x controls the action, so the tuple is turned into an array, np.array([0,2]). By broadcasting rules, a 5 element array can't multiply with a 2 element one.
Your first print suggests you want to multiply two arrays (n,) shape array to produces a (n,n) array, or (n*n,).
We could reshape x to (5,1):
In [170]: x[:,None]
Out[170]: # elsewhere this might called a column vector
array([[0],
[1],
[2],
[3],
[4]])
Now by broadcasting:
In [171]: x*x[:,None]
Out[171]:
array([[ 0, 0, 0, 0, 0],
[ 0, 1, 2, 3, 4],
[ 0, 2, 4, 6, 8],
[ 0, 3, 6, 9, 12],
[ 0, 4, 8, 12, 16]])
and if you want it as 1d:
In [172]: np.ravel(x*x[:,None])
Out[172]:
array([ 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 0, 2, 4, 6, 8, 0, 3,
6, 9, 12, 0, 4, 8, 12, 16])
The [171] action is: (5,) * (5,1) => (1,5) * (5,1) => (5,5)
The basic broadcasting rules are:
leading dimensions might be added to match
size 1 dimensions might be adjusted to match
This multiplication is like the outer or cartesian product of two arrays:
In [173]: np.outer(x,x)
Out[173]:
array([[ 0, 0, 0, 0, 0],
[ 0, 1, 2, 3, 4],
[ 0, 2, 4, 6, 8],
[ 0, 3, 6, 9, 12],
[ 0, 4, 8, 12, 16]])
I believe you are looking for something like this:
a = np.arange(21)
b = np.arange(21)
a[:, np.newaxis] * b
More can be found here: https://numpy.org/doc/stable/user/basics.broadcasting.html
To create a COCO dataset of annotated images, you need to convert binary masks into either polygons or uncompressed run length encoding representations depending on the type of object.
The pycocotools library has functions to encode and decode into and from compressed RLE, but nothing for polygons and uncompressed RLE.
I can use skimage's measure library to generate polygons of masks, but I'm not sure how to create uncompressed RLEs.
I can use this RLE encoder to create a representation of RLE from an image, but I'm not sure what format COCO expects. COCO just mentions that they use a "custom Run Length Encoding (RLE) scheme"
for example,
ground_truth_binary_mask = np.array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)
fortran_ground_truth_binary_mask = np.asfortranarray(ground_truth_binary_mask)
rle(fortran_ground_truth_binary_mask)
outputs:
(array([26, 36, 46, 56, 61]), array([3, 3, 3, 3, 1]))
this is what a coco RLE looks like:
{
"segmentation": {
"counts": [
272,
2,
4,
4,
4,
4,
2,
9,
1,
2,
16,
43,
143,
24,
5,
8,
16,
44,
141,
25,
8,
5,
17,
44,
140,
26,
10,
2,
17,
45,
129,
4,
5,
27,
24,
5,
1,
45,
127,
38,
23,
52,
125,
40,
22,
53,
123,
43,
20,
54,
122,
46,
18,
54,
121,
54,
12,
53,
119,
57,
11,
53,
117,
59,
13,
51,
117,
59,
13,
51,
117,
60,
11,
52,
117,
60,
10,
52,
118,
60,
9,
53,
118,
61,
8,
52,
119,
62,
7,
52,
119,
64,
1,
2,
2,
51,
120,
120,
120,
101,
139,
98,
142,
96,
144,
93,
147,
90,
150,
87,
153,
85,
155,
82,
158,
76,
164,
66,
174,
61,
179,
57,
183,
54,
186,
52,
188,
49,
191,
47,
193,
21,
8,
16,
195,
20,
13,
8,
199,
18,
222,
17,
223,
16,
224,
16,
224,
15,
225,
15,
225,
15,
225,
15,
225,
15,
225,
15,
225,
15,
225,
15,
225,
15,
225,
14,
226,
14,
226,
14,
39,
1,
186,
14,
39,
3,
184,
14,
39,
4,
183,
13,
40,
6,
181,
14,
39,
7,
180,
14,
39,
9,
178,
14,
39,
10,
177,
14,
39,
11,
176,
14,
38,
14,
174,
14,
36,
19,
171,
15,
33,
32,
160,
16,
30,
35,
159,
18,
26,
38,
158,
19,
23,
41,
157,
20,
19,
45,
156,
21,
15,
48,
156,
22,
10,
53,
155,
23,
9,
54,
154,
23,
8,
55,
154,
24,
7,
56,
153,
24,
6,
57,
153,
25,
5,
57,
153,
25,
5,
58,
152,
25,
4,
59,
152,
26,
3,
59,
152,
26,
3,
59,
152,
27,
1,
60,
152,
27,
1,
60,
152,
86,
154,
80,
160,
79,
161,
42,
8,
29,
161,
41,
11,
22,
2,
3,
161,
40,
13,
18,
5,
3,
161,
40,
15,
2,
5,
8,
7,
2,
161,
40,
24,
6,
170,
35,
30,
4,
171,
34,
206,
34,
41,
1,
164,
34,
39,
3,
164,
34,
37,
5,
164,
34,
35,
10,
161,
36,
1,
3,
28,
17,
155,
41,
27,
16,
156,
41,
26,
17,
156,
41,
26,
16,
157,
27,
4,
10,
25,
16,
158,
27,
6,
8,
11,
2,
12,
6,
2,
7,
159,
27,
7,
14,
3,
4,
19,
6,
160,
26,
8,
22,
18,
5,
161,
26,
8,
22,
18,
4,
162,
26,
8,
23,
15,
4,
164,
23,
11,
23,
11,
7,
165,
19,
17,
22,
9,
6,
167,
19,
22,
18,
8,
3,
170,
18,
25,
16,
7,
1,
173,
17,
28,
15,
180,
17,
30,
12,
181,
16,
34,
6,
184,
15,
225,
14,
226,
13,
227,
12,
228,
11,
229,
10,
230,
9,
231,
9,
231,
9,
231,
9,
231,
8,
232,
8,
232,
8,
232,
8,
232,
8,
232,
8,
232,
7,
233,
7,
233,
7,
233,
7,
233,
8,
232,
8,
232,
8,
232,
9,
231,
9,
231,
9,
231,
10,
230,
10,
230,
11,
229,
13,
227,
14,
226,
16,
224,
17,
223,
19,
221,
23,
217,
31,
3,
5,
201,
39,
201,
39,
201,
39,
201,
39,
201,
39,
201,
40,
200,
40,
200,
41,
199,
41,
199,
41,
199,
22,
8,
12,
198,
22,
12,
8,
198,
22,
14,
6,
198,
22,
15,
6,
197,
22,
16,
5,
197,
22,
17,
5,
196,
22,
18,
4,
196,
22,
19,
4,
195,
22,
19,
5,
194,
22,
20,
4,
194,
25,
21,
1,
193,
27,
213,
29,
211,
30,
210,
35,
6,
6,
193,
49,
191,
50,
190,
50,
190,
51,
189,
51,
189,
52,
188,
53,
187,
53,
187,
54,
186,
54,
186,
54,
186,
55,
185,
55,
185,
55,
185,
55,
185,
55,
185,
55,
185,
55,
185,
55,
185,
55,
185,
55,
185,
55,
185,
55,
185,
55,
185,
55,
185,
55,
185,
28,
1,
26,
185,
23,
11,
21,
185,
20,
17,
17,
186,
18,
21,
15,
186,
16,
23,
14,
187,
14,
25,
14,
187,
14,
26,
12,
188,
14,
28,
10,
188,
14,
226,
14,
226,
16,
224,
17,
223,
19,
221,
20,
220,
22,
218,
24,
18,
3,
12,
3,
180,
25,
10,
1,
4,
6,
10,
6,
178,
28,
7,
12,
8,
8,
177,
49,
3,
12,
176,
65,
175,
67,
173,
69,
171,
53,
3,
14,
170,
37,
20,
9,
4,
1,
169,
36,
21,
8,
175,
35,
22,
7,
176,
34,
23,
7,
176,
34,
23,
6,
177,
35,
22,
6,
177,
35,
22,
8,
175,
35,
23,
9,
173,
35,
205,
36,
204,
39,
201,
43,
197,
48,
36,
1,
155,
48,
35,
3,
154,
49,
33,
5,
154,
48,
32,
6,
155,
49,
27,
10,
155,
51,
24,
11,
154,
54,
21,
11,
155,
56,
19,
11,
155,
56,
18,
11,
156,
56,
17,
11,
157,
56,
16,
12,
157,
56,
14,
13,
159,
56,
12,
13,
160,
61,
5,
14,
162,
78,
165,
75,
167,
73,
168,
72,
170,
70,
171,
69,
173,
67,
176,
64,
179,
61,
182,
58,
183,
57,
185,
54,
187,
53,
188,
51,
191,
49,
192,
47,
195,
45,
196,
43,
198,
42,
199,
40,
201,
38,
203,
36,
205,
34,
207,
32,
210,
28,
213,
26,
216,
22,
221,
16,
228,
8,
10250
],
"size": [
240,
320
]
}
}
Information on the format is available here: https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/mask.py
RLE is a simple yet efficient format for storing binary masks. RLE
first divides a vector (or vectorized image) into a series of
piecewise constant regions and then for each piece simply stores the
length of that piece. For example, given M=[0 0 1 1 1 0 1] the RLE
counts would be [2 3 1 1], or for M=[1 1 1 1 1 1 0] the counts would
be [0 6 1] (note that the odd counts are always the numbers of zeros).
import numpy as np
from itertools import groupby
def binary_mask_to_rle(binary_mask):
rle = {'counts': [], 'size': list(binary_mask.shape)}
counts = rle.get('counts')
for i, (value, elements) in enumerate(groupby(binary_mask.ravel(order='F'))):
if i == 0 and value == 1:
counts.append(0)
counts.append(len(list(elements)))
return rle
test_list_1 = np.array([0, 0, 1, 1, 1, 0, 1])
test_list_2 = np.array([1, 1, 1, 1, 1, 1, 0])
print(binary_mask_to_rle(test_list_1))
print(binary_mask_to_rle(test_list_2))
output:
{'counts': [2, 3, 1, 1], 'size': [7]}
{'counts': [0, 6, 1], 'size': [7]}
You can use mask.frPyObjects(rle, size_x, size_y) to encode the RLE, and then do all the usual mask operations.
import json
import numpy as np
from pycocotools import mask
from skimage import measure
ground_truth_binary_mask = np.array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)
fortran_ground_truth_binary_mask = np.asfortranarray(ground_truth_binary_mask)
encode the mask to RLE:
rle = binary_mask_to_rle(fortran_ground_truth_binary_mask)
print(rle)
output:
{'counts': [6, 1, 40, 4, 5, 4, 5, 4, 21], 'size': [9, 10]}
compress the RLE, and then decode:
compressed_rle = mask.frPyObjects(rle, rle.get('size')[0], rle.get('size')[1])
mask.decode(compressed_rle)
output:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
As an improvement of #waspinator 's answer. This is 35% faster.
def binary_mask_to_rle(binary_mask):
rle = {'counts': [], 'size': list(binary_mask.shape)}
counts = rle.get('counts')
last_elem = 0
running_length = 0
for i, elem in enumerate(binary_mask.ravel(order='F')):
if elem == last_elem:
pass
else:
counts.append(running_length)
running_length = 0
last_elem = elem
running_length += 1
counts.append(running_length)
return rle
In order to decode the binary masks encoded in the COCO annotations, you need to first use the COCO's API to get the RLE and then use opencv to get the contours like this:
# Import libraries
import numpy as np
import cv2
import json
import mask
# Read the annotations
file_path = "coco/annotations/stuff_annotations_trainval2017/stuff_train2017.json"
with open(file_path, 'r') as f:
data = json.load(f)
updated_data = []
# For each annotation
for annotation in data['annotations']:
# Initialize variables
obj = {}
segmentation = []
segmentation_polygons = []
# Decode the binary mask
mask_list = mask.decode(annotation['segmentation'])
mask_list = np.ascontiguousarray(mask_list, dtype=np.uint8)
mask_new, contours, hierarchy = cv2.findContours((mask_list).astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Get the contours
for contour in contours:
contour = contour.flatten().tolist()
segmentation.append(contour)
if len(contour) > 4:
segmentation.append(contour)
if len(segmentation) == 0:
continue
# Get the polygons as (x, y) coordinates
for i, segment in enumerate(segmentation):
poligon = []
poligons = []
for j in range(len(segment)):
poligon.append(segment[j])
if (j+1)%2 == 0:
poligons.append(poligon)
poligon = []
segmentation_polygons.append(poligons)
# Save the segmentation and polygons for the current annotation
obj['segmentation'] = segmentation
obj['segmentation_polygons'] = segmentation_polygons
updated_data.append(obj)
Note: Only the COCO stuff 2017 annotations are using binary masks, the COCO person 2017 annotations aren't, so you don't need to decode the latter and find their contours.
Inspired by this solution.
Do it like this:
import numpy as np
from pycocotools import mask as m
# Create bool array
n_a = np.random.normal(size=(10, 10))
b_a = np.array(n_a > 0.5, dtype=np.bool, order='F')
# Encode bool array
e_a = m.encode(b_a)
d_a = m.decode(e_a)
# Decode byte string rle encoded mask
sz = pred['mask']['size']
c = pred['mask']['counts'][2:-1]
es = str.encode(c)
t = {'size': [450, 800], 'counts': es}
dm = m.decode(t)
i am doing conversion between images(alphabet I) and arr(np.array)
the mode is 'L', grayscale img.
there is no pixels like noise in np.array but when i did conversion.
img = Image.fromarray(arr)
img.save(path)
then there are noises in saved img.
ex> arr[0] components are all 255 but there is some noise in the first line of saved image.
i don't know why noise happens in images.
Problem is due to format of image.
Solution is to use non compressing format of image (such as .png or .gif)
Here is example which replicates this problem:
Sample files for this example are: 'demo.png' and 'demo.jpg' files which are equivalent in content.
import numpy as np
from scipy import misc
img_jpg = misc.imread('demo.jpg', mode='L')
img_png = misc.imread('demo.png', mode='L')
Here is output for variable img_jpg:
array([[ 17, 255, 82, 0, 74, 78, 0, 73],
[255, 255, 255, 255, 255, 255, 255, 255],
[134, 147, 146, 135, 131, 129, 131, 142],
[255, 255, 0, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255],
[ 3, 25, 255, 255, 129, 163, 0, 225],
[ 0, 255, 0, 0, 0, 0, 0, 0],
[ 0, 32, 64, 92, 130, 167, 196, 219],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
Here is output for variable img_png:
array([[ 17, 255, 82, 0, 74, 78, 0, 73],
[255, 255, 255, 255, 255, 255, 255, 255],
[134, 147, 146, 135, 131, 129, 131, 142],
[255, 255, 0, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255],
[ 3, 25, 255, 255, 129, 163, 0, 225],
[ 0, 255, 0, 0, 0, 0, 0, 0],
[ 0, 32, 64, 92, 130, 167, 196, 219],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
Lets try saving them to file:
misc.imsave('img_jpg.jpg', img_jpg)
misc.imsave('img_png.png', img_png)
Now lets recover them:
img_jpg = misc.imread('img_jpg.jpg', mode='L')
img_png = misc.imread('img_png.png', mode='L')
Here is output for variable img_jpg:
array([[ 26, 251, 84, 0, 75, 88, 0, 67],
[247, 253, 255, 250, 255, 235, 255, 255],
[133, 166, 124, 132, 141, 121, 135, 143],
[255, 235, 12, 255, 248, 255, 255, 250],
[251, 255, 255, 252, 255, 240, 251, 243],
[ 4, 36, 222, 255, 137, 163, 0, 247],
[ 0, 251, 14, 5, 0, 6, 0, 0],
[ 3, 27, 65, 89, 122, 170, 204, 222],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
Here is output for variable img_png:
array([[ 17, 255, 82, 0, 74, 78, 0, 73],
[255, 255, 255, 255, 255, 255, 255, 255],
[134, 147, 146, 135, 131, 129, 131, 142],
[255, 255, 0, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255],
[ 3, 25, 255, 255, 129, 163, 0, 225],
[ 0, 255, 0, 0, 0, 0, 0, 0],
[ 0, 32, 64, 92, 130, 167, 196, 219],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
You can observe that .jpg file got corrupted while .png file had its original content unchanged.
Kind regards.
I seem to have some problems dividing a integer with an numpy.array..
Doing this seem to return a array of zeroes, instead of decimals values..
Example:
>>> import numpy as np
>>> length = 257
>>> length = 256
>>> x = np.array(range(0,length)
... )
>>> x
array([ 0, 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, 136, 137, 138, 139, 140, 141, 142,
143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155,
156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168,
169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194,
195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246,
247, 248, 249, 250, 251, 252, 253, 254, 255])
>>> x*1/length
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0])
>>>
Why is it returning zeros?
That's because it is doing an integer division. Try this
x*1/256.0
Because the array consists out of integers, and in python-2.x / is defined as integer division which round down the result, so 99/100 is considered to be 0.
You can first cast to a float:
x.astype(float)*1/length
A float is a floating point and such datastructures - putting it bold - support a numbers with decimal dot.