Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the 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

I have been trying to make this work and have not figured out how to do it yet.

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 Answer

0 votes
by (36.8k points)

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

 If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Browse Categories

...