Python Tutorials

Published: 05 Aug 2015 Category: programming_study

Draw rectangle:

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = np.zeros((512, 512, 3), np.uint8)
cv2.rectangle(img, (20, 20), (411, 411), (55, 255, 155), 2)
plt.imshow(img, 'brg')
cv2.imwrite("/path/to/save/img.jpg", img)

Use PIL to show ndarray:

import Image
import numpy as np
w,h = 512,512
data = np.zeros( (w,h,3), dtype=np.uint8)
data[256,256] = [255,0,0]
img = Image.fromarray(data, 'RGB')
img.save('my.png')

Convert numpy multi-dim ndarray to 1-dim array:

np.asarray(a).reshape(-1)

Remove duplicate elements

# Our input list.
values = [5, 5, 1, 1, 2, 3, 4, 4, 5]

# Convert to a set and back into a list.
set = set(values)
result = list(set)
print(result)

Output:

>>> [1, 2, 3, 4, 5] 

Simple operations

os.getcwd()
os.chdir(path)
os.path.dirname(os.path.abspath(__file__))
# sort array by column
from operator import itemgetter
a = ([2, 2, 2, 40], [5, 5, 5, 10], [1, 1, 1, 50], [3, 3, 3, 30], [4, 4, 4, 20])
sorted(a, key=itemgetter(3))
>>> [[5, 5, 5, 10], [4, 4, 4, 20], [3, 3, 3, 30], [2, 2, 2, 40], [1, 1, 1, 50]]
import numpy as np
np.amax
np.sum