Hello I am testing the getpixel() function in PIL but i keep getting "IndexError: image index out of range."
It works when i do this:
from PIL import ImageGrab, Image
image = ImageGrab.grab(bbox=(0, 0, 1920, 1080))
print(image.getpixel((10,10)))
But not when I do this:
from PIL import ImageGrab, Image
image = ImageGrab.grab(bbox=(1207, 301, 272, 537))
print(image.getpixel((10,10)))
Related
For a file read with:
import PIL
import tensorflow as tf
from keras_preprocessing.image import array_to_img
path_image = "path/cat_960_720.jpg"
read_image = PIL.Image.open(path_image)
# read_image.show()
image_decode = tf.image.decode_image(read_image)
print("This is the size of the Sample image:", image_decode.shape, "\n")
print("This is the array for Sample image:", image_decode)
resize_image = tf.image.resize(image_decode, (32, 32))
print("This is the Shape of resized image", resize_image.shape)
print("This is the array for resize image:", resize_image)
to_img = array_to_img(resize_image)
to_img.show()
I keep getting error for this line tf.image.decode_image(read_image):
ValueError: Attempt to convert a value
(<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=960x586 at
0x11AA49F40>) with an unsupported type (<class
'PIL.JpegImagePlugin.JpegImageFile'>) to a Tensor.
How can I pass imae read with PIL to tensorflow so that I could decode and resize,
so that I could resize this big picture to 32x32x3?
A few options, here is 1 if you have to use PIL:
import PIL
import tensorflow as tf
from keras_preprocessing.image import array_to_img
import numpy as np
path_image = "/content/cat.jpg"
read_image = np.asarray(PIL.Image.open(path_image))
resize_image = tf.image.resize(read_image, (32, 32))
print("This is the Shape of resized image", resize_image.shape)
print("This is the array for resize image:", resize_image)
to_img = array_to_img(resize_image)
Here is an option with TF only:
import tensorflow as tf
from keras_preprocessing.image import array_to_img
path_image = "/content/cat.jpg"
read_image = tf.io.read_file(path_image)
read_image = tf.image.decode_image(read_image)
resize_image = tf.image.resize(read_image, (32, 32))
print("This is the Shape of resized image", resize_image.shape)
print("This is the array for resize image:", resize_image)
to_img = array_to_img(resize_image)
I get an error when performing image processing for some images using python. The error is:
"AttributeError: type object 'Image' has no attribute 'open'".
I would be happy if you can review the code below and help me to fix this error.
#imprint text on image
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from IPython.display import Image
list2 = ['mydata/marketst670503.jpg','mydata/marketst8407.jpg','mydata/potsdamriot6805.jpg','mydata/rescue671221a.jpg']
outfile = 'sample-text.jpg'
for line in list2:
print (line)
img = Image.open(line)
draw = ImageDraw.Draw(img)
# font = ImageFont.truetype(<font-file>, <font-size>)
font = ImageFont.truetype("Colombia.ttf", 200)
draw.text((0, 0),"Sample Text",(255,0,0),font=font)
img.save(outfile)
display(Image(filename=outfile))
Notice! you have 2 imports under THE SAME NAME, overriding eachother.
I would make this change:
#mabe you replace the original import to a more nutral name..
import PIL.Image as Image
from PIL import ImageFont
from PIL import ImageDraw
from IPython.display import Image as DisplayImage # this line fixes your problem
list2 = ['mydata/marketst670503.jpg','mydata/marketst8407.jpg','mydata/potsdamriot6805.jpg','mydata/rescue671221a.jpg']
outfile = 'sample-text.jpg'
for line in list2:
print (line)
img = Image.open(line)
draw = ImageDraw.Draw(img)
# font = ImageFont.truetype(<font-file>, <font-size>)
font = ImageFont.truetype("Colombia.ttf", 200)
draw.text((0, 0),"Sample Text",(255,0,0),font=font)
img.save(outfile)
display(Image(filename=outfile))
and for next time, don't confuse yourself! choose a more nutral name then Image, and there won't be duplicates :)
*** Edited!***
Some weeks ago i load a png image into my Tkinter Canvas and drawed with create_image, but now i can't do this anymore, i tried convert with ImageTk but png did not display
I have the following code:
load = Image.open("mouse.png")
self.img = ImageTk.PhotoImage(load)
self.draw.create_image(100,100,image=self.img,anchor = NW)
self.draw.image = self.img
I just need to present a png image
Try this
vSmallIco = (15, 15)
self.original = Image.open('.//data//img//plus.png')
resized = self.original.resize(vSmallIco, Image.ANTIALIAS)
self.plusIco = ImageTk.PhotoImage(resized)
self.medIco = ttk.Label(self.mf, image=self.plusIco, style='Main.TLabel')
this is done with this import
from PIL import Image, ImageTk
Also, please use a *.png image if you wish to use png.
I would like to load an image in tkinter from a pygame surface and I am having a problem.
This is what I am currently trying:
image= pygame.image.tostring(surf, 'RGB')
tkimage= tkinter.PhotoImage(data= image)
canvas.create_image(0, 0, tkimage)
but I unfortunately get this error:
_tkinter.TclError: couldn't recognize image data
The PhotoImage class can only read GIF and PGM/PPM files, either directly from a file or as base64 encoded string.
You should use the Python Imaging Library for loading and creating the image for Tk.
Here's an example:
import pygame
from PIL import Image
import ImageTk
import Tkinter
# load image in pygame
pygame.init()
surf = pygame.image.load('bridge.png')
# export as string / import to PIL
image_str = pygame.image.tostring(surf, 'RGB') # use 'RGB' to export
w, h = surf.get_rect()[2:]
image = Image.fromstring('RGB', (w, h), image_str) # use 'RGB' to import
# create Tk window/widgets
root = Tkinter.Tk()
tkimage = ImageTk.PhotoImage(image) # use ImageTk.PhotoImage class instead
canvas = Tkinter.Canvas(root)
canvas.create_image(0, 0, image=tkimage)
canvas.pack()
root.mainloop()
------- UPDATE ------
import pygame
from pygame.locals import *
from PIL import Image
import ImageTk
import Tkinter
# load image in pygame
pygame.init()
surf = pygame.image.load('pic_temp.png') # you can use any Surface a Camers is also an Surface
mode = "RGB"
# export as string / import to PIL
image_str = pygame.image.tostring(surf, mode) # use 'RGB' to export
size = (640, 480)
#image = Image.fromstring(mode, size, image_str)
# use frombuffer() - fromstring() is no longer supported
image = Image.frombuffer(mode, size, image_str, 'raw', mode, 0, 1) # use 'RGB' to import
# create Tk window/widgets
root = Tkinter.Tk()
tkimage = ImageTk.PhotoImage(image) # use ImageTk.PhotoImage class instead
label = Tkinter.Label(root, image=tkimage)
label.pack()
root.mainloop()
I have the following PIL code to print text in an image
import os, sys
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
img = Image.open("one.jpg")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf",27)
draw.text((100, 100), "test test test", font=font)
img.save("out.jpg")
This works on one.jpg file. However on another test file called two.jpg, it doesn't print anything. From what I see, the only difference between two documents is the lower resolution on two.jpg. The file one.jpg is 200x200 dpi, two.jpg is 60x60 dpi.
How can I get draw.text to work in lower res?
Thanks,
You need to specify a color for the text:
import os
import sys
import ImageFont
import Image
import ImageDraw
img = Image.open("two.jpg")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf",27)
draw.text((100, 100), "test test test", font=font, fill = 'blue')
img.save("out.jpg")