簡體   English   中英

使用Pillow在Python中將數組轉換為圖像(tif)

[英]Converting Array into Image (tif) in Python using Pillow

我正在嘗試將array轉換為圖像( tif )以進行壓縮(在另一端將被撤消)。 但是,我遇到了第一個障礙...

我有以下幾點:

pillow_image = Image.fromarray(image_data)

這給了我這個錯誤:

  File "/Users/workspace/test-app/env/lib/python2.7/site-packages/PIL/Image.py", 

第2155行,位於fromarray arr = obj中。 array_interface AttributeError:“元組”對象沒有屬性“ array_interface

我在這里做錯了什么?

image_data是4個numpy數組的元組,每個數組(形狀可能為(H,W))。 您需要image_data為單個形狀數組(H,W,4)。 因此,請使用np.dstack合並通道。

您的至少一個陣列具有dtype int32。 但是要將其用作8位顏色通道,數組必須為dtype uint8 (因此最大值為255)。 您可以使用astype將數組轉換為astype uint8 希望您的數據不包含大於255的值。如果確實如此,則astype('uint8')將僅保留最低有效位(即,返回模256的數字)。

image_data = np.dstack(image_data).astype('uint8')
pillow_image = Image.fromarray(image_data)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM