1

I cannot figure out how to do what Ben Eater did.

I have the exact same code (different file name) but the error I get is that I cannot use the argument pixels[x,y] for chr() to write to a binary file

The video I linked has all the information of what I am trying to accomplish. If you have a specific question for me, ask away. btw...I have literally been trying to make this work about a year and have not figured out how to do it so...yeah.

'''

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))

'''

here is the error message

    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

1 Answers1

0

Make sure the image is in the correct location. According to your current code, it should be in the same directory as the python script. If you want to specify otherwise, you should do it like so:

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

out_file = open("C:\Users\Nicky\Desktop\tolaptop\...Margarita3.bin", "wb")
Michael Hawkins
  • 2,793
  • 1
  • 19
  • 33
  • Thanks. You were actually spot on. I have it saved in two locations... And I was not exporting the image correctly I think. Do you have any recommendations to learn python for PIL and indexing arrays? (basically the above code is not going to give me exactly the format I need it in) – twistedhypvic67 Jun 27 '20 at 02:51
  • No problem, please consider up-voting or selecting as the answer if it was helpful. As for learning PIL, the best place is most likely the online documentation: https://pillow.readthedocs.io/en/stable/. What do you mean by indexing arrays? – Michael Hawkins Jun 27 '20 at 02:54
  • 1
    I cant really describe it very well so ill just tell you the problem I have. I have a memory device set up at 256x128 bytes. fixed dimensions. I have to fit the 200x150 image into it. I already have "the solution" but it takes a decent amount of thought to reorganize the data correctly when programming. I am familiar with C++ (not at all with python) and it has taken me a while to begin to figure out how to organize the data in C++. And I am a new member so my upvotes dont show. – twistedhypvic67 Jun 27 '20 at 03:05