Reading ETOPO tiff images into numpy array (Python) - python

I'm trying to read one of these images in Python https://www.ngdc.noaa.gov/mgg/global/relief/ETOPO2022/data/15s/15s_surface_elev_gtif/
They are greyscale tiff images. I managed to read them with the Pillow (PIL) library:
im = Image.open(filepath)
and all is well. I get something with dimensions 3600 x 3600, which seems reasonable.
Now I want to look at the pixel values so I try to create a numpy array:
imarray = numpy.array(im)
with various arguments, but I always end up with nothing (size = 1). Has anybody tried to read these images and successfully accessed the pixel values? Is the problem related to having 32 bit greyscale values?
I have also tried other ways, using imageio and cv2 libraries, but so far no luck. I'm relatively new to Python and these libraries, so maybe I'm heading in the wrong direction here. Has anyone tried and succeeded with these particular ones?

The images are compressed with "Adobe Deflate" compression. You can tell that with exiftool like this:
exiftool YOURIMAGE.TIF
...
...
Compression : Adobe Deflate
...
As that is the case, you need to have libtiff installed to decompress the images. You can tell that like this:
python3 -m PIL
--------------------------------------------------------------------
Pillow 9.1.1
Python 3.10.0 (v3.10.0:b494f5935c, Oct 4 2021, 14:59:19) [Clang 12.0.5 (clang-1205.0.22.11)]
--------------------------------------------------------------------
Python modules loaded from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL
Binary modules loaded from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 9.1.1
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.12.1
--- LITTLECMS2 support ok, loaded 2.14
--- WEBP support ok, loaded 1.2.4
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 2.1.3
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.0
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.12
--- LIBTIFF support ok, loaded 4.4.0
--- RAQM (Bidirectional Text) support ok, loaded 0.9.0, fribidi 1.0.12, harfbuzz 5.3.1
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
BLP
Extensions: .blp
Features: open, save, encode
--------------------------------------------------------------------
...
...
...
So, I imagine you need to install libtiff. How you do that depends on your OS.

Related

Different pixel values loading an image from stream in python2 and python3

I'm migrating some code from python2 to python3 which loads an image from a bytes stream. I need the resulting image to have the exact same values in all its pixels. However with the implementation below both images differ in some pixels on some values of intensity.
Here is the python 2.7 implementation:
from PIL import Image
from cStringIO import StringIO
import requests
img_url = "https://images.unsplash.com/photo-1508921912186-1d1a45ebb3c1?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8cGhvdG98ZW58MHx8MHx8&w=1000&q=80"
response = requests.get(img_url)
bytes_data = response.content
stream = StringIO(bytes_data)
img = Image.open(stream)
And here is the python3.8 implementation:
from PIL import Image
from io import BytesIO
import requests
img_url = "https://images.unsplash.com/photo-1508921912186-1d1a45ebb3c1?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8cGhvdG98ZW58MHx8MHx8&w=1000&q=80"
response = requests.get(img_url)
bytes_data = response.content
stream = BytesIO(bytes_data)
img = Image.open(stream)
In both virtual environments I'm using the same version of PIL (2.8.2) to make sure the differences don't come from there. (Edit 1)
The requests response returns the same, bytes_data is exactly the same bytes string in both implementations.
My guess is that the pixel differences come from the stream object, but I don't know much about the topic. (Edit 1)
Edit 1: If the PIL version is the same (2.8.2) in both python2.7 and python3.8 the results are the same. This would point out that the difference comes from the PIL version rather than the python version.
Edit 2: The loaded JPGS start differing at PIL version 9.0.0
That stream is JPEG data. JPEG is lossy and implementations of JPEG codecs are permitted to make different approximations and trade-offs in order to prefer performance or speed or compression ratio. Likewise, different versions of the same library are allowed to do so.
My guess would be that you have a different JPEG library version between the 2 environments. You should be able to check with:
python3 -m PIL | grep -i JPEG
and
python2.7 -m PIL | grep -I JPEG
Other ways to see the PIL version:
import PIL
print(PIL.__version__)
Or:
import PIL
print(dir(PIL))
Or from your shell outside your Python interpreter:
pip3 show Pillow
Name: Pillow
Version: 8.4.0
Summary: Python Imaging Library (Fork)
Home-page: https://python-pillow.org
Author: Alex Clark (PIL Fork Author)
Author-email: aclark#python-pillow.org
License: HPND
Location: /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages
Requires:
Required-by: CairoSVG, imageio, matplotlib

How to convert video on python to .mp4 without ffmpeg?

