Skip to content

Instantly share code, notes, and snippets.

View jdhao's full-sized avatar
:octocat:
Swimming 🏊 in the sea of code~~

jdhao jdhao

:octocat:
Swimming 🏊 in the sea of code~~
View GitHub Profile
@jdhao
jdhao / draw_rectangle.py
Last active June 5, 2023 19:49
Draw rectangle on image using OpenCV, but we start with PIL Image object and finally covert back to PIL Image object
# im is a PIL Image object
im_arr = np.asarray(im)
# convert rgb array to opencv's bgr format
im_arr_bgr = cv2.cvtColor(im_arr, cv2.COLOR_RGB2BGR)
# pts1 and pts2 are the upper left and bottom right coordinates of the rectangle
cv2.rectangle(im_arr_bgr, pts1, pts2),
color=(0, 255, 0), thickness=3)
im_arr = cv2.cvtColor(im_arr_bgr, cv2.COLOR_BGR2RGB)
# convert back to Image object
im = Image.fromarray(im_arr)
@jdhao
jdhao / benchmark_lambda_itemgetter.py
Last active February 14, 2022 06:41
Benchmark result of lambda and itemgetter used in sort method in Python
"""
Description: In this script, I plot the benchmark result of lambda compared
to itemgetter in the operator package. We sort a list of tuple (which has two
elements) to benchmark. The list element number ranges from 100 to 1000000.
"""
# import numpy as np
import matplotlib
import matplotlib.pyplot as plt
colors = ["#e6194b",
@jdhao
jdhao / crop_rectangle.py
Last active November 7, 2022 13:24
Given an image and a 4 points in the image (no 3 points are co-linear). Find the rotated rectangle enclosing the polygon formed by the 4 points and crop the rotated rectangle from the image. All done by OpenCV.
import cv2
import numpy as np
def main():
img = cv2.imread("test_image.jpg")
# assume coord is a list with 8 float values, the points of the rectangle area should
# have be clockwise
x1, y1, x2, y2, x3, y3, x4, y4 = coord
@jdhao
jdhao / draw_text_on_img_PIL.py
Last active March 27, 2021 11:20
Draw text on image using PIL or OpenCV. Two complete examples are given.
from PIL import Image, ImageFont, ImageDraw
im = Image.new('RGB', (1024, 1024), (255, 255, 255))
# in the font parameter, use a valid path to a font
font = ImageFont.truetype(
font="D:/code_exprt/fzyh.ttf",
size=250)
drawer = ImageDraw.Draw(im)
@jdhao
jdhao / get_unicode_code_point.py
Last active February 14, 2019 02:33
How to show the Unicode code point of a character or show the character corresponding to a certain code point in Python 3.
# show code point in hexadecimal format,
# see reference https://stackoverflow.com/questions/38909362/is-there-a-way-to-find-a-characters-unicode-code-point-in-python-2-7
hex(ord('我'))
# get the character corresponding to a code point,
# reference: https://stackoverflow.com/questions/10715669/python-unicode-codepoint-to-unicode-character
chr(0x6211) # the code point of 我 is \u6211
@jdhao
jdhao / update_mintty.md
Last active October 11, 2019 01:56
How to update mintty for msys2 or Ubuntu on Windows?

Msys2

To update mintty packaged with msys2, first download the latest version of mintty from here. Download the mintty tarball and extract it. Then copy mintty.exe to MSYS2_ROOT/usr/bin to replace the old mintty.exe file.

Restart msys2 and you should be albe to use the latest mintty. Reference here.

Ubuntu on Windows

Install the latest wsltty release from here. Mintty will be updated.

@jdhao
jdhao / login_to_windows_store.md
Created February 17, 2019 12:26
解决登录 Windows Store 遇到登录错误问题
@jdhao
jdhao / Sublime_text_regex_cheat_sheet.md
Last active April 23, 2024 08:58
Sublime Text regular expression cheat sheet, you can also find it in https://jdhao.github.io/2019/02/28/sublime_text_regex_cheat_sheet/

Special characters

expression Description
. Match any character
^ Match line begin
$ Match line end
* Match previous RE 0 or more times greedily
*? Match previous RE 0 or more times non-greedily
+ Match previous RE 1 or more times greedily
@jdhao
jdhao / .pdbrc
Created April 18, 2019 07:02
Pdb configurations
# Enable tab completion
import rlcompleter
import pdb
pdb.Pdb.complete=rlcompleter.Completer(locals()).complete
@jdhao
jdhao / .zshrc
Created April 18, 2019 07:25
Zsh configuration
# Path to your oh-my-zsh installation.
export ZSH="/home/haojiedong/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
# to use random theme, enable the following option
ZSH_THEME="random"