Importing PIL ImageFont is giving a py3 import error - python

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.

Related

Having trouble importing cv2, outputting an importerror

Input
Trying to import these libraries and cv2 is throwing an error:
import argparse
import cv2
from datetime import datetime
import keyboard as key
import imutils
import mediapip as mp
import numpy as np
import os
import pandas as pd
from PIL import Image
import sys
import time
This is the error returned:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-ee20a49702d1> in <module>
1 import argparse
----> 2 import cv2
3 from datetime import datetime
4 import keyboard as key
5 import imutils
~/opt/anaconda3/lib/python3.8/site-packages/cv2/__init__.py in <module>
7
8 from .cv2 import *
----> 9 from .cv2 import _registerMatType
10 from . import mat_wrapper
11 from . import gapi
ImportError: cannot import name '_registerMatType' from 'cv2.cv2' (/Users/ryleigh_grove/opt/anaconda3/lib/python3.8/site-packages/cv2/cv2.cpython-38-darwin.so)

How to link images pr file in Google Colab

I failed to show the image,
below the code,
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import cv2
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.optimizers import RMSprop
img= image.load_img("cnn_happy_notHappy/traing/happy/1.png")
Error:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-13-2c0e7bdac5f8> in <module>()
----> 1 img= image.load_img("cnn_happy_notHappy/traing/happy/1.png")
1 frames
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/utils.py in load_img(path, grayscale, color_mode, target_size, interpolation)
111 raise ImportError('Could not import PIL.Image. '
112 'The use of `load_img` requires PIL.')
--> 113 with open(path, 'rb') as f:
114 img = pil_image.open(io.BytesIO(f.read()))
115 if color_mode == 'grayscale':
FileNotFoundError: [Errno 2] No such file or directory: 'cnn_happy_notHappy/traing/happy/1.png'
The folder screenshots where image folder and file have,
Please help me to solve this issue,
You can import your images first in google colab then assign it to your img variable.
# you can run this in first cell
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import cv2
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.optimizers import RMSprop
# you can run this is the second cell will show a button to upload the file in google collab
from google.colab import files
uploaded = files.upload()
# use this after image in uploaded in the previous cell successfullt
img= image.load_img("1.png")

Displaying/getting Images from an URL in Python

I am new to python. But I got a task and I need to Displaying/getting Images from an URL.
I have been using Jupyter notebook with python to try to do this.
import sys
print(sys.version)
3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]
I was trying to do it as in this post but none of the answers work.
With
import urllib, cStringIO
file = cStringIO.StringIO(urllib.urlopen(URL).read())
img = Image.open(file)
I get:
ImportError Traceback (most recent call last)
<ipython-input-33-da63c9426dad> in <module>()
1 url='http://images.mid-day.com/images/2017/feb/15-Justin-Bieber.jpg'
2 print(url)
----> 3 import urllib, cStringIO
4
5 file = cStringIO.StringIO(urllib.urlopen(URL).read())
ImportError: No module named 'cStringIO'
With:
from PIL import Image
import requests
from io import BytesIO
response = requests.get(url)
img = Image.open(BytesIO(response.content))
I get:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-168cd6221ea3> in <module>()
1 #response = requests.get("https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg")
----> 2 response.read("https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg").decode('utf-8')
3 img = Image.open(StringIO(response.content))
AttributeError: 'Response' object has no attribute 'read'
With:
from PIL import Image
import requests
from StringIO import StringIO
response = requests.get(url)
img = Image.open(StringIO(response.content))
I get:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-37-5716207ad35f> in <module>()
3 from PIL import Image
4 import requests
----> 5 from StringIO import StringIO
6
7 response = requests.get(url)
ImportError: No module named 'StringIO'
Etc....
I thought it was going to be an easy task, but so far I haven't been able to find an answer.
I really hope someone can help me
This worked for me
from PIL import Image
import requests
from io import BytesIO
url = "https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg"
response = requests.get(url)
img = Image.open(BytesIO(response.content))
img.show()
You are getting an error because you used the line response.read("https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg").decode('utf-8') instead. I'd switch back to using response = requests.get(url)
Additionally, for your error: ImportError: No module named 'cStringIO', you are using python3. StringIO and cStringIO from python 2 were removed in python 3. Use from io import StringIO instead. See StringIO in Python3 for more details.
This might be duplicated with https://stackoverflow.com/a/46954931/4010864.
For your third option with PIL you can try this:
from PIL import Image
import requests
import matplotlib.pyplot as plt
response = requests.get(url, stream=True)
img = Image.open(response.raw)
plt.imshow(img)
plt.show()

Alternative way of connecting to PIL library?

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)

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