我制作了一个卷积神经网络,我希望它获取输入图片和输出图片,但是当我将图片转换为张量时,它们的尺寸错误:
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [20, 3, 5, 5], but got 3-dimensional input of size [900, 1440, 3] instead
Run Code Online (Sandbox Code Playgroud)
如何更改图片的尺寸?为什么需要改变?以及如何将输出变成图片?我尝试使用
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
Run Code Online (Sandbox Code Playgroud)
使 img 标准化,但没有改变尺寸。这是我的神经网络
def __init__(self):
super(Net, self).__init__()
torch.nn.Module.dump_patches = True
self.conv1 = nn.Conv2d(3, 20, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(20, 16, 5)
self.fc1 = nn.Linear(16*5*5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 16*5*5)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = …Run Code Online (Sandbox Code Playgroud) multidimensional-array python-imaging-library python-3.x conv-neural-network pytorch
以下代码给我一个错误:
def save(self):
self.filePath, _ = QFileDialog.getOpenFileName(self, "Open File ", "", "JPEG(*.JPEG *.jpeg *.JPG *.jpg)")
img =Image.open(self.filePath)
conn.execute("INSERT INTO DriverInfo(driverImg)VALUES(?)", repr(memoryview(img)))
conn.commit()
Run Code Online (Sandbox Code Playgroud)
错误是:
TypeError: memoryview: a bytes-like object is required, not 'JpegImageFile'
我正在尝试使用 python 中的 Pillow查找某些文本的大小(以磅为单位) 。据我了解,以磅为单位的字体大小对应于目标显示器或表面上的实际物理英寸,每英寸 72 点。当我使用 Pillow 的textsize方法时,我可以找到以给定字体大小(以磅为单位)呈现的某些文本的像素大小,但不知道如何返回基于英寸的坐标系,因为我没有(并且无法设置)图像的像素密度:
from PIL import Image, ImageFont, ImageDraw
image = Image.new('RGBA', (400, 300), (255, 255, 255))
font = ImageFont.truetype('/Library/Fonts/Arial.ttf', 16)
image.info['dpi'] = 100
print( ImageDraw.Draw(image).textsize('Loreum ipsum', font=font) )
image.info['dpi'] = 1000
print( ImageDraw.Draw(image).textsize('Loreum ipsum', font=font) )
Run Code Online (Sandbox Code Playgroud)
将打印
(101, 18)
(101, 18)
Run Code Online (Sandbox Code Playgroud)
如何获得给定文本的大小(以磅为单位) ?
我有一个巨大的图像数据集,如下所示:
我想改变这些的颜色。所有白色应保持白色,所有紫色应变为白色,其他所有内容应变为黑色。所需的输出如下所示:

我已经在下面编写了代码,它正在执行我想要的操作,但是需要很长时间才能浏览我拥有的大量图片。还有另一种更快的方法吗?
path = r"C:path"
for f in os.listdir(path):
f_name = (os.path.join(path,f))
if f_name.endswith(".png"):
im = Image.open(f_name)
fn, fext = os.path.splitext(f_name)
print (fn)
im =im.convert("RGBA")
for x in range(im.size[0]):
for y in range(im.size[1]):
if im.getpixel((x, y)) == (255, 255, 255, 255):
im.putpixel((x, y),(255, 255, 255,255))
elif im.getpixel((x, y)) == (128, 64, 128, 255):
im.putpixel((x, y),(255, 255, 255,255))
else:
im.putpixel((x, y),(0, 0, 0,255))
im.show()
Run Code Online (Sandbox Code Playgroud) 正如问题所说。当我的脚本在第一个屏幕上运行时,我可以截图第二个屏幕吗?我的意思是我的代码在第一个屏幕上运行,我只想捕获第二个屏幕。我该怎么做?当然,这一切都是ImageGrab.grab()由PIL模块完成的。
我正在尝试使用 PIL 版本 6.2.1 在 python 3.7.3 中执行以下代码:
render = ImageTk.PhotoImage(Image.open(pic))
Run Code Online (Sandbox Code Playgroud)
但它会导致如下错误消息:
Traceback (most recent call last):
File "F:/python/test/test10.py", line 12, in <module>
render = ImageTk.PhotoImage(Image.open(pic))
File "C:\Users\erica\AppData\Roaming\Python\Python37\site-packages\PIL\ImageTk.py", line 118, in __init__
self.__photo = tkinter.PhotoImage(**kw)
File "C:\Users\erica\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3545, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\erica\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3489, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
Exception ignored in: <function PhotoImage.__del__ at 0x0000027A91FEB0D0>
Traceback (most recent call last):
File "C:\Users\erica\AppData\Roaming\Python\Python37\site-packages\PIL\ImageTk.py", line …Run Code Online (Sandbox Code Playgroud) 我正在使用 PIL 的图像库来处理 JPG 文件。根据我的理解,调用 PIL 的open()函数应该返回一个 类型的对象Image。但是我遇到一个问题,它返回类型的对象PIL.JpegImagePlugin.JpegImageFile。这是我正在运行的代码:
from PIL import Image
for filename in os.listdir(""):
new_filename = filename
if(filename[0] == '.'):
new_filename = filename[2:]
picture = Image.open(new_filename, 'r')
print(type(picture))
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何解决这个问题吗?
我一直在尝试从扫描的 PDF(带有不可选择文本的图像)中提取文本。
但是,我得到的输出不是人类可读的。
我想要包含 pdf 链接中的日期、发票号的信息(https://drive.google.com/file/d/1qQsqhlSKTZs-hlswrV8PIirR36896KXZ/view)。
请帮助我以纯文本形式提取和存储相同的内容。
import PyPDF2
from PIL import Image
pdf_reader = PyPDF2.PdfFileReader(r'document.pdf', 'rb')
page = pdf_reader.getPage(85)
if '/XObject' in page['/Resources']:
xobject = page['/Resources']['/XObject'].getObject()
for obj in xobject:
if xobject[obj]['/Subtype'] == '/Image':
size = (xobject[obj]['/Width'], xobject[obj]['/Height'])
data = xobject[obj]._data
print("*******", data)
print(xobject[obj]['/Filter'])
Run Code Online (Sandbox Code Playgroud) 我想旋转并通过旋转的图像,就像所附的图像一样。我的结果不居中,文本超出图像。图像尺寸为 794x1096 你可以帮我吗?
这是我的代码:
x = input('Insert Name Here ')
y = input('Insert full name')
img = Image.open("Path/watermark_example.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('calibri.ttf', 55)
draw.text((30, 300),x,(128, 128, 128, 255), font=font)
draw.text((500, 500),x,(128, 128, 128, 255),font=font)
img1 = img.rotate(15, expand = True, fillcolor = 'white')
img.putalpha(128)
img.paste(img1)
img.show()
img.save(f'Path/watermark_example{y}.png')
Run Code Online (Sandbox Code Playgroud)