extrabacon / python-shell

Run Python scripts from Node.js with simple (but efficient) inter-process communication through stdio
2.12k stars 224 forks source link

FileNotFoundError: [Errno 2] No such file or directory: 'pngquant' #300

Open whenmoon opened 1 year ago

whenmoon commented 1 year ago

Describe the bug My python script runs correctly when I run it using Python3 app.py but when using python-shell to run the script it fails with the error:

Traceback (most recent call last):
  File "/Users/user/Desktop/code/lang-sa/lang-segment-anything/app.py", line 68, in <module>
    heic_img.save('/Users/user/Downloads/IMG_4313.png')
  File "/Users/user/miniconda3/lib/python3.11/site-packages/heic2png/heic2png.py", line 53, in save
    subprocess.run(['pngquant', '--quality', quality_str, '-f', '-o', str(output_path), str(output_path)])
  File "/Users/user/miniconda3/lib/python3.11/subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/miniconda3/lib/python3.11/subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/Users/user/miniconda3/lib/python3.11/subprocess.py", line 1950, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'pngquant'

Python code Relevant Python code to reproduce the problem

import numpy as np
from lang_sam.utils import draw_image
from PIL import Image
from lang_sam import LangSAM
from heic2png import HEIC2PNG
import torch
from PIL import Image
from torchvision.utils import draw_bounding_boxes
from torchvision.utils import draw_segmentation_masks
import os
os.environ['CURL_CA_BUNDLE'] = ''

def draw_image(image, masks, boxes, labels, alpha=1):
    image = torch.from_numpy(image).permute(2, 0, 1)
    if len(boxes) > 0:
        image = draw_bounding_boxes(image, boxes, colors=['red'] * len(boxes), labels=labels, width=2)
    if len(masks) > 0:
        image = draw_segmentation_masks(image, masks=masks, colors=['white'] * len(masks), alpha=alpha)
    return image.numpy().transpose(1, 2, 0)

heic_img = HEIC2PNG('/Users/user/Downloads/IMG_4313.heic', quality=70)  # Specify the quality of the converted image
heic_img.save('/Users/user/Downloads/IMG_4313.png') <<<< error on this line
... 

Javascript code

const options = {
  pythonPath: '/Users/user/miniconda3/bin/Python3',
};

PythonShell.run('/Users/user/Desktop/code/lang-sa/lang-segment-anything/app.py', options).then(messages => {

Expected behavior A description of what you expected to happen. The script runs as normal without failure

Actual behavior A description of what actually happened. The script fails / errors. It's like it can't find the required libraries because we are running the script from a node server and not the directory of the script.

Other Information (please complete the following information):

Additional context Add any other context about the problem here.