Error While executing Google Vision API text detection from file - python

Hi I am learning Google vision API and I implemented the text detection from file where I have to draw bounding boxes to visualize the words, paragraphs and blocks. But during execution I am getting an error in Pillow library. Can someone help me out? I have mentioned the error below and part of the code down here:
CODE:
def draw_boxes(image, bounds, color, width=5):
draw = ImageDraw.Draw(image)
for bound in bounds:
draw.line([
bound.vertices[0].x, bound.vertices[0].y,
bound.vertices[1].x, bound.vertices[1].y,
bound.vertices[2].x, bound.vertices[2].y,
bound.vertices[3].x, bound.vertices[3].y,
bound.vertices[0].x, bound.vertices[0].y], fill=color, width=width)
return image```
#CODE ENDS
#Error
```Traceback (most recent call last):
File "/home/sridhar/PycharmProjects/documentOCR/venv/lib/python3.6/site-packages/PIL/ImagePalette.py", line 99, in getcolor
return self.colors[color]
KeyError: (255, 255, 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "myOCR.py", line 60, in <module>
draw_boxes(image, bounds, 'yellow')
File "myOCR.py", line 37, in draw_boxes
bound.vertices[0].x, bound.vertices[0].y], fill=color, width=width)
File "/home/sridhar/PycharmProjects/documentOCR/venv/lib/python3.6/site-packages/PIL/ImageDraw.py", line 155, in line
ink = self._getink(fill)[0]
File "/home/sridhar/PycharmProjects/documentOCR/venv/lib/python3.6/site-packages/PIL/ImageDraw.py", line 112, in _getink
ink = self.palette.getcolor(ink)
File "/home/sridhar/PycharmProjects/documentOCR/venv/lib/python3.6/site-packages/PIL/ImagePalette.py", line 109, in getcolor
self.palette[index + 256] = color[1]
IndexError: bytearray index out of range```
EDIT:
This error happens only to specific images, when I tried with a new image it works perfectly fine.

Related

TypeError: argument must be sequence while using Pillow

The goal is to overlay pfp on top of template, and then place the string description underneath pfp. When I run the code, I get the error TypeError: argument must be sequence.
def edit(template, pfp, description):
x = (template.size[0] - image.size[0])/2
y = (template.size[1] - image.size[1])/2
Image.Image.paste(template, pfp, (round(x), round(y)))
draw = ImageDraw.Draw(template)
draw.text(round((template.size[0]/8), round(y-15)), description)
template.show()
return template
Full error message:
Traceback (most recent call last):
File "/Users/shalim/PycharmProjects/EDPtigerstars/main.py", line 30, in <module>
edit(background, image, x[1]).save(x[0])
File "/Users/shalim/PycharmProjects/EDPtigerstars/main.py", line 18, in edit
draw.text(round((template.size[0]/8), round(y-15)), description)
File "/Users/shalim/PycharmProjects/EDPtigerstars/venv/lib/python3.8/site-packages/PIL/ImageDraw.py", line 512, in text
draw_text(ink)
File "/Users/shalim/PycharmProjects/EDPtigerstars/venv/lib/python3.8/site-packages/PIL/ImageDraw.py", line 496, in draw_text
self.draw.draw_bitmap(coord, mask, ink)
TypeError: argument must be sequence
Process finished with exit code 1
Try putting your x, y coordinates inside a tuple:
draw.text((x,y)), "Text")

mcpi-e. error when getting coordinates. mc.player.getPos()

Code:
from mcpi_e.minecraft import Minecraft
mc = Minecraft.create('127.0.0.1', 4711)
pos = mc.player.getPos()
When trying to get coordinates.
mc.player.getPos()
an error pops up
Error:
Traceback (most recent call last):
File "C:\Users\merka\Desktop\pyt minecraft\buy.py", line 23, in <module>
pos = mc.player.getPos()
File "C:\Users\merka\AppData\Local\Programs\Python\Python39\lib\site-packages\mcpi_e\minecraft.py", line 230, in getPos
return CmdPositioner.getPos(self, self.playerId)
File "C:\Users\merka\AppData\Local\Programs\Python\Python39\lib\site-packages\mcpi_e\minecraft.py", line 50, in getPos
s = self.conn.sendReceive(self.pkg + b".getPos", id)
File "C:\Users\merka\AppData\Local\Programs\Python\Python39\lib\site-packages\mcpi_e\connection.py", line 103, in sendReceive
return self.receive()
File "C:\Users\merka\AppData\Local\Programs\Python\Python39\lib\site-packages\mcpi_e\connection.py", line 97, in receive
raise RequestError("%s failed"%self.lastSent.strip())
mcpi_e.connection.RequestError: b'entity.getPos()' failed
Process finished with exit code 1
what can be done?
mc = Minecraft.create('127.0.0.1', 4711, "merka")
I should have added a nickname

PyOpenGL, pygame, and errors when drawing a shape

I've been writing a custom snake game using python, pygame, and pyopengl. I'm trying to draw a shape on the screen. However, I've stumbled upon this error:
Traceback (most recent call last):
File "F:\Projects\python\Python_Game\venv\lib\site-packages\OpenGL\latebind.py", line 43, in __call__
return self._finalCall( *args, **named )
TypeError: 'NoneType' object is not callable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\Projects\python\Python_Game\src\game.py", line 35, in <module>
main()
File "F:\Projects\python\Python_Game\src\game.py", line 31, in main
game.draw_shapes()
File "F:\Projects\python\Python_Game\src\game_classes.py", line 203, in draw_shapes
f.draw()
File "F:\Projects\python\Python_Game\src\game_classes.py", line 128, in draw
shape.draw()
File "F:\Projects\python\Python_Game\src\opengl_classes.py", line 128, in draw
glVertex2fv(cosine, sine)
File "F:\Projects\python\Python_Game\venv\lib\site-packages\OpenGL\latebind.py", line 47, in __call__
return self._finalCall( *args, **named )
File "F:\Projects\python\Python_Game\venv\lib\site-packages\OpenGL\wrapper.py", line 689, in wrapperCall
pyArgs = tuple( calculate_pyArgs( args ))
File "F:\Projects\python\Python_Game\venv\lib\site-packages\OpenGL\wrapper.py", line 450, in calculate_pyArgs
yield converter(args[index], self, args)
File "F:\Projects\python\Python_Game\venv\lib\site-packages\OpenGL\arrays\arrayhelpers.py", line 115, in asArraySize
byteSize = handler.arrayByteCount( result )
AttributeError: ("'NumberHandler' object has no attribute 'arrayByteCount'", <function asArrayTypeSize.<locals>.asArraySize at 0x000002642A35DCA0>)
The console is throwing me a TypeError and an Attribute error. I'm not sure if this is due to my code or an issue with one of the libraries. I'm using Python 3.9.1, pygame 2.0.1, and PyOpenGL 3.1.5.
Here's the snippet of my script where the issue arises:
class Circle:
def __init__(self, pivot: Point, radius: int, sides: int, fill: bool, color: Color):
self.pivot = pivot
self.radius = radius
self.sides = sides
self.fill = fill
self.color = color
# Draw the shape of the circle
def draw(self):
glColor3f(self.color.r, self.color.g, self.color.b)
if self.fill:
glBegin(GL_POLYGON)
else:
glBegin(GL_LINE_LOOP)
for i in range(100):
cosine = self.radius * cos(i*2*pi/self.sides) + self.pivot.x
sine = self.radius * sin(i*2*pi/self.sides) + self.pivot.y
glVertex2fv(cosine, sine)
glEnd()
The argument of glVertex2fv must be an array with 2 elements. If you have two separate coordinates which are not aggregated in an array you must use glVertex2f. See glVertex:
glVertex2fv(cosine, sine)
glVertex2f(cosine, sine)

Large number error with the Python Turtle

I am reading a set of coordinates from a file to produce a shape in the Python Turtle. After reading reading in the coordinates, I have written a for loop that goes through the list of coords and draws them. However, upon running with a correct file name, I get this error:
Traceback (most recent call last):
File "C:\Users\06113\Desktop\My Stuff\Python Saves\Vec Draw\Vec Draw.py", line 55, in <module>
t.goto(cs)
File "C:\Users\06113\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 1774, in goto
self._goto(Vec2D(*x))
File "C:\Users\06113\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 3195, in _goto
self._update() #count=True)
File "C:\Users\06113\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2660, in _update
self._update_data()
File "C:\Users\06113\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2650, in _update_data
self.screen._drawline(self.currentLineItem, self.currentLine,
File "C:\Users\06113\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 543, in _drawline
cl.append(x * self.xscale)
TypeError: can't multiply sequence by non-int of type 'float'
Is there any way at all that I could fix this in MY file? I would rather not go into the turtle module where I could easily mess something up.
The code for this section in my file is:
t = turtle.Turtle(visible=False)
t.speed(0)
for i in range(0, len(coord_list), 2):
if i == 0 and i+1 == 1:
fcs = (coord_list[i], coord_list[i+1])
t.pu()
t.goto(fcs)
t.pd()
pass
else:
cs = (coord_list[i], coord_list[i+1])
t.goto(cs)
cs = None
pass
pass
t.goto(fcs)
print("Vector image drawn.")

how to read an image from a function in PIL

how to read an image from a function in PIL ? in this scenerio i'm passing a image through paste_image function but it won't support PIL
def paste_image(image):
for i in range(epoches):
im2 = Image.open('/home/navaneeth/work/oneon/1.png')
x, y = im2.size
image.paste(im2, (0, 0, x, y))
image.save("test_"+str(i)+".jpg", "JPEG")
and i'm getting this error
Traceback (most recent call last):
File "main.py", line 109, in <module>
paste_image(image)
File "main.py", line 98, in paste_image
image.paste(im2, (0, 0, x, y))
AttributeError: 'numpy.ndarray' object has no attribute 'paste'
from PIL import Image
im2 = Image.open("/home/navaneeth/work/oneon/1.png")
You can use this code to get the result you want.

Categories