Skip to content

Instantly share code, notes, and snippets.

View yuchen-xue's full-sized avatar
🏔️
Nature Lover

Yuchen Xue yuchen-xue

🏔️
Nature Lover
View GitHub Profile
@yuchen-xue
yuchen-xue / extract-pdf.py
Created May 10, 2025 06:42
A function for extracting text and images from a PDF file.
def extract_pdf(
pdf_path: str | os.PathLike,
img_dir: str | os.PathLike,
text_output_path: str | os.PathLike,
skip_first_image: bool = True,
) -> None:
"""
Extracts text and images from a PDF file.
This function extracts text from each page of the PDF and saves it to a text file.
It also extracts images from each page and saves them in a specified directory.
@yuchen-xue
yuchen-xue / gen-sine.py
Created May 8, 2025 08:45
Generate a sine wave signal with given amplitude and frequency
import numpy as np
import numpy.typing as npt
def generate_sine_wave(time_steps: npt.NDArray, amp: float, freq: float) -> npt.NDArray:
"""Generate a sine wave signal with given amplitude and frequency.
Args:
time_steps (npt.NDArray): _Time steps in the form of an 1D array_
amp (float): _Amplitude of the sine wave_
@yuchen-xue
yuchen-xue / experimentA.dat
Created May 5, 2025 20:13
Data of Software Lab
0.000000E+00 9.189000
2.000000E-03 9.189000
4.000000E-03 9.189000
6.000000E-03 9.189000
8.000000E-03 9.189000
1.000000E-02 9.189000
1.200000E-02 9.189000
1.400000E-02 9.189000
1.600000E-02 9.189000
1.800000E-02 9.189000
@yuchen-xue
yuchen-xue / resize-gif.py
Created April 23, 2025 20:56
Resize a gif file from a URL and store it locally
import cv2
import numpy as np
import imageio.v3 as iio
from pygifsicle import optimize
im = iio.imread(
'https://github.com/yuchen-xue/Spring-MySQL-TF-Detection/raw/master/asset/demo.gif', index=None)
stacked_frames = np.stack([
cv2.resize(frame, (640, 360), interpolation=cv2.INTER_AREA) for frame in im
], axis=0)
@yuchen-xue
yuchen-xue / resize-white-fill.py
Created April 12, 2025 12:42
Resize an image to a target width and height, while keeping the original height-width ratio. If the original height-width ratio is different from the target height-width ratio, fill the rest of the area with white pixels.
import argparse
from pathlib import Path
import cv2
import numpy.typing as npt
def resize_white_fill(img: npt.NDArray, w_target: int, h_target: int) -> npt.NDArray:
"""Resize an image to a target width and height, while keeping the original
height-width ratio. If the original height-width ratio is different from
@yuchen-xue
yuchen-xue / gist:9454fe6595c425f6e878409fe8482b90
Created November 14, 2024 21:13
This function is useful for printing out multiple passport photos. It duplicates the input image and produces a 3*3 duplication of the input image.
import cv2
import numpy as np
WHITE = (255, 255, 255)
def dup_img_9(
file_in: str, file_out: str, pad_horizontal: int = 10, pad_vertical: int = 10
):
"""
@yuchen-xue
yuchen-xue / img-stack.py
Last active March 8, 2025 19:12
This program reads images from a folder named "images" and produces a long image by stacking those images together.
# /// script
# requires-python = ">=3.4"
# dependencies = [
# "opencv-python",
# ]
# ///
from pathlib import Path
import cv2
import numpy as np
@yuchen-xue
yuchen-xue / Dockerfile
Last active February 2, 2024 06:48
Dockerfile for building pytorch, pyplot & cv2
##############################################################################
# Dockerfile for building an image with non-root privilege as default
# In addition, it's capable of displaying images created by
# `cv2.imshow()` and `plt.show()`
# In order to use the full functionality, one should
#
# 1. Run the command `xhost +local:docker`
#
# 2. build an image by including
@yuchen-xue
yuchen-xue / Dockerfile
Created February 1, 2024 12:52
Build opencv inside a docker image
# ref:
# https://github.com/Valian/docker-python-opencv-ffmpeg/blob/master/Dockerfile-py3-cuda
ARG CUDA_VERSION="10.0"
ARG FLAVOR="runtime"
FROM nvidia/cuda:${CUDA_VERSION}-cudnn7-${FLAVOR}-ubuntu16.04
ARG OPENCV_VERSION="4.1.0"
# Install all dependencies for OpenCV
RUN apt-get -y update && \
@yuchen-xue
yuchen-xue / main.py
Created February 1, 2024 12:50
ImportDocstring
import mymodule
print(mymodule.__doc__)
print(mymodule.f.__doc__)