티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
Working with TIFFs (import, export) in Python using numpy
Python에서 numpy를 사용하여 TIFF(가져오기, 내보내기) 작업
문제 내용
I need a python method to open and import TIFF images into numpy arrays so I can analyze and modify the pixel data and then save them as TIFFs again. (They are basically light intensity maps in greyscale, representing the respective values per pixel)
저는 TIFF 이미지를 numpy 배열로 열어 가져와 픽셀 데이터를 분석하고 수정한 다음 다시 TIFF로 저장할 Python 메소드가 필요합니다. (이미지는 각 픽셀에 해당하는 값이 표시된 회색조의 빛 강도 맵입니다)
I couldn't find any documentation on PIL methods concerning TIFF. I tried to figure it out, but only got "bad mode" or "file type not supported" errors.
저는 TIFF에 관한 PIL 메소드에 대한 문서를 찾을 수 없었습니다. 시도해보았지만 "bad mode" 또는 "file type not supported" 오류만 얻을 뿐이었습니다.
What do I need to use here?
여기서 무엇을 사용해야 할까요?
높은 점수를 받은 Solution
First, I downloaded a test TIFF image from this page called a_image.tif
. Then I opened with PIL like this:
먼저, 이 페이지에서 a_image.tif라는 테스트 TIFF 이미지를 다운로드했습니다. 그런 다음 다음과 같이 PIL로 열었습니다.
>>> from PIL import Image
>>> im = Image.open('a_image.tif')
>>> im.show()
This showed the rainbow image. To convert to a numpy array, it's as simple as:
이렇게 하면 무지개 이미지가 표시됩니다. numpy 배열로 변환하려면 간단합니다.
>>> import numpy
>>> imarray = numpy.array(im)
We can see that the size of the image and the shape of the array match up:
이미지의 크기와 배열의 모양이 일치하는 것을 확인할 수 있습니다.
>>> imarray.shape
(44, 330)
>>> im.size
(330, 44)
And the array contains uint8
values:
배열은 uint8 값을 포함합니다.
>>> imarray
array([[ 0, 1, 2, ..., 244, 245, 246],
[ 0, 1, 2, ..., 244, 245, 246],
[ 0, 1, 2, ..., 244, 245, 246],
...,
[ 0, 1, 2, ..., 244, 245, 246],
[ 0, 1, 2, ..., 244, 245, 246],
[ 0, 1, 2, ..., 244, 245, 246]], dtype=uint8)
Once you're done modifying the array, you can turn it back into a PIL image like this:
배열을 수정한 후에는 다음과 같이 다시 PIL 이미지로 변환할 수 있습니다.
>>> Image.fromarray(imarray)
<Image.Image image mode=L size=330x44 at 0x2786518>
가장 최근 달린 Solution
There is a nice package called tifffile
which makes working with .tif or .tiff files very easy.
tifffile이라는 멋진 패키지가 있습니다. 이 패키지를 사용하면 .tif 또는 .tiff 파일을 처리하는 것이 매우 쉬워집니다.
Install package with pip
패키지를 pip로 설치한 후
pip install tifffile
Now, to read .tif/.tiff file in numpy array format:
numpy 배열 형식으로 .tif/.tiff 파일을 읽으려면 다음과 같이합니다.
from tifffile import tifffile
image = tifffile.imread('path/to/your/image')
# type(image) = numpy.ndarray
If you want to save a numpy array as a .tif/.tiff file:
numpy 배열을 .tif/.tiff 파일로 저장하려면:
tifffile.imwrite('my_image.tif', my_numpy_data, photometric='rgb')
or
또는
tifffile.imsave('my_image.tif', my_numpy_data)
You can read more about this package here.
이 패키지에 대해 더 알아보려면 여기를 방문하세요.
출처 : https://stackoverflow.com/questions/7569553/working-with-tiffs-import-export-in-python-using-numpy
'개발 > 파이썬' 카테고리의 다른 글
파이썬에서 range()를 사용하여 리스트의 역순 출력 (0) | 2023.02.28 |
---|---|
판다스 데이터프레임 열(문자열)을 날짜로 변환 (0) | 2023.02.27 |
딕셔너리에서 키가 존재하는지 확인하고 값 증가시키기 (0) | 2023.02.26 |
파이썬 쉘에서 pyspark 모듈 가져오기 (0) | 2023.02.26 |
리스트에서 특정 값을 모두 제거하기 (0) | 2023.02.26 |