BYTE in image #7592
-
Hello everyone, help me fix the error. When running the code it gives the following error: Traceback (most recent call last):
File "C:/Users/Babaev/Desktop/fsf.py", line 9, in <module>
img = image.open(io.BytesIO(a))
File "C:\Users\Babaev\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py", line 3305, in open
raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000001AB6132F920> Code: import cv2 as cv
import numpy as np
import PIL.Image as image
import io
import base64
byte_data = input()
a = base64.b64decode(byte_data)
num = 0
img = image.open(io.BytesIO(a))
for i in range(img):
for j in i:
if list(j) == [255, 255, 255]:
num+=1
print(num) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
Hi. What is the value of Also, the following code doesn't look correct to me. for i in range(img):
for j in i:
if list(j) == [255, 255, 255]: I expect this is what you were after for x in range(img.width):
for y in range(img.height):
if img.getpixel((x, y)) == (255, 255, 255): |
Beta Was this translation helpful? Give feedback.
-
Thanks. So, there is a problem here in how you are passing the image data to Pillow. Your code attempts to decode base64-encoded data with You should also be aware that you cannot copy and paste the bytes that you have into >>> byte_data = input()
b'\xff\xd8'
>>> byte_data
"b'\\xff\\xd8'"
>>> byte_data = input()
\xff\xd8
>>> byte_data
'\\xff\\xd8' neither of those attempts becomes Maybe your intention was to base64 encode the bytes, so that you could paste that into A base64 encoded version of your data is
However, I find that too long to paste into Instead, I'm going to create a shorter example.
gives
If I copy and paste that when I run this code import PIL.Image as image
import base64
import io
byte_data = input()
a = base64.b64decode(byte_data)
num = 0
img = image.open(io.BytesIO(a))
for x in range(img.width):
for y in range(img.height):
if img.getpixel((x, y)) == (255, 255, 255):
num += 1
print(num) I get
|
Beta Was this translation helpful? Give feedback.
-
can you say more about base64, because i have got mistake |
Beta Was this translation helpful? Give feedback.
-
Looking at one of your deleted comments, it sounds like you are trying an answer a question from an online learning course. I don't expect that asking others about how to solve it is intended to be a part of that. |
Beta Was this translation helpful? Give feedback.
-
Yes, this is a course, my assignment was to write code. I tried but I got an error. I understand your code, but didn't inderstand how make my input() in base64 |
Beta Was this translation helpful? Give feedback.
Thanks. So, there is a problem here in how you are passing the image data to Pillow.
Your code attempts to decode base64-encoded data with
base64.b64decode(byte_data)
- but the data that you posted is not base64 encoded.You should also be aware that you cannot copy and paste the bytes that you have into
input()
and have Python get bytes as a result. If I try and paste in a shorter example,neither of those attempts becomes
b'\xff\xd8'
Maybe your intention was to base64 encode the bytes, so that you could paste that into
input()
. To base64 encode a variable, you ca…