I want make a converter based on python 3.8
I'm using imageoi API 2.6.1
Here's some of my codes what i think i did it wrong
from tkinter import *
from tkinter import filedialog
import imageio
import os
root = Tk()
ftypes = [('All Files', "*.*"), ('Webm', "*.webm")]
ttl = "Select Files(s)"
dir1 = 'D:/My Pictures/9gag'
root.fileName = filedialog.askopenfilenames(filetypes=ftypes, initialdir=dir1, title=ttl)
lst = list(root.fileName)
def path_leaf(path):
return path.strip('/').strip('\\').split('/')[-1].split('\\')[-1]
print([path_leaf(path) for path in lst])
lst2 = [path_leaf(path) for path in lst]
print(lst)
def gifMaker(inputPath, targetFormat):
outputPath = os.path.splitext(inputPath)[0] + targetFormat
print(f'converting {inputPath} \n to {outputPath}')
reader = imageio.get_reader(inputPath)
fps = reader.get_meta_data()['fps']
writer = imageio.get_writer(outputPath, fps=fps)
for frames in reader:
writer.append_data(frames)
print(f'Frame {frames}')
print('Done!')
writer.close()
for ad in lst2:
gifMaker(ad, '.gif')
And the error are shown like this
Traceback (most recent call last):
File "D:/My Pictures/GIF/GIF.py", line 41, in <module>
gifMaker(ad, '.gif')
File "D:/My Pictures/GIF/GIF.py", line 28, in gifMaker
reader = imageio.get_reader(inputPath)
File "C:\Python\Anaconda3\lib\site-packages\imageio\core\functions.py", line 173, in get_reader
request = Request(uri, "r" + mode, **kwargs)
File "C:\Python\Anaconda3\lib\site-packages\imageio\core\request.py", line 126, in __init__
self._parse_uri(uri)
File "C:\Python\Anaconda3\lib\site-packages\imageio\core\request.py", line 278, in _parse_uri
raise FileNotFoundError("No such file: '%s'" % fn)
FileNotFoundError: No such file: 'D:\My Pictures\GIF\a6VOVL2_460sv.mp4'
So, what am i missing or fault? I don't understand why the error is showing "file is not found". Can someone explain to me in detail, how these lines of error occurred?
There are several possibilities
Maybe you misstyped the path/filename.
Maybe the space in the path is causing trouble.
Related
I use the Pillow lib to create thumbnails. I have to create a lot of them, actually more than 10.000
The program works fine, but after processing round about 1.500, I get the following error:
Traceback (most recent call last):
File "thumb.py", line 15, in <module>
im = Image.open('/Users/Marcel/images/07032017/' + infile)
File "/Users/Marcel/product-/PIL/Image.py", line 2339, in open
im = _open_core(fp, filename, prefix)
File "/Users/Marcel/product-/PIL/Image.py", line 2329, in _open_core
im = factory(fp, filename)
File "/Users/Marcel/product-/PIL/ImageFile.py", line 97, in __init__
self._open()
File "/Users/Marcel/product-/PIL/PngImagePlugin.py", line 538, in _open
s = self.png.call(cid, pos, length)
File "/Users/Marcel/product-/PIL/PngImagePlugin.py", line 136, in call
return getattr(self, "chunk_" + cid.decode('ascii'))(pos, length)
File "/Users/Marcel/product-/PIL/PngImagePlugin.py", line 319, in chunk_iCCP
icc_profile = _safe_zlib_decompress(s[i+2:])
File "/Users/Marcel/product-/PIL/PngImagePlugin.py", line 90, in _safe_zlib_decompress
raise ValueError("Decompressed Data Too Large")
ValueError: Decompressed Data Too Large
My program is very straight forward:
import os, sys
import PIL
from PIL import Image
size = 235, 210
reviewedProductsList = open('products.txt', 'r')
reviewedProducts = reviewedProductsList.readlines()
t = map(lambda s: s.strip(), reviewedProducts)
print "Thumbs to create: '%s'" % len(reviewedProducts)
for infile in t:
outfile = infile
try:
im = Image.open('/Users/Marcel/images/07032017/' + infile)
im.thumbnail(size, Image.ANTIALIAS)
print "thumb created"
im.save('/Users/Marcel/product-/thumbs/' + outfile, "JPEG")
except IOError, e:
print "cannot create thumbnail for '%s'" % infile
print "error: '%s'" % e
I am performing this operation locally on my MacBook Pro.
This is to protect against a potential DoS attack on servers running Pillow caused by decompression bombs. It occurs when a decompressed image is found to have too large metadata. See http://pillow.readthedocs.io/en/4.0.x/handbook/image-file-formats.html?highlight=decompression#png
Here's the CVE report: https:// www.cvedetails.com/cve/CVE-2014-9601/
From a recent issue:
If you set ImageFile.LOAD_TRUNCATED_IMAGES to true, it will suppress
the error (but still not read the large metadata). Alternately, you can
change set the values here: https://github.com/python-pillow/Pillow/ blob/master/PIL/PngImagePlugin.py#L74
https://github.com/python-pillow/Pillow/issues/2445
following code should help you in setting what accepted answer says.
from PIL import PngImagePlugin
LARGE_ENOUGH_NUMBER = 100
PngImagePlugin.MAX_TEXT_CHUNK = LARGE_ENOUGH_NUMBER * (1024**2)
It's not documented how to set this value. I hope people find this useful.
def morse_audio( item ):
from pyglet import media
import pyglet
import time
import glob
import os
import wave
from contextlib import closing
files = []
audios = []
for file in glob.glob('C:\\Users\\MQ\'s Virual World\\Downloads\\Morse\\*.wav'):
ass = str(os.path.join('C:\\Users\MQ\'s Virual World\\Downloads\\Morse', file))
print (ass)
files.append(ass)
#audio = media.load(files[1])
#audio.play()
#print (len(files))
one = list(item)
str_list = [x.strip(' ') for x in one]
str_list = [x.strip('/') for x in str_list]
for s in str_list[0]:
if s != "-" and s != ".":
list(item)
for letter in item:
for i in range(0, 51):
if letter == " ":
time.sleep(1.5)
audios.append("noise3.wav")
break
if letter != letterlst[i] and letter != letterlst[i].lower():
continue
else:
print (files[i])
audio = media.load(files[i])
audio.play()
audios.append(files[i])
audios.append("noise2.wav")
time.sleep(1)
else:
lst = item.split()
print (' '.join(lst))
for code in lst:
for i in range(0, 51):
if code == "/":
time.sleep(1.5)
audios.append("noise3.wav")
break
if code != morse[i]:
continue
else:
print (files[i])
audio = media.load(files[i])
audio.play()
audios.append(files[i])
audios.append("noise2.wav")
time.sleep(1)
break
outfile = "sounds.wav"
data= []
for file in audios:
w = wave.open(file, 'rb')
lol = w.getparams()
print (lol)
data.append( [w.getparams(), w.readframes(w.getnframes())] )
w.close()
with closing(wave.open(outfile, 'wb')) as output:
# find sample rate from first file
with closing(wave.open(audios[0])) as w:
output.setparams(w.getparams())
# write each file to output
for audioo in audios:
with closing(wave.open(audioo)) as w:
output.writeframes(w.readframes(w.getnframes()))()))
So this code previously worked but I wanted to use different file types other then .wav files but because that worked so poorly I went back to .wav. These are different .wav files but the ones that worked before get the same error message. Which is:
Traceback (most recent call last):
File "C:\Users\MQ's Virual World\AppData\Local\Programs\Python\Python35-32\morsecode.py", line 187, in <module>
morse_audio("0123456789ÁÄ#&':,$=!-().+?;/_")
File "C:\Users\MQ's Virual World\AppData\Local\Programs\Python\Python35-32\morsecode.py", line 96, in morse_audio
audio.play()
File "C:\Users\MQ's Virual World\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyglet\media\__init__.py", line 473, in play
player.play()
File "C:\Users\MQ's Virual World\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyglet\media\__init__.py", line 1012, in play
self._set_playing(True)
File "C:\Users\MQ's Virual World\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyglet\media\__init__.py", line 993, in _set_playing
self._create_audio_player()
File "C:\Users\MQ's Virual World\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyglet\media\__init__.py", line 1083, in _create_audio_player
self._audio_player = audio_driver.create_audio_player(group, self)
File "C:\Users\MQ's Virual World\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyglet\media\drivers\directsound\__init__.py", line 502, in create_audio_player
return DirectSoundAudioPlayer(source_group, player)
File "C:\Users\MQ's Virual World\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyglet\media\drivers\directsound\__init__.py", line 184, in __init__
None)
File "C:\Users\MQ's Virual World\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyglet\com.py", line 125, in <lambda>
self.method.get_field()(self.i, self.name)(obj, *args)
File "_ctypes/callproc.c", line 920, in GetResult
OSError: [WinError -2147024809] The parameter is incorrect
I've tried .wav files that used to work. It works when I use a .ogg file. Also works with mp3s. Seems only .wav files are giving it issues. Very suddenly and randomly.
I am working on a python program that displays a series of images using Tkinter and ImageTk. I have not been able to display more than a single image. Below is a small complete program that reproduces the error. The program searches the current directly recursively for jpg files, and displays them as the uses presses Enter.
import Tkinter, ImageTk,os, re
def ls_rec(direc):
try:
ls = os.listdir(direc)
except Exception as e:
return
for f in os.listdir(direc):
fpath = os.path.join(direc, f)
if os.path.isfile(fpath):
yield fpath
elif os.path.isdir(fpath):
for f2 in iterate_dir(os.path.join(direc,f)):
yield f2
images = filter(lambda a:re.match('.*\\.jpg$',a),ls_rec(os.getcwd()))
assert(len(images)>10)
top = Tkinter.Tk()
image_label = Tkinter.Label(top)
Label_text = Tkinter.Label(top,text="Below is an image")
img = None
i = 0
def get_next_image(event = None):
global i, img
i+=1
img = ImageTk.PhotoImage(images[i])
label.config(image=img)
label.image = img
top.bind('<Enter>',get_next_image)
label.pack(side='bottom')
Label_text.pack(side='top')
get_next_image()
top.mainloop()
The program fails with the following traceback:
Traceback (most recent call last):
File "/usr/lib/python2.7/pdb.py", line 1314, in main
pdb._runscript(mainpyfile)
File "/usr/lib/python2.7/pdb.py", line 1233, in _runscript
self.run(statement)
File "/usr/lib/python2.7/bdb.py", line 387, in run
exec cmd in globals, locals
File "<string>", line 1, in <module>
File "/home/myuser/Projects/sample_images.py", line 1, in <module>
import Tkinter, ImageTk,os, re
File "/home/myuser/Projects/sample_images.py", line 32, in get_next_image
img = ImageTk.PhotoImage(some_image[1])
File "/usr/lib/python2.7/dist-packages/PIL/ImageTk.py", line 109, in __init__
mode = Image.getmodebase(mode)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 245, in getmodebase
return ImageMode.getmode(mode).basemode
File "/usr/lib/python2.7/dist-packages/PIL/ImageMode.py", line 50, in getmode
return _modes[mode]
KeyError: '/home/myuser/sampleimage.jpg'
Does anyone get the same behavior when running this code? What am I doing wrong?
EDIT: Using korylprince's solution, and a bit of cleaning, the following is a working version of the original code:
import os, re, Tkinter, ImageTk
def ls_rec(direc, filter_fun=lambda a:True):
for (dirname, dirnames, fnames) in os.walk(direc):
for fname in fnames:
if filter_fun(fname):
yield os.path.join(dirname,fname)
top = Tkinter.Tk()
image_label = Tkinter.Label(top)
text_label = Tkinter.Label(top,text="Below is an image")
images = ls_rec(os.getcwd(), lambda a:re.match('.*\\.jpg$',a))
imgL = []
def get_next_image(event = None):
fname = images.next()
print fname
fhandle = open(fname)
img = ImageTk.PhotoImage(file=fhandle)
fhandle.close()
imgL.append(img)
image_label.config(image=img)
top.bind('<Return>',get_next_image)
image_label.pack(side='bottom')
text_label.pack(side='top')
get_next_image()
top.mainloop()
Edit: top.bind('<Enter>'...) actually bound the event of the mouse entering the frame, rather than user pressing Enter key. The correct line is top.bind('<Return>',...).
ImageTk.PhotoImage is not really documented properly.
You should try something like this:
#outside of functions
images = list()
#inside function
global images
with open(images[i]) as f:
img = ImageTk.PhotoImage(file=f)
images.append(img)
The reason for putting the image in the list is so that python will have a reference to it. Otherwise the garbage collector will delete the image object eventually.
I am having some trouble with an small slide show script that I am trying to make. The script runs just fine when the folder of files is not altered, but if i add a picture to the folder I get an tcl error stating that the file cannot be opened or is missing. I am really struggeling to see the issue here and I am hoping that some of you can?
the function where adding a picture to the folder without having to restart it is the main function, so that the slides just change automatically. It will be a slide show of graphs running in our office as an information screen.
See the code and error underneath. Any help is much appritiated!
Code
from itertools import cycle
import os
try:
import Tkinter as tk
except ImportError:
class App(tk.Tk):
def __init__(self,x, y, delay):
print "init running"
tk.Tk.__init__(self)
self.geometry('+{}+{}'.format(x, y))
self.delay = delay
self.image_files = []
self.new_image = []
for path, subdirs, files in os.walk('C:\kitfinder2\pictures'):
for filename in files:
f = filename
self.image_files.append(f)
self.new_image.append(f)
self.pictures = cycle((tk.PhotoImage(file=image), image)
for image in self.image_files)
self.picture_display = tk.Label(self)
self.picture_display.pack()
def UpdateImages(self):
self.new_image = []
for path, subdirs, files in os.walk('C:\kitfinder2\pictures'):
for filename in files:
f = filename
self.new_image.append(f)
if len(self.image_files) != len(self.new_image):
print "Difference!"
self.image_files = []
for path, subdirs, files in os.walk('C:\kitfinder2\pictures'):
for filename in files:
f = filename
self.image_files.append(f)
self.pictures = cycle((tk.PhotoImage(file=image), image)
for image in self.image_files)
print "image updated!:" + str(self.image_files)
self.show_slides()
def show_slides(self):
print self.image_files
'''cycle through the images and show them'''
img_object, img_name = next(self.pictures)
self.picture_display.config(image=img_object)
self.title(img_name)
self.after(self.delay, self.UpdateImages)
def run(self):
self.mainloop()
delay = 3500
x = 100
y = 50
app = App( x, y, delay)
app.UpdateImages()
app.run()
Error message
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 533, in callit
func(*args)
File "start_stopp.py", line 56, in UpdateImages
self.show_slides()
File "start_stopp.py", line 60, in show_slides
img_object, img_name = next(self.pictures)
File "start_stopp.py", line 53, in <genexpr>
for image in self.image_files)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3326, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3282, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't open "2.gif": no such file or directory
You're only appending the basename and then trying to open it. e.g. 2.gif. Unfortunately, 2.gif isn't in the current directory -- it's (somewhere) in 'C:\kitfinder2\pictures'
You probably want something like:
self.image_files.append(os.path.join(path, filename))
rather than simply:
self.image_files.append(f)
Brother you can use:
photo = PhotoImage(file = "path_of_file")
you can put litter r before "path_of_file" to override putting douple \
photo = PhotoImage(file=r'F:\Python Programs\Tkinter\2.png')
I am working on a python program that displays a series of images using Tkinter and ImageTk. I have not been able to display more than a single image. Below is a small complete program that reproduces the error. The program searches the current directly recursively for jpg files, and displays them as the uses presses Enter.
import Tkinter, ImageTk,os, re
def ls_rec(direc):
try:
ls = os.listdir(direc)
except Exception as e:
return
for f in os.listdir(direc):
fpath = os.path.join(direc, f)
if os.path.isfile(fpath):
yield fpath
elif os.path.isdir(fpath):
for f2 in iterate_dir(os.path.join(direc,f)):
yield f2
images = filter(lambda a:re.match('.*\\.jpg$',a),ls_rec(os.getcwd()))
assert(len(images)>10)
top = Tkinter.Tk()
image_label = Tkinter.Label(top)
Label_text = Tkinter.Label(top,text="Below is an image")
img = None
i = 0
def get_next_image(event = None):
global i, img
i+=1
img = ImageTk.PhotoImage(images[i])
label.config(image=img)
label.image = img
top.bind('<Enter>',get_next_image)
label.pack(side='bottom')
Label_text.pack(side='top')
get_next_image()
top.mainloop()
The program fails with the following traceback:
Traceback (most recent call last):
File "/usr/lib/python2.7/pdb.py", line 1314, in main
pdb._runscript(mainpyfile)
File "/usr/lib/python2.7/pdb.py", line 1233, in _runscript
self.run(statement)
File "/usr/lib/python2.7/bdb.py", line 387, in run
exec cmd in globals, locals
File "<string>", line 1, in <module>
File "/home/myuser/Projects/sample_images.py", line 1, in <module>
import Tkinter, ImageTk,os, re
File "/home/myuser/Projects/sample_images.py", line 32, in get_next_image
img = ImageTk.PhotoImage(some_image[1])
File "/usr/lib/python2.7/dist-packages/PIL/ImageTk.py", line 109, in __init__
mode = Image.getmodebase(mode)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 245, in getmodebase
return ImageMode.getmode(mode).basemode
File "/usr/lib/python2.7/dist-packages/PIL/ImageMode.py", line 50, in getmode
return _modes[mode]
KeyError: '/home/myuser/sampleimage.jpg'
Does anyone get the same behavior when running this code? What am I doing wrong?
EDIT: Using korylprince's solution, and a bit of cleaning, the following is a working version of the original code:
import os, re, Tkinter, ImageTk
def ls_rec(direc, filter_fun=lambda a:True):
for (dirname, dirnames, fnames) in os.walk(direc):
for fname in fnames:
if filter_fun(fname):
yield os.path.join(dirname,fname)
top = Tkinter.Tk()
image_label = Tkinter.Label(top)
text_label = Tkinter.Label(top,text="Below is an image")
images = ls_rec(os.getcwd(), lambda a:re.match('.*\\.jpg$',a))
imgL = []
def get_next_image(event = None):
fname = images.next()
print fname
fhandle = open(fname)
img = ImageTk.PhotoImage(file=fhandle)
fhandle.close()
imgL.append(img)
image_label.config(image=img)
top.bind('<Return>',get_next_image)
image_label.pack(side='bottom')
text_label.pack(side='top')
get_next_image()
top.mainloop()
Edit: top.bind('<Enter>'...) actually bound the event of the mouse entering the frame, rather than user pressing Enter key. The correct line is top.bind('<Return>',...).
ImageTk.PhotoImage is not really documented properly.
You should try something like this:
#outside of functions
images = list()
#inside function
global images
with open(images[i]) as f:
img = ImageTk.PhotoImage(file=f)
images.append(img)
The reason for putting the image in the list is so that python will have a reference to it. Otherwise the garbage collector will delete the image object eventually.