Calculate the difference between two datetime objects in Python
dateTimeDifference = dateTimeA - dateTimeB # Divide difference in seconds by number of seconds in hour (3600) dateTimeDifferenceInHours = dateTimeDifference.total_seconds() / 3600
dateTimeDifference = dateTimeA - dateTimeB # Divide difference in seconds by number of seconds in hour (3600) dateTimeDifferenceInHours = dateTimeDifference.total_seconds() / 3600
1- Add the bot to the group.
Go to the group, click on group name, click on Add members, in the searchbox search for your bot like this: @my_bot, select your bot and click add.
2- Send a dummy message to the bot.
You can use this example: /my_id @my_bot
(I tried a few messages, not all the messages work. The example above works fine. Maybe the message should start with /)
3- Go to following url: https://api.telegram.org/botXXX:YYYY/getUpdates
replace XXX:YYYY with your bot token
4- Look for “chat”:{“id”:-zzzzzzzzzz,
-zzzzzzzzzz is your chat id (with the negative sign).
References
https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id
1 –
'Hello, {}'.format(name)
Output
'Hello, Bob'
2 –
'Hey {name}, there is a 0x{errno:x} error!'.format(name=name, errno=errno)
Output
'Hey Bob, there is a 0xbadc0ffee error!'
3 –
f'Hello, {name}!'
Output
'Hello, Bob!'
pix = numpy.array(pic)
References
https://stackoverflow.com/questions/384759/how-to-convert-a-pil-image-into-a-numpy-array
Read shape of image
image:np.ndarray = cv2.imread("cow.jpg") print(image.shape)
Output
(559, 838, 3)
Show one channel
image: np.ndarray = cv2.imread("cow.jpg") # channels : BGR => 0,1,2 image2 = image[:, :, 1] # show channel 1 which is Green cv2.imshow("image", image2) cv2.waitKey(0) cv2.destroyAllWindows()
Manipulate channel
image: np.ndarray = cv2.imread("cow.jpg") image2 = image.copy() # channels : BGR => 0,1,2 # Here we want to show only Red channel image2[:, :, 0] = 0 image2[:, :, 1] = 0 cv2.imshow("image", image2) cv2.waitKey(0) cv2.destroyAllWindows()
Create an array from a list
my_list = [1, 2, 3] my_array = np.array(my_list)
Create an array with arrange
my_array = np.arange(0, 10) my_array2 = np.arange(0, 10, 2) # with step 2
Create an array with zeros
my_array = np.zeros((2, 3)) # default type if float my_array2 = np.zeros((2, 3), np.int32) # with type int
Create an array with ones
my_array = np.ones((10, 8), np.int32)
Create an array from random
np.random.seed(101) array1 = np.random.randint(0, 100, 10) array2=np.random.randint(0,100,10)
Max, Min, Mean
np.random.seed(101) array1 = np.random.randint(0, 100, 10) print(array1.max()) print(array1.argmax()) # index of max value print(array1.min()) print(array1.argmin()) # index of min value print(array1.mean())
Indexing an array
matrix = np.arange(0, 100).reshape(10, 10) print(matrix) print(matrix[2,3]) row = 1 col = 4 print(matrix[row,col])
Manipulating an array
matrix = np.arange(0, 100).reshape(10, 10) print(matrix) matrix[0:3, 0:3] = 0 print(matrix)
Copying an array
matrix: np.ndarray = np.arange(0, 100).reshape(10, 10) print(matrix) matrix2 = matrix.copy() matrix2[0:3, 0:3] = 0 print(matrix2)
Slicing an array
matrix = np.arange(0, 100).reshape(10, 10) print(matrix[1, :]) # whole row 1 print(matrix[:, 1]) # whole col 1 print(matrix[1, 1:4]) # row 1 from col 1 to 4 print(matrix[1:4, 1]) # col 1 from row 1 to 4 new_matrix: np.ndarray = matrix[1, :] print(new_matrix.reshape(5, 2))
Reshaping an array
np.random.seed(101) array1 = np.random.randint(0, 100, 10) print(array1) print(array1.shape) array2 = array1.reshape((2, 5)) print(array2) print(array2.shape)
Read Pixel
import cv2 import numpy as np import pprint image = cv2.imread('cow.jpg',cv2.IMREAD_COLOR) pixel= image[55,55] pprint.pprint(pixel)
Change Pixel
import cv2 import numpy as np import pprint image = cv2.imread('cow.jpg', cv2.IMREAD_COLOR) image[55, 55] = [255, 255, 255] pixel = image[55, 55] pprint.pprint(pixel)
Change the color of Region of Image
import cv2 import numpy as np import pprint image = cv2.imread('cow.jpg', cv2.IMREAD_COLOR) image[150:200, 100:150] = [255, 255, 255] cv2.imshow("image", image) cv2.waitKey(0) cv2.destroyAllWindows()
Image CharacteristicsÂ
import cv2 import numpy as np import pprint image = cv2.imread('cow.jpg', cv2.IMREAD_COLOR) print(image.shape) print(image.size) print(image.dtype)
Copy Paste Region
import cv2 import numpy as np import pprint image = cv2.imread('cow.jpg', cv2.IMREAD_COLOR) watch_face = image[200:250,107:194] image[0:50,0:87] = watch_face cv2.imshow('image',image) cv2.waitKey(0) cv2.destroyAllWindows()
References
https://pythonprogramming.net/image-operations-python-opencv-tutorial/
https://www.youtube.com/watch?v=1pzk_DIL_wo&list=PLQVvvaa0QuDdttJXlLtAJxJetJcqmqlQq&index=4
https://github.com/mhdr/OpenCV/tree/master/004
Drawing Line
# Draw a diagonal blue line with thickness of 5 px cv.line(img,(0,0),(511,511),(255,0,0),5)
Drawing Rectangle
cv.rectangle(img,(384,0),(510,128),(0,255,0),3)
Drawing Circle
cv.circle(img,(447,63), 63, (0,0,255), -1)
Drawing Ellipse
cv.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
Drawing Polygon
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32) pts = pts.reshape((-1,1,2)) cv.polylines(img,[pts],True,(0,255,255))
Adding Text to Images
font = cv.FONT_HERSHEY_SIMPLEX cv.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv.LINE_AA)
References
https://docs.opencv.org/4.0.1/dc/da5/tutorial_py_drawing_functions.html
https://www.youtube.com/watch?v=U6uIrq2eh_o&list=PLQVvvaa0QuDdttJXlLtAJxJetJcqmqlQq&index=3
https://pythonprogramming.net/drawing-writing-python-opencv-tutorial/
https://github.com/mhdr/OpenCV/tree/master/003
cap = cv2.VideoCapture(0) fourcc = cv2.VideoWriter_fourcc(*'XVID') # specify the codec out=cv2.VideoWriter('output.avi',fourcc,20.0,(640,480)) while True: ret, frame = cap.read() cv2.imshow("frame", frame) out.write(frame) # exit video capture if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() out.release() cv2.destroyAllWindows()
References
https://www.youtube.com/watch?v=Jvf5y21ZqtQ
https://github.com/mhdr/OpenCV/tree/master/002
import cv2 import numpy as np cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow("gray", gray) # exit video capture if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows()
References
https://www.youtube.com/watch?v=Jvf5y21ZqtQ
https://github.com/mhdr/OpenCV/tree/master/002