Intellipaat 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

2 Answers

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

0 votes
ago by (2k points)

For writing the pixel information of an image to a binary file format within the scope of Python Imaging Library (PIL, now called Pillow), you should consider the following steps: 

- Start by opening the image with the utilization of `Image.open()` method 

- Thereafter, transform the image into the required type such as fetching the Ror raw mode pixel data where the access is required. 

- The next step is where the image gets transformed to bytes thus the raw pixel data gets extracted

- And Finally, the binary file gets written with the pixel data.

```Python

Code Logic:

from PIL import Image

# Opens the image

image = Image.open('example_image.jpg')

# If image is already not in RGB mode then convert it to it.

image = image.convert('RGB')

# Get pixel data

pixel_data = image.tobytes()

# Opens the binary file to write

with open('image_pixels.bin', 'wb') as bin_file:

    bin_file.write(pixel_data)

```

Description: 

```Image.open()``` is responsible for finding the image file and opening it. 

```image.convert('RGB')``` makes sure the image is in RGB mode, this can be changed in accordance with the image mode (e.g. RGBA or L). 

    

```image.tobytes()``` Flattens the image and translates it into an array of pixel data. 

open('image_pixels.bin','wb') takes off a binary file ready for writing with the mode ensuring that the file is in binary.

And ```bin_file.write(pixel_data)``` is essencially used to convert the pixel data into binary file format. 

The pixels acquired after converting to binary are stored in image_pixel.bin file.

31k questions

32.9k answers

507 comments

693 users

Browse Categories

...