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)
Related
I am trying to access to the temperature data stored in a tiff-file. I was provided with a python script that was supposed to be able to do this, but I keep getting the following error:
Traceback (most recent call last):
File "read_tiff.py", line 57, in <module>
im = Image.open(sourcePath)
File "/Users/myname/opt/anaconda3/lib/python3.8/site-packages/PIL/Image.py", line 2943, in open
raise UnidentifiedImageError(
PIL.UnidentifiedImageError: cannot identify image file 'Testbild.tiff'
This is the relevant section of the code:
sourcePath = "Testbild.tiff"
im = []
try:
sourcePath = sys.argv[1]
except IndexError:
print('usage: python read_tiff.py filename')
sys.exit()
try:
im = Image.open(sourcePath)
except FileNotFoundError:
print('File not found: ' + sourcePath)
sys.exit()
imarray = np.array(im)
This is what I checked:
with another random tiff file it worked, so it is probably not the script but the file itself (/maybe I need to install some additional package??)
the tiff file can be opened without a problem in IrfanView and Photoscan
when prompting "file Testbild.tiff" I get "Testbild.tiff: TIFF image data, little-endian" so it definitely IS a tiff file
Is anyone able to help me out?
Cheers!
EDIT:
The import statement for Image is from PIL import Image
If necessary, these are all the import statements of the script:
import matplotlib.pyplot as plt
from PIL import Image
import sys
import numpy as np
import math
import cv2
Try using:
from skimage import io
im = io.imread(sourcePath)
This will open it directly as an array as well.
In my case, it worked to read .tif file to ndarray:
path2dir = r'C:\data\WORK\image4example'
name_img = 'Slice_25.tif'
path2img = os.path.join(path2dir, name_img)
im = cv2.imread(path2img , cv2.IMREAD_ANYDEPTH)
plt.imshow(im)
i want to encoding some of folders that contain face images.
this is the code that i've been tried.
import face_recognition
import cv2
import numpy as np
import os
import glob
video_capture = cv2.VideoCapture(0)
known_face_encodings = []
known_face_names = []
# Load image folder and learn how to recognize it.
os.chdir("./coba1")
for file in glob.glob("*.jpg"):
images = face_recognition.load_image_file(file)
images_encoding = face_recognition.face_encodings(images)[0]
known_face_encodings.append(images_encoding)
known_face_names.append("jokowi")
print(images_encoding)
#Load image folder and learn how to recognize it.
os.chdir("./coba")
for file in glob.glob("*.jpg"):
images = face_recognition.load_image_file(file)
images_encoding = face_recognition.face_encodings(images)[0]
known_face_encodings.append(images_encoding)
known_face_names.append("amber heard")
print(images_encoding)
when i run the code, the terminal shows this warning
Traceback (most recent call last):
File "face_rec.py", line 32, in <module>
os.chdir("./coba")
FileNotFoundError: [Errno 2] No such file or directory: './coba'
is there anything wrong with my code? i really need the solution, thanks in advance !
I'm programming a solution to a problem, and I've run into an issue with the PIL image method.
choice = input("Would you like to save the maze as a file, Y/N?").upper()
if choice == "Y":
canvas.update()
canvas.postscript(file="maze.eps", colormode='color')
img = Image.open("maze.eps")
However I get the following error message:
Traceback (most recent call last):
File "C:\Users\Matthew\Desktop\NEA\Technical Solution\mazeVisualiser.py", line 66, in <module>
img = Image.open("maze.eps")
AttributeError: type object 'Image' has no attribute 'open'
But while learning the PIL module I know this is valid like so:
from PIL import Image
img = Image.open('brick-house.png')
Any help would be greatly appreciated as this has got me completely stuck.
Try This:
import PIL.Image
fp = open("brick-house.png", "rb")
img = PIL.Image.open(fp)
img.show()
I am trying to read scanned images from a pdf using wand and display it using PIL. But I get some error. First page of the pdf file works perfectly but the second page shows this error.
Code
from wand.image import Image
from wand.display import display
from PIL import Image as PI
import pyocr
import pyocr.builders
import io
import numpy as np
import cStringIO
tool = pyocr.get_available_tools()[0]
lang = tool.get_available_languages()[1]
req_image = []
final_text = []
image_pdf = Image(filename="DEEP_PLAST_20.700.pdf", resolution=200)
image_jpeg = image_pdf.convert('jpeg')
img_page = Image(image=image_jpeg.sequence[1])
img_buffer = np.asarray(bytearray(img_page.make_blob()), dtype=np.uint8)
print(img_buffer)
# im = PI.fromarray(img_buffer)
im = PI.open(cStringIO.StringIO(img_buffer))
I get this error.
Traceback (most recent call last):
File "ocr.py", line 43, in <module>
im = PI.open(cStringIO.StringIO(img_buffer))
File "/home/sahil/anaconda2/lib/python2.7/site-packages/PIL/Image.py", line 2452, in open
% (filename if filename else fp))
IOError: cannot identify image file <cStringIO.StringI object at 0x7fc4a8f168b0>
I don't why the code fails on the second page of the pdf whereas it works for the first one.
Any help would be appreciated!
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.