Skip to content

Instantly share code, notes, and snippets.

@jigangkim
Created May 14, 2024 00:47
Show Gist options
  • Save jigangkim/a84bb1d529a3387717b9b7628bff39d7 to your computer and use it in GitHub Desktop.
Save jigangkim/a84bb1d529a3387717b9b7628bff39d7 to your computer and use it in GitHub Desktop.
Miscellaneous tools
import PyPDF2
def merge_pdfs(pdf_file1, pdf_file2, output):
pdf_writer = PyPDF2.PdfWriter()
for file in [pdf_file1, pdf_file2]:
pdf_reader = PyPDF2.PdfReader(file)
for page_num in range(len(pdf_reader.pages)):
page = pdf_reader.pages[page_num]
pdf_writer.add_page(page)
with open(output, 'wb') as out:
pdf_writer.write(out)
# Usage
merge_pdfs('./source1.pdf', './source2.pdf', 'merged.pdf')
import cairosvg
import glob
from PIL import Image
def convert_svg_to_jpg(svg_path, jpg_path):
# Convert SVG to PNG using CairoSVG
png_path = svg_path.replace('.svg', '.png')
cairosvg.svg2png(url=svg_path, write_to=png_path)
# Open the PNG file and convert it to JPG
img = Image.open(png_path)
img = img.convert('RGBA') # Ensure alpha channel is present
# Create a white image of the same size
white_img = Image.new('RGBA', img.size, 'WHITE') # Create a white rgba background
white_img.paste(img, (0, 0), img) # Paste the image on the background. Go from the start of the image (0,0)
# Convert image to RGB and save
white_img.convert('RGB').save(jpg_path, 'JPEG')
if __name__ == '__main__':
files = glob.glob('/home/path_to_svg_files/*.svg')
for file in files:
convert_svg_to_jpg(file, file.replace('.svg', '.jpg'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment