I'm getting a TypeError when I try and run the following code:
import re
from unicodedata import normalize
from docx import Document
from wand.image import Image
from wand.drawing import Drawing
from wand.font import Font
doc = Document("P.docx")
docText = []
for para in doc.paragraphs:
docText.append(para.text)
fullText = "\n".join(docText)
ct = 242
def get(source, begin, end):
try:
start = source.index(len(begin)) + len(begin)
finish = source.index(len(end), len(start))
return source[start:finish]
except ValueError:
return ""
def capitalize(string):
cap = ("".join(j[0].upper() + j[1:]) for j in string)
return cap
def find_matches(text):
return capitalize(
[
m
for m in re.findall(
r"^[^0-9]\s+([^.;]+\s*)+[.;]+", normalize("NFKD", text), re.MULTILINE
)
]
)
with Image(width=300, height=300, psuedo='xc:black') as canvas:
left, top, width, height = 10, 10, 10, 10
for match in find_matches(text=fullText):
ct += 1
match_words = match.split(" ")
match = " ".join(match_words[:-1]) + 'ct'
with Drawing() as context:
context.fill_color = 'white'
context.rectangle(left=left, top=top, width=width, height=height)
font = Font('/System/Library/Fonts/arial.ttf')
context(canvas)
canvas.caption(match)
canvas.save(filename='patdrawTest.png')
The exact error goes as follows:
Traceback (most recent call last):
File "C:\Users\crazy\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\wand\image.py", line 3221, in caption
raise TypeError()
TypeError
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:/Users/crazy/PycharmProjects/Patents/PatDraw.py", line 54, in
<module>
canvas.caption(match) File "C:\Users\crazy\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\wand\image.py",
line 1061, in wrapped
result = function(self, *args, **kwargs) File "C:\Users\crazy\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\wand\image.py",
line 3223, in caption
raise TypeError('font must be specified or existing in image') TypeError: font must be specified or existing in image
I'm fully at a loss and can't figure it out. Any help would be great.
Related
When I tried to read the luna dataset,there's the error:
Traceback (most recent call last):
File "run_preprocess.py", line 57, in <module>
save_preprocessed_data()
File "run_preprocess.py", line 48, in save_preprocessed_data
ct = CTScan(seriesuid=series_id, centers=tp_co, radii=radii, clazz=0)
File "C:\Luna16-master\prepare\_classes.py", line 14, in __init__
path = glob(f'''{RESOURCES_PATH} /*/{seriesuid}.mhd''')[0]
IndexError: list index out of range
How to solve it?
Thanks in advance.
The code is:
class CTScan(object):
def __init__(self, seriesuid, centers, radii, clazz):
self._seriesuid = seriesuid
self._centers = centers
path = glob(f'''{RESOURCES_PATH} /*/{seriesuid}.mhd''')[0]
# path = paths[0]
self._ds = sitk.ReadImage(path)
self._spacing = np.array(list(reversed(self._ds.GetSpacing())))
self._origin = np.array(list(reversed(self._ds.GetOrigin())))
self._image = sitk.GetArrayFromImage(self._ds)
self._radii = radii
self._clazz = clazz
self._mask = None
from tkinter import *
from glob import glob
import demoCheck
import random
import quitter
from PIL import ImageTk
gifdir = 'd:/Python/Jupyter/Programming Python/Chap 8 - GUI/pic/'
class buttonpics(Frame):
def __init__(self, parent=None, dir='./gifs/', **kwargs):
Frame.__init__(self, parent, **kwargs)
self.pack()
self.lbl = Label(self, text='None', bg='blue', fg='red')
self.lbl.pack(fill=BOTH)
self.btn = Button(self, text='Press me', bg='white', command=self.draw)
self.btn.pack()
self.quitter = quitter.Quitter(self)
self.quitter.pack(anchor=S)
self.files = glob(dir+'*.jpg')
self.images = [(file, ImageTk.PhotoImage(file=file))
for file in self.files]
def draw(self):
name, photo = random.choice(self.images)
self.btn.config(image=photo)
self.lbl.config(text=name)
buttonpics(dir=gifdir).mainloop()
The codes are trying to load jpg formatted pictures to Tkinter through Pillow's ImageTk module. However, the system generated the following error:
> Traceback (most recent call last): File
> "d:/Python/Jupyter/Programming Python/Chap 8 -
> GUI/buttonpics-func.py", line 31, in <module>
> buttonpics(dir=gifdir).mainloop() File "d:/Python/Jupyter/Programming Python/Chap 8 -
> GUI/buttonpics-func.py", line 22, in __init__
> self.images = [(file, ImageTk.PhotoImage(file=file)) File "d:/Python/Jupyter/Programming Python/Chap 8 -
> GUI/buttonpics-func.py", line 22, in <listcomp>
> self.images = [(file, ImageTk.PhotoImage(file=file)) File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageTk.py", line 89,
> in __init__
> image = _get_image_from_kw(kw) File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageTk.py", line 58,
> in _get_image_from_kw
> return Image.open(source) File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 2930,
> in open
> raise UnidentifiedImageError( PIL.UnidentifiedImageError: cannot identify image file 'd:/Python/Jupyter/Programming Python/Chap 8 -
> GUI/pic\\IMG_4147.jpg' Exception ignored in: <function
> PhotoImage.__del__ at 0x00000226CF6CFD30> Traceback (most recent call
> last): File
> "C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageTk.py", line 118,
> in __del__
> name = self.__photo.name AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
If the extension is changed to png, the program can be run smoothly. Does this result from a compatibility issue with jpg format or there are some other reasons? Thank you for the help!
I'm creating a bot to automate the label making process. i encountered this problem while trying to save (append) multiple images to a pdf. This issue only happens when I'm using a large no. of images and the program works fine if I'm using a few image.
I want to fix this program so that it can process unlimited no. of images and append it to a single pdf file.
from PIL import Image, ImageDraw, ImageFont
import textwrap
import pandas
import datetime
import numpy
import os
from tkinter import filedialog
from tqdm import tqdm_gui
datenow = datetime.datetime.now()
pdate = datenow.strftime("%d-%m-%y")
y = 0
excel = filedialog.askopenfilename(initialdir="/", title="Select A File", filetypes=(("Excel File", "*.xlsx"),("all files", "*.*")))
save_dir = filedialog.askdirectory()
imlist = []
df = pandas.read_excel(excel)
for data in (df.values):
name, no = data
x = 0
while x<no:
x = x+1
y = y+1
LH = len(name)
if LH < 32:
W = 70
Lng = 16
if LH > 32:
W = 53
Lng = 22
name = name.upper()
para = textwrap.wrap(name, width=Lng)
MAX_W, MAX_H = 800, 592
im = Image.open('TFC.jpg')
draw = ImageDraw.Draw(im)
font = ImageFont.truetype('OpenSans-ExtraBold.ttf', W)
current_h, pad = 155, -5
for line in para:
w, h = draw.textsize(line, font=font)
draw.text(((MAX_W - w) / 2, current_h), line, font=font)
current_h += h + pad
imlist.append(im)
im.save(f'{save_dir}/{pdate}{" TF "}{y}.pdf' , save_all=True, append_images=imlist)
os.startfile(save_dir)
The error which I'm getting:
"C:\Users\RAJA MONSINGH\PycharmProjects\Fuzzy\venv\Scripts\python.exe" "C:/Users/RAJA MONSINGH/PycharmProjects/Lable/lable3.py"
Traceback (most recent call last):
File "C:\Users\RAJA MONSINGH\AppData\Roaming\Python\Python38\site-packages\PIL\ImageDraw.py", line 465, in Draw
return im.getdraw(mode)
AttributeError: 'JpegImageFile' object has no attribute 'getdraw'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/RAJA MONSINGH/PycharmProjects/Lable/lable3.py", line 34, in <module>
draw = ImageDraw.Draw(im)
File "C:\Users\RAJA MONSINGH\AppData\Roaming\Python\Python38\site-packages\PIL\ImageDraw.py", line 467, in Draw
return ImageDraw(im, mode)
File "C:\Users\RAJA MONSINGH\AppData\Roaming\Python\Python38\site-packages\PIL\ImageDraw.py", line 59, in __init__
im.load()
File "C:\Users\RAJA MONSINGH\AppData\Roaming\Python\Python38\site-packages\PIL\ImageFile.py", line 233, in load
s = read(self.decodermaxblock)
File "C:\Users\RAJA MONSINGH\AppData\Roaming\Python\Python38\site-packages\PIL\JpegImagePlugin.py", line 394, in load_read
s = self.fp.read(read_bytes)
MemoryError
Process finished with exit code 1
Files needed to run the program:
https://drive.google.com/file/d/1rS-auW0MHmdjoPlNET71isL8AyEPPyGT/view?usp=sharing
I'm having an issue trying to make a screen grab of an area defined by lines in a config file:
The following code:
def coordstore(): # check line 1 of config file (screencap name)
f=open('config.txt')
line=f.readlines()
coordstore.x1,coordstore.y1,coordstore.x2,coordstore.y2 = (line[4]).strip(),(line[5]).strip(),(line[6]).strip(),(line[7]).strip() # note: confusing. 0=line 1, 1=line2 etc.
coordstore.coords = coordstore.x1+',',coordstore.y1+',',coordstore.x2+',',coordstore.y2
coordstore.stripped = ' '.join((coordstore.coords))
print(coordstore.stripped)
return(coordstore.stripped)
coordstore()
def screenshot():
import pyscreenshot as ImageGrab2
# part of the screen
if __name__ == '__main__':
im = ImageGrab2.grab(bbox=(coordstore.stripped)) # X1,Y1,X2,Y2
im.save('tc.png')
screenshot()
prints exactly this value: 10, 20, 100, 300
it then fails with this Traceback:
Traceback (most recent call last):
File "file location", line 82, in <module>
screenshot()
File "file location", line 80, in screenshot
im = ImageGrab2.grab(bbox=(coordstore.stripped)) # X1,Y1,X2,Y2
File "PYCHARM\venv\lib\site-packages\pyscreenshot\__init__.py", line 67, in grab
to_file=False, childprocess=childprocess, backend=backend, bbox=bbox)
File "PYCHARM\venv\lib\site-packages\pyscreenshot\__init__.py", line 38, in _grab
x1, y1, x2, y2 = bbox
ValueError: too many values to unpack (expected 4)
If I manually replace (bbox=(coordstore.stripped)) with (bbox=(10, 20, 100, 300)) which is literally identical to what the variable prints, the code works perfectly but is not useful for my needs. What am I not seeing? Thanks for the help!
UPDATE:
I have tried another approach.
def coordstore(): # check line 1 of config file (screencap name)
f=open('config.txt')
line=f.readlines()
coordstore.x1 = (line[4]).strip()
coordstore.y1 = (line[5]).strip()
coordstore.x2 = (line[6]).strip()
coordstore.y2 = (line[7]).strip()
print(coordstore.x1+',', coordstore.y1+',', coordstore.x2+',', coordstore.y2)
return(coordstore.x1, coordstore.y1, coordstore.x2, coordstore.y2)
coordstore()
def screenshot():
import pyscreenshot as ImageGrab2
# part of the screen
if __name__ == '__main__':
im = ImageGrab2.grab(bbox=(coordstore.x1+',', coordstore.y1+',', coordstore.x2+',', coordstore.y2+',')) # X1,Y1,X2,Y2
im.save('tc.png')
screenshot()
This prints
10, 20, 100, 300
but returns the following Traceback errors:
Traceback (most recent call last):
File "module location", line 84, in <module>
screenshot()
File "module location", line 82, in screenshot
im = ImageGrab2.grab(bbox=(coordstore.x1+',', coordstore.y1+',', coordstore.x2+',', coordstore.y2+',')) # X1,Y1,X2,Y2
File "\PYCHARM\venv\lib\site-packages\pyscreenshot\__init__.py", line 67, in grab
to_file=False, childprocess=childprocess, backend=backend, bbox=bbox)
File "\PYCHARM\venv\lib\site-packages\pyscreenshot\__init__.py", line 42, in _grab
raise ValueError('bbox y2<=y1')
ValueError: bbox y2<=y1
As you have mentioned in comments the value of coordstore.stripped is string. And expected argument by ImageGrab2.grab is type of tuple so, first you have to convert it into tuple.
Update the method like this:
def screenshot():
import pyscreenshot as ImageGrab2
# part of the screen
if __name__ == '__main__':
im = ImageGrab2.grab(bbox=tuple(map(int, coordstore.stripped.split(', ')))) # X1,Y1,X2,Y2
im.save('tc.png')
I'm trying to segment one window in curses into several sub-windows (with derwin()).
The code creates two sub-windows and I can add a string; no problem with the first function. The second one is pretty much exactly the same but gives me an error when I try to add a string with addstr()
class Window(GUI):
'''
Window-object
'''
def __init__(self, y_max , x_max, y_pos , x_pos, Target, screen):
self.Win_Count = 0
self.y_pos = y_pos
self.x_pos = x_pos
self.y_max = y_max
self.x_max = x_max
self.parent = screen
self.Target = Target
#Window-Objects
self.Win = self.create_win_parent(y_pos)
self.Name_Win = self.create_name_win(self.Win)
self.IP_Win = self.create_ip_win(self.Win)
def create_win_parent(self, y_pos):
y_size = 1
x_size = self.x_max - self.x_pos
new_win_obj = self.parent.derwin(y_size, x_size, self.y_pos, 0)
self.Win_Count += 1
return new_win_obj
def create_name_win(self, Win_Obj):
x = Win_Obj.derwin(1,40, 0,0)
x.box()
x.addstr(0,5," CUSTOMER NAME ")
return x
def create_ip_win(self, Win_Obj):
x = Win_Obj.derwin(1,15, 0,41)
x.box()
x.addstr(0,5," IP-ADDRESS ")
return x
I'm getting this vague error:
Traceback (most recent call last):
File "./2pollng.py", line 229, in <module>
wrapper(main) # Enter the main loop
File "/usr/lib/python2.6/curses/wrapper.py", line 43, in wrapper
return func(stdscr, *args, **kwds)
File "./2pollng.py", line 222, in main
Main_App.Run(screen)
File "./2pollng.py", line 106, in Run
self.Create_Win(self.Inv.index(e), e)
File "./2pollng.py", line 90, in Create_Win
Win_Obj = Window(self.y_max, self.x_max, y_pos, x_pos, Target_x, self.screen)
File "./2pollng.py", line 141, in __init__
self.IP_Win = self.create_ip_win(self.Win)
File "./2pollng.py", line 160, in create_ip_win
x.addstr(0,5," IPADDRESS ")
_curses.error: addstr() returned ERR
def create_ip_win(self, Win_Obj):
x = Win_Obj.derwin(1,15, 0,41)
x.box()
x.addstr(0,5," IP-ADDRESS ")
return x
In this function Win_Obj.derwin(1,15, 0,41) shows that x-pos should between 0 and 14. While in the code addstr(0,5," IP-ADDRESS ") x starts at 5 and the length of string " IP-ADDRESS " is greater than (15-5). So you got the ERROR.
Not really sure about the specifics but it had ( as indicated by the interpreter, duh) something to do with the strings and them not having enough space in the subwindows i created.