"ERROR: broken data stream when reading image file" (TrueType fonts libjpeg and PIL on OS X) - macos

I'm running OS X 10.6.7, Python 2.6, and writing a small wxPython application that uses PIL to manipulate an image and add some text to it.
After quite a bit of configuration woes with attempting to get PIL running with libjpeg and PIL, I followed the instructions at this site.
Upon running python setup.py build_ext -i (from inside the Imaging-1.1.7 folder) I get:
--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version 1.1.7
platform darwin 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- FREETYPE2 support available
--- LITTLECMS support available
--------------------------------------------------------------------
Additionally, running python selftest.py in the Imaging-1.1.7 install folder, I get:
--------------------------------------------------------------------
PIL 1.1.7 TEST SUMMARY
--------------------------------------------------------------------
Python modules loaded from ./PIL
Binary modules loaded from ./PIL
--------------------------------------------------------------------
--- PIL CORE support ok
--- TKINTER support ok
--- JPEG support ok
--- ZLIB (PNG/ZIP) support ok
--- FREETYPE2 support ok
--- LITTLECMS support ok
--------------------------------------------------------------------
Running selftest:
--- 57 tests passed.
However, when rendering my first PIL image in my own code, I still get:
File "/Library/Python/2.6/site-packages/PIL/Image.py", line 1290, in resize self.load()
File "/Library/Python/2.6/site-packages/PIL/ImageFile.py", line 215, in load raise_ioerror(e)
File "/Library/Python/2.6/site-packages/PIL/ImageFile.py", line 52, in raise_ioerror
raise IOError(message + " when reading image file")
IOError: broken data stream when reading image file
I'm kind of at a loss as to what to try next. Any ideas? The code that is causing this crash seems to be:
try:
self.img = self.img.resize((self.screenwidth, self.screenheight),
Image.ANTIALIAS)
except IOError, e:
print "Error resizing: " + str(e)
self.img = self.img.resize((self.screenwidth, self.screenheight))
raise

You should consider uninstalling PIL and following the instructions on this website -
http://jetfar.com/libjpeg-and-python-imaging-pil-on-snow-leopard/
In particular, it appears that you want to make sure that libjpeg is at version 6b, not 8c (see below)
I am able to reproduce this with Mac OS X 10.5 using macports
This is related to a another question:
How to solve IOError:broken data stream when reading image file?
The answer there indicates that the bug might have something to do with libjpeg
As far as I can tell, the error appears to be caused by a forward incompatibility with libjpeg v8c.
I cannot determine what is causing this exactly; all I can say is that on a variety of other systems I use that aren't exhibiting this behavior error (OS X 10.5 with fink, Redhat 5.2, Ubuntu 11.10 and 10.10), they all use libjpeg 6b. Only macports uses 8c, and macports is the only system where I am getting this error.

I was able to use PIL with the homebrew package manager.

I've got a similar error when reading .jpg file with an up-to-date Python environment and Sierra Mac OS. I've found a quick workaround which consists to convert all the .jpg files to .png files Hoping that can help someone else.

Related

No available image handler could decode this transfer syntax JPEG Lossless when read DICOM and ploting using matplotlib

