In pillow library, BICUBIC is not working - python

This is my code.
import sys, os
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from scipy import *
sys.path.insert(0, 'C:/research')
im = Image.open('C:/research/1.jpg')
hei, wei = im.height, im.width
im_bicubic = im.resize((wei,hei), im.BICUBIC)
im.save('C:/research/1ori.jpg') #original image
im_bicubic.save('C:/research/1bic.jpg') #Images with bicubic applied
But I get this error.
AttributeError: 'JpegImageFile' object has no attribute 'BICUBIC'
Why is this message coming up?
.bmp, the same message pops up.
What should I do?

You need to use PIL.Image.BICUBIC instead of im.BICUBIC.
So you need to change:
im_bicubic = im.resize((wei,hei), im.BICUBIC)
to
im.resize((wei,hei),PIL.Image.BICUBIC)
You also need to import pil like so:
import PIL

Related

Python Error ImportError: No module named skimage.io

When I run this program in Python, it show me this error:
ImportError: No module named skimage.io.
I have already run the command pip install scikit-image, but I still get this error. Can you please help me?
This is my code:
import matplotlib.pyplot as plt
import skimage.io as io
import skimage.color as color
parrots = io.imread('C:\Python27\Example\parrots.bmp')
parrots_hsv= skcolor.convert_colorspace(parrots, 'RGB','HSV')
fig, ax= plt.subplots(nclos = 2, figsize=('8,4'))
ax[0].imshow(parrots)
ax[0].set_title('original image')
restored_image =skcolore.convert_colorspace(parrots_hsv, 'HSV','RGB')
ax[1].imshow(restored_image)
ax[1].set_title('restored image')
plt.show()`enter code here`
I reproduced this error like so:
import math.sqrt as sq
print(sq(4))
Error:
File ".\Solution.py", line 1, in <module>
import math.sqrt as sq
ModuleNotFoundError: No module named 'math.sqrt'; 'math' is not a package
repaired by:
from math import sqrt as sq
print(sq(4))
So I presume if you changed your imports to the below code it might fix it:
from matplotlib import pyplot as plt
from skimage import io as io
from skimage import color as color
In addition, you might want to read a bit here for a simple explanation about imports.

Getting the following error while using scikit-image to read images "AttributeError: 'PngImageFile' object has no attribute '_PngImageFile__frame' "

I am using scikit-image to load a random image from a folder. OpenCV is being used for operations later on..
Code is as follows (only relevant parts included)
import imageio
import cv2 as cv
import fileinput
from collections import Counter
from data.apple_dataset import AppleDataset
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from torchvision.models.detection.mask_rcnn import MaskRCNNPredictor
from torchvision.transforms import functional as F
import utility.utils as utils
import utility.transforms as T
from PIL import Image
import skimage.io
from skimage.viewer import ImageViewer
from matplotlib import pyplot as plt
%matplotlib inline
APPLE_IMAGE_PATH = r"__mypath__\samples\apples\images"
# Load a random image from the images folder
FILE_NAMES = next(os.walk(APPLE_IMAGE_PATH))[2]
random_apple_in_folder = os.path.join(APPLE_IMAGE_PATH, random.choice(FILE_NAMES))
apple_image = skimage.io.imread(random_apple_in_folder)
apple_image_cv = cv.imread(random_apple_in_folder)
apple_image_cv = cv.cvtColor(apple_image_cv, cv.COLOR_BGR2RGB)
Error is as follows
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-9575eed18f18> in <module>
11 FILE_NAMES = next(os.walk(APPLE_IMAGE_PATH))[2]
12 random_apple_in_folder = os.path.join(APPLE_IMAGE_PATH, random.choice(FILE_NAMES))
---> 13 apple_image = skimage.io.imread(random_apple_in_folder)
14 apple_image_cv = cv.imread(random_apple_in_folder)
AttributeError: 'PngImageFile' object has no attribute '_PngImageFile__frame'
How do i proceed from here? What should i change???
This is a bug in Pillow 7.1.0. You can upgrade Pillow with pip install -U pillow. See this bug report for more information:
https://github.com/scikit-image/scikit-image/issues/4548

error : 'module' object is not callable when using logmmse

I am trying to reduce noise in my audio_file and want to have an output file which doesn't contain noise, and I use the logmmse library:
I use this code:
import wavio
import numpy as np
from logmmse import logmmse_from_file
import logmmse
r = wavio.read('03-01-02-02-01-01-01(read).wav')
y,sr = librosa.load('03-01-02-02-01-01-01(read).wav')
#print(y)
import numpy as np
A = np.asarray(y)
but I have this error:
TypeError: 'module' object is not callable!
can you help me please?
#print(A)
logmmse(A, r.rate, output_file = 'log.wav')
As the error states, you are trying to call the module itself. I suppose what you're trying to do is use the logmmse function inside the logmmse module, so you should do:
logmmse.logmmse(A, r.rate, output_file = 'log.wav')

Importing a Function in Python: ImportError "moduleX has no attribute Y"

I'm attempting to import a function defined in another file into the one I am working in.
The function I'm trying to import is in a file called ParallelEqns.py and looks like:
import sys
import numpy as np
import scipy as sp
import sympy as sym
import matplotlib.pyplot as plt
import os
def ParDeriv(x,p):
derivative = []
for k in range(nS):
test = x[(k-1)%nS]*(x[(k+1)%nS] - x[(k-2)%nS]) - x[(k)%nS] + p
if k == 0:
derivative = test
else:
derivative = np.vstack([derivative, test])
return derivative
The file I'm working in looks like:
import sys
import numpy as np
import scipy as sp
import sympy as sym
import matplotlib.pyplot as plt
import os
from ParallelEqns import ParDeriv
That gives me an error of "cannot import name 'ParDeriv'"
If I change the file to:
import sys
import numpy as np
import scipy as sp
import sympy as sym
import matplotlib.pyplot as plt
import os
import ParallelEqns
ParDeriv = ParallelEqns.ParDeriv
I get an error that says "module 'ParallelEqns' has no attribute 'ParDeriv'"
I've checked that both files are in the same directory. I'm not sure what I'm doing wrong here
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Edit: I've answered my own question by closing everything down and restarting python. It looks like I needed to restart python after creating the ParallelEqns.py file for it to correctly import
It turns out I just needed to restart python as I had created the file that I was trying to import after starting up python. Once I did that it worked out

Error while converting rgb to lab : python

import skimage
from skimage import io, color
import numpy as np
import scipy.ndimage as ndi
rgb = io.imread('img.jpg')
lab = color.rgb2lab(skimage.img_as_float(rgb))
l_chan1 = lab[:,:,0]
l_chan1 /= np.max(np.abs(l_chan1))
l_chan_med = ndi.median_filter(l_chan1, size=5)
skimage.io.imshow(l_chan_med)
I am trying to do some image processing. While i was changing the color scheme, i am getting an error for rgb2lab function. " color.rgb2lab module object has no attribute 'rgb2lab'. I have imported all the required libraries.Any suggestions will be appreciated
Try this:
import skimage
from skimage import io
from skimage.color import rgb2lab
import numpy as np
import scipy.ndimage as ndi
rgb = io.imread('img.jpg')
lab = rgb2lab(skimage.img_as_float(rgb))
l_chan1 = lab[:,:,0]
l_chan1 /= np.max(np.abs(l_chan1))
l_chan_med = ndi.median_filter(l_chan1, size=5)
skimage.io.imshow(l_chan_med)
I don't know what's wrong, but your code runs fine on my machine. Python 2.7.6, OS X Yosemite. I would try reinstalling scikit-image.

Categories

Resources