Does anybody know how to upload an image (using filedialog.askopenfile) and then storing the uploaded image to an existing folder on my computer?! All the examples available on the internet require image paths, and i get an error whenever i provide the filepath for the uploaded image, am i doing something wrong?
import cv2
import os
from tkinter.filedialog import askopenfile
filename = askopenfile(title ='open', filetypes=(("PNGs", "*.png"),("JPGs", "*.jpg"), ("GIFs", "*.gif")))
img = cv2.imread(filename)
path = "/Users/mac/desktop/test" # => Folder path
cv2.imwrite(os.path.join(path, img)
Related
This is my code. It works well and able to write the image to the specified folder in IDE. But, after converting the .py file to .exe file by PyInstaller, cv2.imwrite does not work anymore. Is there anyone can help me to solve this issue?
# enter code here
import cv2
import os
# Image path
image_path = r'C:\Users\Meadow\Desktop\Saving Images\jeremy.jpg'
# Image directory
directory = r'C:\Users\Meadow\Desktop\Saving Images'
# Using cv2.imread() method to read the image
img = cv2.imread(image_path)
# Change the current directory to specified directory
os.chdir(directory)
# List files and directories in 'C:/Users/Rajnish/Desktop/GeeksforGeeks'
print("Before saving image:")
print(os.listdir(directory))
# Filename
filename = 'savedImage.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, img)
# List files and directories
# in 'C:/Users / Rajnish / Desktop / GeeksforGeeks'
print("After saving image:")
print(os.listdir(directory))
print('Successfully saved')
I am trying to run a simple example code to write an image using opencv on python3. Code reference:1
import cv2
import os
image_path = r'C:\Users\840g1 touch\Desktop\B2.jpg'
directory = r'C:\Users\840g1 touch\Desktop'
img = cv2.imread(image_path)
os.chdir(directory)
print("Before saving image:")
print(os.listdir(directory))
# Filename
filename = 'savedImage.jpg'
cv2.imwrite(filename, img)
print("After saving image:")
print(os.listdir(directory))
print('Successfully saved')
Image is displaying and everything but the image is not getting saved anywhere. I am using Anaconda on windows. Not sure if the problem is related to the code or my PC.
Any help is much appreciated!
You did not provide a path for imwrite so it writes in your pythons current working directory.
change the line:
cv2.imwrite(filename, img)
to something like:
cv2.imwrite(os.path.join(directory,filename), img)
note:
you can get your current working dir with
os.getcwd()
I have a folder contains files of type PDF, PNG, and JPEG. I'm trying to convert PDF files to images and this is the code I've tried:
from pdf2image import convert_from_path, convert_from_bytes
from pdf2image.exceptions import (
PDFInfoNotInstalledError,
PDFPageCountError,
PDFSyntaxError
)
images = convert_from_path('41117 UIs in eng.pdf')
for i, image in enumerate(images):
fname = 'image'+str(i)+'.jpg'
image.save(fname, "JPEG")
Clearly this code is only for a single pdf file and I want to transfer the code to serve several pdf files that are mixed with other file types in the same folder.
please help.
You could try something like this (this script finds pdf files in the same directory with your python program):
import os
from pdf2image import convert_from_path, convert_from_bytes
from pdf2image.exceptions import (
PDFInfoNotInstalledError,
PDFPageCountError,
PDFSyntaxError
)
# get all pdf files from directory
pdf_files = [filename for filename in os.listdir(
'.') if filename.endswith('.pdf')]
for pdf_file in pdf_files:
images = convert_from_path(pdf_file)
print(pdf_file)
for i, image in enumerate(images):
fname = pdf_file+'_image'+str(i)+'.jpg'
image.save(fname, "JPEG")
I am trying to execute this script
from PIL import Image
im = Image.open("image.jpg")
nx, ny = im.size
It is working fine when I run it in python shell
pytesser_v0.0.1]#env python
>>> from PIL import Image
>>> im = Image.open("image.jpg")
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=46x24 at 0x7FA4688F16D0>
but when I put it in a some test.py file and run it like python test.py
I am getting this error
File "test1.py", line 17, in <module>
im = Image.open("image.jpg")
File "/usr/local/python.2.7.11/lib/python2.7/site-packages/PIL/Image.py", line 2309, in open
% (filename if filename else fp))
IOError: cannot identify image file 'image.jpg'
please help me with this issue, Thanks
PS: Earlier I installed PIL from Imaging-1.1.7 setup.py, later I installed Pillow, I think the problem was in the mutual presence of the PIL and Pillow library on the machine.
Seems like PIL library haven't fixed this bug yet.
Here is my solution:
Open image using OpenCV library, then convert it to PIL image
from PIL import Image
import cv2
image_path = 'Folder/My_picture.jpg'
# read image using cv2 as numpy array
cv_img = cv2.imread(image_path)
# convert the color (necessary)
cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
# read as PIL image in RGB
pil_img = Image.fromarray(cv_img).convert('RGBA')
Then you can operate with it as with a regular PIL image object.
Make sure that "image.jpg" is in the same directory as "test1.py".
If it isn't then you could either move it, or put the correct directory inside of Image.open().
I have the same issue.
This is because the test.py does not have the same pathname. If you are working in the same folder it will work.
However, the solution i found was to put in the full path + file name so that it is unambiguous.
"c:\...fullpath...\image.jpg"
You can do it like this:
from PIL import Image
import os
curDir = os.getcwd()
fileName = "image.jpg"
fn = curDir + "\\" + fileName
print(fn)
image = Image.open(fn)
image.show()
This works. Please let me know if you find better.
I am generating a gif with images2gif from some .png pictures. I am using the following script:
__author__ = 'Robert'
from images2gif import writeGif
from PIL import Image
import os
file_names = sorted((fn for fn in os.listdir('/home/manager/Desktop/sf_linux_shared/project/prueba') if fn.endswith('.png')))
#['animationframa.png', 'animationframb.png', ...] "
images = [Image.open(fn) for fn in file_names]
size = (150,150)
for im in images:
im.thumbnail(size, Image.ANTIALIAS)
print writeGif.__doc__
filename = "my_gif.GIF"
writeGif(filename, images, duration=0.2)
however I have the following error:
IOError: [Errno 2] No such file or directory: 'cosa.png'
cosa.png is one of the pictures I want to create the gif with. The problem seems to be in:
images = [Image.open(fn) for fn in file_names]
but I cannot detect it
open(filename) looks in the current working directory if filename is not an
absolute path. The current working directory is the directory from which the script is run.
Either use os.chdir to change the working directory, or make all your filenames absolute:
WORKDIR = '/home/manager/Desktop/sf_linux_shared/project/prueba'
file_names = sorted((os.path.join(WORDIR, fn)
for fn in os.listdir(WORKDIR) if fn.endswith('.png')))