When i use pydicom in python3.6, there are some problem:
import pydicom
import matplotlib.pyplot as plt
import os
import pylab
filePath = "/Users/zhuangrui/Documents/Python/Dicom/dicoms/zhang_bo/0001.dcm"
dataSet_1 = pydicom.dcmread(filePath)
plt.imshow(dataSet_1.pixel_array)
plt.show()
here is the problem:
How can this problem be solved? Thank you very much!
I've faced with the same problem, after doing some research on the suggested link above. I've managed to solve it by updating to the latest pydicom module "1.2.0" and installing gdcm. You can update the pydicom with
pip install -U git+https://github.com/pydicom/pydicom.git
You can find the latest gdcm here and this link explains the installation.
I use anaconda and it's easier to install the gdcm package and solve the problem. If you use anaconda
just type inside from your environment:
conda install pydicom --channel conda-forge to get pydicom's latest and
conda install -c conda-forge gdcm
to get the gdcm. This resolves the problem. Hope these will help.
With pydicom, you need an appropriate image handler also installed to handle compressed image types.
For JPEG lossless, in theory the following should work: jpeg_ls, gdcm, or Pillow with jpeg plugin. All of these also require Numpy to be installed. See the discussion at https://github.com/pydicom/pydicom/issues/532.
There is also a pull request in progress to add more descriptive error messages for what image handlers are needed for different images.
Problem:
I was trying to read medical images with .dcm extension. But was getting an error on Windows as well as on Ubuntu. I find a solution which will work on both the machined.
The error I got on Ubuntu is: NotImplementedError: this transfer syntax JPEG 2000 Image Compression (Lossless Only), can not be read because Pillow lacks the jpeg 2000 decoder plugin
(Note for Windows I was getting a different error but I am sure it's because of the same issue i.e. Pillow does not support JPEG 2000 format)
Platforma Information:
I am using: Python 3.6, Anaconda and Ubuntu, 15 GB RAM
RAM is important:
The solution I applied is the same as Ali explained above. But I want to add this installation may take time (depending on RAM you are using). On ubuntu where I am using 15 GB RAM on Cloud platform taken less time and on Windows on a local machine having 4 GB RAM taken a lot of time.
Solution
Anaconda is necessary. Why?
Please check the official doc of pydicom (https://pydicom.github.io/pydicom/dev/getting_started.html) its mentioned "To install pydicom along with image handlers for compressed pixel data, we encourage you to use Miniconda or Anaconda" (Note for Windows I was getting a different error)
If you are using Ubuntu directly open Terminal. If you are using Windows then on Anaconda Navigator go to Environment from here start terminal. Execute the following commands on it:
pip install -U git+https://github.com/pydicom/pydicom.git
conda install pydicom --channel conda-forge
conda install -c conda-forge gdcm
Cross Check:
Now use .dcm file for which we got the Error. Try to use the following code in Python notebook
filename = 'FileName.dcm'
ds = pydicom.dcmread(filename)
plt.imshow(ds.pixel_array, cmap=plt.cm.bone)
It should print the output. Also try this code:
ds.pixel_array
This will give you the array containing values.

Installing Cairo on Mac - Could not find libpng in the pkg-config search path

checking whether cairo's PNG functions feature could be enabled... no
I have been following the setup for configuring node canvas. Cairo is needed but i am having issues trying to set it up. https://github.com/Automattic/node-canvas/wiki/Installation---OSX
If libpng fails and you get the error "error: recommended PNG functions feature could not be enabled", try installing this binary and re-running the configure step.
I followed above by installing the binaries on Mac OS 9 and run the configuration command and still no luck. I am running the install shell script in node-canvas repository:
https://github.com/Automattic/node-canvas/blob/master/install
In my /usr/local/lib I currently have:
dtrace libjpeg.8.dylib libpng.a libpng14.a
libfreetype.6.dylib libjpeg.a libpng.dylib libpng14.dylib
libfreetype.a libjpeg.dylib libpng.la libpng14.la
libfreetype.dylib libjpeg.la libpng12.0.dylib libpng15.15.dylib
libfreetype.la libpixman-1.0.dylib libpng12.0.dylib.dSYM libpng15.15.dylib.dSYM
libgif.4.1.6.dylib libpixman-1.a libpng12.a libpng15.a
libgif.4.dylib libpixman-1.dylib libpng12.dylib libpng15.dylib
libgif.a libpixman-1.la libpng12.la libpng15.la
libgif.dylib libpng.3.dylib libpng14.14.dylib node_modules
libgif.la libpng.3.dylib.dSYM libpng14.14.dylib.dSYM pkgconfig
Libpng-1.6.10 has been replaced by libpng-1.6.12.
In your install script, change
LIBPNG="http://downloads.sourceforge.net/project/libpng/libpng16/1.6.10/libpng-1.6.10.tar.gz"
to
LIBPNG="http://downloads.sourceforge.net/project/libpng/libpng16/1.6.12/libpng-1.6.12.tar.gz"
I have a set of detailed instructions for building cairo here:
https://github.com/soegaard/racket-osx-libs
There resulting libraries are here:
https://github.com/soegaard/racket-osx-libs/tree/master/lib

imread() stopped working on Octave

I've tried googling it and it seems nobody else has had this kind of error.
I'm using Octave 3.6.1 on Ubuntu 12.04. I've been using the imread() funcion to read images for quite some time, but just yesterday it started throwing this error:
octave:5> I = imread('example.jpg');
error: imread: invalid image file: Magick++ exception: Magick: Unable to open file (tures/example.jpg) reported by coders/jpeg.c:955 (ReadJPEGImage)
error: called from:
error: /usr/share/octave/3.6.1/m/image/imread.m at line 74, column 7
If I try to read a PNG, I get a similar error:
octave:5> I = imread('example.png');
error: imread: invalid image file: Magick++ exception: Magick: Unable to open file (tures/example.png) reported by coders/png.c:2790 (ReadPNGImage)
error: called from:
error: /usr/share/octave/3.6.1/m/image/imread.m at line 74, column 7
Does anybody know what's happening? How to fix this?
Thanks a lot!
I had the same problem with 3.6.4 on Windows 8.1, the simple workaround for me was this:
IMAGE_PATH = ('C:\Users\...');
where the image is stored
Image = file_in_path(IMAGE_PATH, 'image.jpg');
Of course it would be better to fix the actual function, but for now it's good enough.
Hope this helps.
Octave uses GraphicsMagick (or ImageMagick) to handle the reading and writing of images. GraphicsMagick does not handle directly the reading and writing of images, it's a wrapper for the many format libraries under a single API.
When you build GraphicsMagick, it checks your system for available libraries and disables/activates formats as those are presents. You probably built GraphicsMagick yourself without the development libraries libpng and libjpeg. In Ubuntu, I believe those are in the packages libpng12-dev and libjpeg8-dev. Either use the GraphicsMagick libraries that come with your system or if you need to build it yourself, see the instructions in the Octave wiki.
To confirm this is indeed your problem, run gm -version and check if those formats are activated. For example, the output in my system shows:
Feature Support:
Thread Safe yes
Large Files (> 32 bit) yes
Large Memory (> 32 bit) no
BZIP yes
DPS no
FlashPix no
FreeType yes
Ghostscript (Library) no
JBIG yes
JPEG-2000 yes
JPEG yes
Little CMS yes
Loadable Modules no
OpenMP yes (201107)
PNG yes
TIFF yes
TRIO no
UMEM no
WMF yes
X11 yes
XML yes
ZLIB yes

Numpy on osx 10.8.2 and python 3.2.3

What EXACTLY do I have to do to get numpy to work? I've read that it's supported in 3.2.3 and that it should work by using setup.py. I'm getting errors talking about os_path. Is there something I'm missing?
Here is the full traceback:
Python 3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Converting to Python3 via 2to3...
Traceback (most recent call last):
File "/Users/cameron/Downloads/numpy-master/setup.py", line 214, in <module>
setup_package()
File "/Users/cameron/Downloads/numpy-master/setup.py", line 175, in setup_package
__file__ = os.path.join(os.curdir, os.path.basename(__file__))
NameError: global name '__file__' is not defined
>>>
You are probably using the Python version pre-installed with Mac OS X. I strongly suggest you use a distribution where all the components (python, scipy, numpy, etc) are built to work together properly, like macports. Macports is a package manager for Open Source software, and it automatically resolves and installs dependencies.
After the installation of Macports, the only thing you need to do is type "sudo port install py27-numpy" or "sudo port install py33-numpy" at the command prompt, depending on whether you prefer to use Python 2 or 3 (some packages might not run on Python3 yet). This will automatically download and install the correct version of python and all other software packages that might be required for numpy to run properly.
Try to compile a Python from scratch first (by downloading the source) and not using the one shipped by Apple. See the recommendation on the related SciPy/Numpy page.

Error in Installing the Cairo R Package

I am attempting to install the Cairo package in the development version of R on Mac OS X 10.7.4, but I'm running into an issue that I cannot resolve.
I have installed Cairo with homebrew (i.e., brew install cairo) and received the following message:
==> Caveats This formula is keg-only, so it was not symlinked into /usr/local.
Mac OS X already provides this program and installing another version
in parallel can cause all kinds of trouble.
The Cairo provided by Leopard is too old for newer software to link
against.
Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add its lib
& include paths to your build variables:
LDFLAGS -L/usr/local/Cellar/cairo/1.12.2/lib
CPPFLAGS -I/usr/local/Cellar/cairo/1.12.2/include
With the above message in mind, I attempted to install the Cairo package from R-Forge with the following R command:
install.packages("Cairo", repos="http://www.rforge.net/", configure.args = c("CAIRO_LIBS=/usr/local/Cellar/cairo/1.12.2/lib", "CAIRO_CFLAGS=/usr/local/Cellar/cairo/1.12.2/include/cairo"))
I receive the following error:
checking cairo.h usability... no
checking cairo.h presence... no
checking for cairo.h... no
configure: error: Cannot find cairo.h!
Please install cairo (http://www.cairographics.org/) and/or set
CAIRO_CFLAGS/LIBS correspondingly. ERROR: configuration failed for package ‘Cairo’
I repeated the same command without the repos argument (by default, I use the FHCRC mirror) with the same result.
At this point, the problem seems obvious: the file cairo.h is not present. But then I listed the files in the include/cairo directory. Here's the response:
ls /usr/local/Cellar/cairo/1.12.2/include/cairo
cairo-deprecated.h cairo-ft.h
cairo-ps.h
cairo-script-interpreter.h
cairo-svg.h
cairo-xcb.h
cairo-xlib.h
cairo-features.h
cairo-pdf.h
cairo-quartz.h
cairo-script.h
cairo-version.h
cairo-xlib-xrender.h
cairo.h
So, I'm pointing the installation of the Cairo package to the appropriate place, but the error persists. I have tried variations, such as CAIRO_CFLAGS=/usr/local/Cellar/cairo/1.12.2/include, with no luck.
Thoughts? Any help you can provide is greatly appreciated.
In case it's necessary, here is my sessionInfo:
R Under development (unstable) (2012-08-08 r60208) Platform: x86_64-apple-darwin11.4.0 (64-bit)
locale: [1]
en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages: [1] stats graphics grDevices utils
datasets methods base
other attached packages: [1] BiocInstaller_1.5.12
loaded via a namespace (and not attached): [1] tools_2.16.0
I had similar issue with cairo config error, and I already installed cairo with homebrew. Try below
brew install pkg-config
This solved mine problem.
A coworker and I just fixed the problem by ignoring all of what I typed above. Rather, we installed Cairo and all of its dependencies from source manually, thereby bypassing the usage of brew or variants like port. This worked like a charm, so I am not entirely sure what was wrong above.
After trying everything listed here, I found this blog post about installing the "full R package" using brew: https://luispuerto.net/blog/2018/05/11/installing-r-with-homebrew-with-all-the-capabilities/
What finally worked for me was building R explicitly with cairo (brew's default R tap uses --without-cairo and simply editing the tap did not solve the issue for me).
Works like a charm for me with R 4.0.2, cairo 1.16.0_3 under macOS Catalina 10.15.6.

Resources