使用 PIL 和 SKIMAGE 打开 PGM 文件时出错 [英] Error Opening PGM file with PIL and SKIMAGE

查看:91
本文介绍了使用 PIL 和 SKIMAGE 打开 PGM 文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下图像文件:

I have following Image file:

Image

I used PIL and Skimage to open it but I get following errors

First with PIL (tried with and without trucate option): Code:

from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
img = Image.open("image_output.pgm")

Erorr:

OSError: cannot identify image file 'image_output.pgm'

And with Skimage:

Code:

from skimage import io
img = io.imread("image_output.pgm")

Error:

OSError: cannot identify image file <_io.BufferedReader name='image_output.pgm'>

I can open the file with GUI applications like system photo viewer and Matlab.

How can I diagnose what is wrong with image? I compared the byte data with other PGM files which I can open in Python, but could not identify the difference.

Thanks.

解决方案

Your file is P2 type PGM, which means it is in ASCII - you can view it in a normal text editor. It seems neither PIL, nor skimage want to read that, but are happy to read the corresponding P5 type which is identical except it is written in binary, rather than ASCII.

There are a few options...


1) You could use OpenCV to read it:

import cv2
im = cv2.imread('a.pgm')


2) You could convert it to P5 with ImageMagick and then read the output.pgm file with skimage or PIL:

magick input.pgm output.pgm


3) If adding OpenCV, or ImageMagick as a dependency is a real pain for you, it is possible to read a PGM image yourself:

#!/usr/bin/env python3

import re
import numpy as np

# Open image file, slurp the lot
with open('input.pgm') as f:
   s = f.read()

# Find anything that looks like numbers
# Technically, there could be comments that should be ignored
l=re.findall(r'[0-9P]+',s)

# List "l" will contain: P5, width, height, 255, pixel1, pixel2, pixel3...
# Technically, if l[3]>255, you should change the type of the Numpy array to uint16, but that is not the case
w, h = int(l[1]), int(l[2])

# Make Numpy image from data
ni = np.array(l[4:],dtype=np.uint8).reshape((h,w))

这篇关于使用 PIL 和 SKIMAGE 打开 PGM 文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