unable to read woff file with PIL in Python - python

I tried to execute ImageFont.truetype.
You can download the woff file here.
from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype('89874f10.woff', 40)
However, I received the following errors.
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
font = ImageFont.truetype('89874f10.woff', 10)
File "C:\Program Files\Python38\lib\site-packages\PIL\ImageFont.py", line 642, in truetype
return freetype(font)
File "C:\Program Files\Python38\lib\site-packages\PIL\ImageFont.py", line 639, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "C:\Program Files\Python38\lib\site-packages\PIL\ImageFont.py", line 187, in __init__
self.font = core.getfont(
OSError: invalid argument
What is the reason of error?
How to solve the problem?
Thank you very much.

I think you either need to install the freetype library to handle that, or convert the font to conventional Truetype using a converter such as this.

Related

Error in librosa.load('path.webm') RuntimeError: File contains data in an unknown format

I am trying to load sound files on a python script.
I installed, loaded the right version of modules, imported successfully librosa and soundfile, and even ffmpeg (which I found was a solution to this same error for mp3 files)
import os
import json
from scipy import signal
import librosa
[...]
y, sr = librosa.load(directory_path + filename + '.webm', mono = True)
My code works on notebooks (Kaggle) but somehow, and I can't figure out why, it doesn't work when uploaded on a computer cluster. And I really need the cluster for its computational power/memory. :/
The error:
Traceback (most recent call last):
File "/cluster/apps/nss/gcc-6.3.0/python/3.8.5/x86_64/lib64/python3.8/site-packages/librosa/core/audio.py", line 146, in load
with sf.SoundFile(path) as sf_desc:
File "/cluster/home/.local/lib/python3.8/site-packages/soundfile.py", line 740, in __init__
self._file = self._open(file, mode_int, closefd)
File "/cluster/home/.local/lib/python3.8/site-packages/soundfile.py", line 1264, in _open
_error_check(_snd.sf_error(file_ptr),
File "/cluster/home/.local/lib/python3.8/site-packages/soundfile.py", line 1455, in _error_check
raise RuntimeError(prefix + _ffi.string(err_str).decode('utf-8', 'replace'))
RuntimeError: Error opening 'input/00092.webm': File contains data in an unknown format.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "analysis_python.py", line 47, in <module>
y,sr = librosa.load(directory_path + filename + '.webm')
File "/cluster/apps/nss/gcc-6.3.0/python/3.8.5/x86_64/lib64/python3.8/site-packages/librosa/core/audio.py", line 163, in load
y, sr_native = __audioread_load(path, offset, duration, dtype)
File "/cluster/apps/nss/gcc-6.3.0/python/3.8.5/x86_64/lib64/python3.8/site-packages/librosa/core/audio.py", line 187, in __audioread_load
with audioread.audio_open(path) as input_file:
File "/cluster/apps/nss/gcc-6.3.0/python/3.8.5/x86_64/lib64/python3.8/site-packages/audioread/__init__.py", line 116, in audio_open
raise NoBackendError()
audioread.exceptions.NoBackendError
I perused the internet for a solution but no luck. Thank you for your help in advance.

Errors importing 16-bit TIFF in Pillow 7.1.2

I recently updated Pillow from version 6.1.2 to version 7.1.2 and tried to load a 16-bit tiff image, which I mainly work with on a daily basis. Prior to the update, I could load these images fine, but now it raises an error:
q = Image.open(file)
q
Traceback (most recent call last):
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\IPython\core\formatters.py", line 345, in __call__
return method()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\Image.py", line 671, in _repr_png_
self.save(b, "PNG")
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\Image.py", line 2100, in save
self._ensure_mutable()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\Image.py", line 617, in _ensure_mutable
self._copy()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\Image.py", line 610, in _copy
self.load()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\TiffImagePlugin.py", line 1070, in load
return self._load_libtiff()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\TiffImagePlugin.py", line 1182, in _load_libtiff
raise OSError(err)
OSError: -2
Out[21]: <PIL.TiffImagePlugin.TiffImageFile image mode=I;16 size=1024x1024 at 0x180FD99E438>
From looking at other answers, I see that Pillow has had issues with 16-bit images before but I thought they had been resolved. Is there a way to work around this error and access the image data? Or do I need to downgrade to my previous version?

Permission error: The process cannot access the file because it is being used by another process

I'm using windows 10, I'm using tesseract latest version for text recognition, below is the sample code I'm using now. But sometimes for some images, it gives the following.
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-
OCR\tesseract.exe'
config = ("-l eng --oem 1 --psm 7")
text=pytesseract.image_to_string(cv2.imread(r'C:\Users\Kesavan\Desktop\project\text Recogniton\opencv-text-recognition\images\cat.jpg'),config=config)
print(text)
Traceback (most recent call last):
File "C:/Users/Kesavan/PycharmProjects/Project/text recogniton/tester.py", line 6, in <module>
text=pytesseract.image_to_string(cv2.imread(r'C:\Users\Kesavan\Desktop\project\text Recogniton\opencv-text-recognition\images\cat.jpg'),config=config)
File "C:\Users\Kesavan\PycharmProjects\learning\venv\Learnings\lib\site-packages\pytesseract\pytesseract.py", line 350, in image_to_string
}[output_type]()
File "C:\Users\Kesavan\PycharmProjects\learning\venv\Learnings\lib\site-packages\pytesseract\pytesseract.py", line 349, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\Users\Kesavan\PycharmProjects\learning\venv\Learnings\lib\site-packages\pytesseract\pytesseract.py", line 265, in run_and_get_output
return output_file.read().decode('utf-8').strip()
File "C:\Users\Kesavan\AppData\Local\Programs\Python\Python37\lib\contextlib.py", line 119, in __exit__
next(self.gen)
File "C:\Users\Kesavan\PycharmProjects\learning\venv\Learnings\lib\site-packages\pytesseract\pytesseract.py", line 177, in save
cleanup(f.name)
File "C:\Users\Kesavan\PycharmProjects\learning\venv\Learnings\lib\site-packages\pytesseract\pytesseract.py", line 134, in cleanup
raise e
File "C:\Users\Kesavan\PycharmProjects\learning\venv\Learnings\lib\site-packages\pytesseract\pytesseract.py", line 131, in cleanup
remove(filename)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\Kesavan\\AppData\\Local\\Temp\\tess_jjp7wfoq.txt'
This is the image for which I'm facing the issue
Thanks in advance
try to upgrade to the latest pytesseract version or follow this workaround:
https://groups.google.com/d/msg/tesseract-ocr/IyPpisQ1E4U/tl3IP3gqAwAJ

Python Pillow - broken data stream

Pillow looks to be the solution to a lot of my problems, but I can't get it to work correctly. I think there might be something that I'm missing but I can't find any solutions that work.
from PIL import Image
img = Image.open("base64 (1).png")
print(img.size)
print(img.format)
img.show()
I have this set up to load an image, print its size and format, and then show the image. When I run the code, I get this:
(1920, 1080)
PNG
Traceback (most recent call last):
File "C:/Users/##########/PycharmProjects/weatherCanvas/imageTest.py", line 7, in <module>
img.show()
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1736, in show
_show(self, title=title, command=command)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2449, in _show
_showxv(image, **options)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2454, in _showxv
ImageShow.show(image, title, **options)
File "C:\Python27\lib\site-packages\PIL\ImageShow.py", line 51, in show
if viewer.show(image, title=title, **options):
File "C:\Python27\lib\site-packages\PIL\ImageShow.py", line 75, in show
image = image.convert(base)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 844, in convert
self.load()
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 250, in load
raise_ioerror(e)
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 59, in raise_ioerror
raise IOError(message + " when reading image file")
IOError: broken data stream when reading image file
The same thing happens if I replace img.show() with saving it. I know that PIL and Pillow save a temporary file in order to do the show() method, so I think the problem is somewhere in that.
Is there something I'm missing here?

Error with pydub in python

i have successfully imported pydub
but for the code:
from pydub import AudioSegment
song = AudioSegment.from_mp3("c:\mks.mp3")
first_ten_seconds = song[:10000]
song.export("d:\mks.mp3", format="mp3")
But it gives the following error:
python "C:\Users\mKs\Desktop\mks2.py"
Process started >>>
Traceback (most recent call last):
File "C:\Users\mKs\Desktop\mks2.py", line 2, in <module>
song=AudioSegment.from_mp3("c:\mks.mp3");
File "C:\Python27\lib\site-packages\pydub-0.5.2-py2.7.egg\pydub\audio_segment.py", line 194, in from_mp3
return cls.from_file(file, 'mp3')
File "C:\Python27\lib\site-packages\pydub-0.5.2-py2.7.egg\pydub\audio_segment.py", line 189, in from_file
return cls.from_wav(output)
File "C:\Python27\lib\site-packages\pydub-0.5.2-py2.7.egg\pydub\audio_segment.py", line 206, in from_wav
return cls(data=file)
File "C:\Python27\lib\site-packages\pydub-0.5.2-py2.7.egg\pydub\audio_segment.py", line 33, in __init__
raw = wave.open(StringIO(data), 'rb')
File "C:\Python27\lib\wave.py", line 498, in open
return Wave_read(f)
File "C:\Python27\lib\wave.py", line 163, in __init__
self.initfp(f)
File "C:\Python27\lib\wave.py", line 128, in initfp
self._file = Chunk(file, bigendian = 0)
File "C:\Python27\lib\chunk.py", line 63, in __init__
raise EOFError
EOFError
I would love to get help on this topic
The only issue that I see with your code is trailing ";" at the end of last 3 line. Please remove those, and see if you still get the error.
In addition, make sure you have ffmpeg (http://www.ffmpeg.org/) installed. It is required for the support of all of the none wav file formats.
ADDED:
I think you have broken module dependencies in your python installation.
I have tried code that you provided above with python 2.7.2. It worked fine for me:
>>> from pydub import AudioSegment
>>> song = AudioSegment.from_wav('goodbye.wav')
>>> first_ten_seconds = song[:10000]
>>> song.export('goodbye1.wav',format='wav')
<open file 'goodbye1.wav', mode 'wb+' at 0x10cf2b270>

Categories

Resources