ADITYA SHARMA
ADITYA SHARMA

Reputation: 31

Getting 'OSError: -2' while converting a tif image into jpg image using python

I'm trying to convert tiff images into jpg format and use it later in opencv. It is working fine in my local system but when I am executing it over linux server which is not connected to internet it is getting failed while saving the Image object as jpg format.

I'm using python3.8 and had installed all the libraries and its dependencies using wheel files over server using pip.

Here is the piece of code:

import PIL
import cv2

def face_detect(sourceImagepath1, processedFileName, imagename, pdfname):
    temp_path = TEMP_PATH
    processed_path = PROCESSED_PATH
    misc_path = MISC_PATH
    # cascade file path1
    cascpath1 = misc_path + 'frontalface_cascade.xml'
    # Create harr cascade
    faceCascade = cv2.CascadeClassifier(cascpath1)
    # Read image with PIL
    image_pil = Image.open(sourceImagepath1)
    # Save image in jpg format
    image_pil.save(temp_path + processedFileName + '.jpg')
    # Read image with opencv
    image_cv = cv2.imread(temp_path + processedFileName + '.jpg')
    # Convert image into grayscale
    image_gray = cv2.cvtColor(image_cv, cv2.COLOR_BGR2GRAY)

    # Detect faces in the image
    face = faceCascade.detectMultiScale(
        image_gray,
        scaleFactor=1.3,
        minNeighbors=5,
        minSize=(30, 30)
        # flags = cv2.CASCADE_SCALE_IMAGE
    )

    if len(face) > 0:
        # Coordinates based on auto-face detection
        x, y, w, h = face[0][0], face[0][1], face[0][2], face[0][3]
        crop_image = image_pil.crop([x - 20, y - 30, x + w + 40, y + h + 60])
        crop_image.save(processed_path + imagename)
        # Save tif file as pdf
    image_pil.save(processed_path + pdfname, save_all=True)
    # Close image object
    image_pil.close()
    return len(face)

Here TEMP_PATH,PROCESSED_PATH,MISC_PATH are global variables of syntax like '/Users/user/Documents/Temp/'. I'm getting error on line:

image_pil.save(temp_path + processedFileName + '.jpg')

Below is the error i'm getting when executing the file

Traceback (most recent call last):
  File "*path_from_root_directory*/PYTHON_SCRIPTS/Script/staging.py", line 363, in <module>
    auto_face_count = face_detect(sourceImagepath1, processedFileName, imagename, pdfname)
  File "*path_from_root_directory*/PYTHON_SCRIPTS/Script/staging.py", line 71, in greyScaleCheck
    image_pil.save(temp_path + processedFileName + '.jpg')
  File "*path_from_root_directory*/python3.8/site-packages/PIL/Image.py", line 2201, in save
    self._ensure_mutable()
  File "*path_from_root_directory*/python3.8/site-packages/PIL/Image.py", line 624, in _ensure_mutable
    self._copy()
  File "*path_from_root_directory*/python3.8/site-packages/PIL/Image.py", line 617, in _copy
    self.load()
  File "*path_from_root_directory*/python3.8/site-packages/PIL/TiffImagePlugin.py", line 1122, in load
    return self._load_libtiff()
  File "*path_from_root_directory*/python3.8/site-packages/PIL/TiffImagePlugin.py", line 1226, in _load_libtiff
    raise OSError(err)
OSError: -2

I have provided full privileges to python directory and its sub-directories/files. Anyone have any idea why I'm getting this error ?

Upvotes: 0

Views: 42

Answers (0)

Related Questions