I need to convert videos to .mp4 format I used to ffmpeg but it converts for too long. Is there are a way to convert video to .mp4 in python without ffmpeg?
UPD moviepy depends on ffmpeg too (
==
Zulko/moviepy
pip install MoviePy
import moviepy.editor as moviepy
clip = moviepy.VideoFileClip("myvideo.avi")
clip.write_videofile("myvideo.mp4")
As per MoviePy documentation, there is no ffmpeg dependencies:
MoviePy depends on the Python modules Numpy, imageio, Decorator, and tqdm, which will be automatically installed during MoviePy's installation.
ImageMagick is not strictly required, but needed if you want to incorporate texts. It can also be used as a backend for GIFs, though you can also create GIFs with MoviePy without ImageMagick.
PyGame is needed for video and sound previews (not relevant if you intend to work with MoviePy on a server but essential for advanced video editing by hand).
For advanced image processing, you will need one or several of the following packages:
The Python Imaging Library (PIL) or, even better, its branch Pillow.
Scipy (for tracking, segmenting, etc.) can be used to resize video clips if PIL and OpenCV are not installed.
Scikit Image may be needed for some advanced image manipulation.
OpenCV 2.4.6 or a more recent version (one that provides the package cv2) may be needed for some advanced image manipulation.
Matplotlib
I wrote a quick program that will convert all video files of a particular type in a directory to another type and put them in another directory.
I had to install moviepy using Homebrew for it to work rather than rely on PyCharm's package installation.
import moviepy.editor as moviepy
import os
FROM_EXT = "mkv"
TO_EXT = "mp4"
SOURCE_DIR = "/Volumes/Seagate Media/Movies/MKVs"
DEST_DIR = "/Volumes/Seagate Media/Movies/MP4s"
for file in os.listdir(SOURCE_DIR):
if file.lower().endswith(FROM_EXT.lower()):
from_path = os.path.join(SOURCE_DIR, file)
to_path = os.path.join(DEST_DIR, file.rsplit('.', 1)[0]) + '.' + TO_EXT
print(f"Converting {from_path} to {to_path}")
clip = moviepy.VideoFileClip(from_path)
clip.write_videofile(to_path)

Openslide libjpeg error: Wrong JPEG library version

I'm working with Openslide's python bindings. I am using Tif images, which are supported by Openslide. It seems I am able to use the methods read_region and get_thumbnail with a smaller, binary masked Tif of about 100 mb's.
However, with a larger, RGBa Tif of about 1.5 Gb, I get the following error:
openslide.lowlevel.OpenSlideError: Wrong JPEG library version: library
is 90, caller expects 80
I have libjpeg8d installed, and everything seems fine with a smaller Tif. Any suggestions on how fix this issue?
I would guess your smaller TIFF is not JPEG-compressed, but your larger one is.
When libtiff starts the jpeg decoder, it checks that the version number in the libjpeg library binary matches the version number in the libjpeg headers that it was compiled against, and if they do not match, it prints the warning you are seeing.
The error means that you have installed a new jpeg library, but not recompiled libtiff or perhaps openslide.
You don't say what platform you are using, but on linux these issues should all be handled for you by your package manager, as long as you stick to the supported versions. If you've built any parts of the system yourself, you'll need to recheck how each part was configured and installed, and how your environment has been set up.

difference in using the import and from * import statement

When I install PIL http://www.pythonware.com/products/pil/ on 32 bit python, I can use import Image and then use the Image library inside PIL. However, for the 64 bit python, I downloaded http://www.lfd.uci.edu/~gohlke/pythonlibs/. However, when I use import Image, it gives me error, "No Module named Image". However, I can use from PIL import Image.
Why is that I can use import Image directly in one instance and not in another?
You want to install Pillow instead.
It comes in both 32 and 64 bit variants for Windows, and has fixed long-standing issues with the original PIL library.
The PIL library has long been broken when it comes to packaging. Pillow was started as a fork to fix these issues once and for all, and version 2.0.0 added Python 3 support (with a grant from the Python Software Foundation).
They're different libraries. Or, to be specific, they're different packaging of the same library.
PIL is notorious for these sort of issues. Just use Pillow instead.

How to solve IOError:broken data stream when reading image file?

Im working under Ubuntu 11.04. I'm trying to work with PIL in my Django project. Sadly PIL cannot load my image.
Here is the PIL setup summary:
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version 1.1.7
platform linux2 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2]
--------------------------------------------------------------------
*** TKINTER support not available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- FREETYPE2 support available
--- LITTLECMS support available
--------------------------------------------------------------------
I tried coding in python shell and it returns the error when I called load() function:
>>> import PIL
>>> import Image
>>> import _imaging
>>> i = Image.open("someimage.jpg")
>>> i
<JpegImagePlugin.JpegImageFile image mode=RGB size=600x599 at 0x9646C0C>
>>> i.load()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/joseph/Desktop/thesis/env/lib/python2.7/site-packages/PIL/ImageFile.py", line 215, in load
raise_ioerror(e)
File "/home/joseph/Desktop/thesis/env/lib/python2.7/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 am also working in a Virtual Environment, I am having problems with uploading jpeg image on my Django project. PNG works fine but it rejects JPEG
sudo apt-get install libjpeg8-dev
rebuild PIL and install again.
I had the same problem, but unfortunately we were using RHEL which only had libjpeg 6b.
Digging into this problem further, this error seems to occur when you are using a JPEG feature that isn't supported in the version of libjpeg you're using. In my case it was arithmetic encoding (introduced in version 7).
I found the wikipedia page on libjpeg to be helpful in determining which types of JPEG files could be opened by which versions. so...
TL;DR
Upgrade libjpeg if you can or re-save your images as baseline JPEG's.
Use Pillow and report issues as they occur and the Pillow team will fix them. Bonus points for submitting a pull request along with your issue report. Also see: https://stackoverflow.com/a/7770547/185820

Categories