簡體   English   中英

如何使用 function “圖像” Python 圖像庫中的像素數據寫入二進制文件?

[英]How do I write to a binary file using pixel data from the function “image” the Python Image Library?

我無法弄清楚如何做Ben Eater所做的事情。

我有完全相同的代碼(不同的文件名),但我得到的錯誤是我不能使用參數pixels[x,y] for chr() write二進制文件

我鏈接的視頻包含我想要完成的所有信息。 如果你有一個具體的問題要問我,請走開。 順便說一句......我一直在努力完成這項工作大約一年,但還沒有弄清楚如何做到這一點......是的。

'''

from PIL import Image

image = Image.open("Margarita3.png")
pixels = image.load()

out_file = open("Margarita3.bin", "wb")

for y in range(150):
  for x in range(200):
    try:
      out_file.write(chr(pixels[x, y]))
    except IndexError:
      out_file.write(chr(0))

'''

這是錯誤消息

    Traceback (most recent call last):
      File "C:\Users\Nicky\Desktop\tolaptop\wincupl_vga_timings\convert.py", line 
    11, in <module>
        out_file.write(chr(pixels[x,y]))
    TypeError: an integer is required

確保圖像位於正確的位置。 根據您當前的代碼,它應該與 python 腳本位於同一目錄中。 如果你想另外指定,你應該這樣做:

image = Image.open("C:\Users\Nicky\Desktop\tolaptop\...Margarita3.png")
pixels = image.load()

out_file = open("C:\Users\Nicky\Desktop\tolaptop\...Margarita3.bin", "wb")

暫無
暫無

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

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