Skip to content

Instantly share code, notes, and snippets.

@vmfunc
Created March 12, 2025 05:14
Show Gist options
  • Save vmfunc/e51a6e40d236a5c8ada51b0dbcf3f2e5 to your computer and use it in GitHub Desktop.
Save vmfunc/e51a6e40d236a5c8ada51b0dbcf3f2e5 to your computer and use it in GitHub Desktop.
convert ida graph pdf to png
import subprocess
from PIL import Image
import numpy as np
import sys
def convert_flowgraph_to_highres_png(input_path, output_path):
subprocess.run([
'gswin64',
'-sDEVICE=pngalpha',
f'-o', output_path,
'-dLastPage=1',
'-dFirstPage=1',
'-dNOPAUSE',
'-dBATCH',
'-dSAFER',
'-r900',
'-dGraphicsAlphaBits=4',
'-dTextAlphaBits=4',
input_path
], check=True)
def process_image(image_path, output_path, padding=25):
img = Image.open(image_path)
img_array = np.array(img)
if img_array.shape[2] == 4:
mask = (img_array[:,:,0] != 255) | (img_array[:,:,1] != 255) | \
(img_array[:,:,2] != 255) | (img_array[:,:,3] != 0)
else:
mask = (img_array[:,:,0] != 255) | (img_array[:,:,1] != 255) | (img_array[:,:,2] != 255)
rows = np.any(mask, axis=1)
cols = np.any(mask, axis=0)
rmin, rmax = np.where(rows)[0][[0, -1]]
cmin, cmax = np.where(cols)[0][[0, -1]]
rmin = max(0, rmin - padding)
rmax = min(img_array.shape[0] - 1, rmax + padding)
cmin = max(0, cmin - padding)
cmax = min(img_array.shape[1] - 1, cmax + padding)
cropped_img = img.crop((cmin, rmin, cmax+1, rmax+1))
width, height = cropped_img.size
target_width = 2500
scale_factor = target_width / width
new_size = (int(width * scale_factor), int(height * scale_factor))
scaled_img = cropped_img.resize(new_size, Image.LANCZOS)
scaled_img.save(output_path, dpi=(900, 900), quality=100)
def main():
if len(sys.argv) < 2:
print("Usage: python script.py <input_file>")
sys.exit(1)
input_path = sys.argv[1]
output_path = input_path.rsplit('.', 1)[0] + '.png'
temp_png_path = 'temp_output.png'
convert_flowgraph_to_highres_png(input_path, temp_png_path)
process_image(temp_png_path, output_path)
print(f"Image processed and saved to {output_path}")
if __name__ == '__main__':
# High-resolution flow graph image converter for technical diagrams
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment