Alternative way of connecting to PIL library? - python

Usually the library PIL is connected as follows:
from PIL import ImageTk, Image
I would like to connect it this way:
import PIL
but my version does not work. Here's the code:
import os, sys
import tkinter
import PIL
main = tkinter.Tk()
catalogImg1 = 'imgs'
nameImg1 = 'n.jpg'
pathImg1 = os.path.join(catalogImg1, nameImg1)
openImg = PIL.Image.open(pathImg1)
renderImg = PIL.ImageTk.PhotoImage(openImg)
tkinter.Label(main, image=renderImg).pack()
main.mainloop()
The error message is:
Traceback (most recent call last): File
"C:\Python33\projects\PIL_IMAGETK\ImageTK_photoimage - копия.py", line
11, in
openImg = PIL.Image.open(pathImg1) AttributeError: 'module' object has no attribute 'Image'

Importing a package (PIL) does not automatically import subpackages, submodules (PIL.Image, PIL.ImageTk). (Unless the package itself do it).
Explicitly import the submodules.
Replace following line:
import PIL
with:
import PIL.Image
import PIL.ImageTk

This is because, Image is a submodule within the PIL package i.e. It is not a function or class. Importing a package does not automatically import its submodules.
If you want to use the PIL namespace, you can import the module as follows:
import PIL.Image
openImg = PIL.Image.open(pathImg1)
If you want to import all the submodules of PIL, you can do the following
from PIL import *
openImg = Image.open(pathImg1)

Related

How do I edit multiple files in PIL

I was trying to edit alot of images at the same time using pil and python it shows me this error:
my code so far is below
import glob
import PIL
from PIL import Image
image = glob.glob('./*.png')
img = Image.open(image)
img.putalpha(127)
img.save("")
you may try this:
import glob
import PIL
from PIL import Image
for i in glob.glob('./*.png'):
img = Image.open(i)
img.putalpha(127)
img.save("")

Why i am getting "name 'os' is not defined" error?

Why i am getting this error? it should be work.Probally i'm missing something from my sight.
Before that same thing happen the for Classes.I tried rewrite and still same.
import tensorflow as tf
import cv2
import os
import matplotlib.pyplot as plt
import numpy as np
img_array = cv2.imread("Training/0/Training_233976.jpg")
img_array.shape
plt.imshow(img_array)
Datadirectory = "Training/"
Classes = ["0","1","2","3","4","5","6"]
for category in Classes:
path = os.path.join(Datadirectory, category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img))
plt.imshow(cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB))
plt.show()
break
break
I am formalizing in an answer, all you need to do is add a line at the top
import os
import os
use this at beginning of your code or where you are importing other libraries and codes .

Importing PIL ImageFont is giving a py3 import error

I'm trying to import a PIL ImageFont but when I do that, it gives me the following error:
<ipython-input-16-ef225ec0d8fd> in <module>()
----> 1 from PIL import ImageFont
/usr/local/lib/python3.6/dist-packages/PIL/ImageFont.py in <module>()
27
28 from . import Image
---> 29 from ._util import isDirectory, isPath, py3
30 import os
31 import sys
ImportError: cannot import name 'py3'
I can't find any similar issues on the internet related with this.

Adding noise to an image in increments

Hi I am trying to add noise to a QR image that I create, this is my code so far:
import numpy
import scipy
import scipy.misc
import sys
sys.path.append('M:/PythonMods')
import qrcode
if __name__ == "__main__":
myqr = qrcode.make("randomtexxxxxxxxxt")
#myqr.show()
myqr.save("M:/COMPUTINGSEMESTER2/myqr4.png")
filename = 'myqr4.png'
imagea = (scipy.misc.imread(filename)).astype(float)
poissonNoise = numpy.random.poisson(50,imagea.shape).astype(float)
noisyImage = imagea + poissonNoise
Please could someone advise me how I get it to show the noisy image? and how to save the image so I can test it?
Any help really appreciated.
edit
I tried adding this code to the program to get it to show the image:
from PIL import Image
myimage = Image.open(noisyImage)
myimage.load()
But then got this error:
Traceback (most recent call last):
File "M:\COMPUTINGSEMESTER2\untitled4.py", line 28, in <module>
myimage = Image.open(noisyImage)
File "Q:\PythonXY273_MaPS-T.v01\Python27\lib\site-packages\PIL\Image.py", line 1958, in open
prefix = fp.read(16)
AttributeError: 'numpy.ndarray' object has no attribute 'read'
Image.open needs an image file as parameter, use Image.fromarray:
im = Image.fromarray(noisyImage)
im.save("myFile.jpeg")
you may also use matplotlib module to show the image directly:
import matplotlib.pyplot as plt
plt.imshow(noisyImage) #Needs to be in row,col order
scipy.misc.imsave('NoisyImage.jpg', noisyImage)

Can not save file using the below python code. Error: numpy.ndarray object has no attribute 'save'

import os
import sys
import numpy as np
import scipy
import pylab
import pymorph
import mahotas
import matplotlib.pyplot as plt
import Image
from scipy import ndimage
from pymorph import regmax
from PIL import Image
path='all_images'
for file in os.listdir(path):
current = os.path.join(path, file)
extension = os.path.splitext(current)[-1]
fileType = extension.upper()
print(current)
if os.path.isfile(current):
img = mahotas.imread(current)
imgf = ndimage.gaussian_filter(img, 8)
pylab.gray()
imgf.save('dnaa.gif')
Can not save file using the below python code. Error: numpy.ndarray object has no attribute 'save'. Can anyone help how to save file using pylab. I guss the last line of the code has some issue.
Use mahotas.imsave('dnaa.gif', imgf) instead. The NumPy array you get from gaussian_filter doesn't have save functionality built in.

Categories