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.
Related
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")
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.
I'm trying to use Python and PIL to add some text to an image. I am failing on saving the resultant image as JPG.
I've based it on the example given on
https://pillow.readthedocs.io/en/5.2.x/reference/ImageDraw.html#example-draw-partial-opacity-text
from PIL import Image, ImageDraw, ImageFont
def example():
base = Image.open('test.jpg').convert('RGBA')
txt = Image.new('RGBA', base.size, (255,255,255,0))
fnt = ImageFont.truetype('/Library/Fonts/Chalkduster.ttf', 40)
drw = ImageDraw.Draw(txt)
drw.text((10,10), "HELLO", font=fnt, fill=(255,0,0,128))
result= Image.alpha_composite(base, txt)
result.convert('RGB')
print ('mode after convert = %s'%result.mode)
result.save('test1.jpg','JPEG')
example()
Running this prints mode after convert = RGBA
which is then followed by
Traceback (most recent call last):
File "/Users/carl/miniconda3/envs/env0/lib/python3.7/site-packages/PIL/JpegImagePlugin.py", line 620, in _save
rawmode = RAWMODE[im.mode]
KeyError: 'RGBA'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "example.py", line 14, in <module>
example()
File "example.py", line 12, in example
result.save('test1.jpg','JPEG')
File "/Users/carl/miniconda3/envs/env0/lib/python3.7/site-packages/PIL/Image.py", line 2007, in save
save_handler(self, fp, filename)
File "/Users/carl/miniconda3/envs/env0/lib/python3.7/site-packages/PIL/JpegImagePlugin.py", line 622, in _save
raise IOError("cannot write mode %s as JPEG" % im.mode)
OSError: cannot write mode RGBA as JPEG
The image is still RGBA after the convert to RGB function.
What am I doing wrong?
You missed to assign the output to result. Change this code below
old:
result.convert('RGB')
new:
result = result.convert('RGB')
I have a function which returns a pointer to the beginning of a sequence of bytes which correspond to 8 bit greyscale pixels. I'm trying to use PIL's frombuffer() function to create an image out of this. Looking here I did the following:
image_data = (1280*960*ctypes.c_ubyte)()
image_data = Frameptr
im = Image.frombuffer("L", (1280, 960), image_data, "raw", "L", 0, 1)
However, I still get this error message
Traceback (most recent call last):
File "_ctypes/callbacks.c", line 314, in 'calling callback function'
File "C:\Desktop\Program_2013\camera\framegrab.py", line 44, in FrameDataCallBack
im = Image.frombuffer("L", (1280, 960), image_data, "raw", "L", 0, 1)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1853, in frombuffer
core.map_buffer(data, size, decoder_name, None, 0, args)
ValueError: buffer is not large enough
Any help would be appreciated!
I need to flip a picture horizontally, without using the reverse function, I thought I had it right but the error I get is
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
Flip("bm.gif","bm.ppm")
File "C:\Users\....ImageProcessingSKLT.py", line 133, in Flip
pic1 = graphics.getPixel(x,y)
AttributeError: 'module' object has no attribute 'getPixel'
The code I have is
def Flip(image1, image2):
img = graphics.Image(graphics.Point(0, 0), image1)
X = img.getWidth()
Y = img.getHeight()
for y in range(Y//2):
for x in range(X):
pic1 = graphics.getPixel(x,y)
pic2 = graphics.setPixel(X-x,y)
temp = graphics.getColor(pic1)
graphics.setColor(pic1,getColor(pic2))
graphics.setColor(pic2,temp)
image2 = pic2
return image2
What does the error mean? and how do I fix it?
pic1 = graphics.getPixel(x,y)
pic2 = graphics.setPixel(X-x,y)
Probably should be:
pic1 = img.getPixel(x,y)
pic2 = img.setPixel(X-x,y)
The interpreter is complaining that it can't find the getPixel function inside the module graphics; it's img.getPixel, not graphics.getPixel.