- html - 我的下拉菜单的内容关闭得太快
- c# - 使用 Html Agility Pack 从网页中的表中获取值而不使用 "SelectNode'
- html - 内容容器下的 CSS 下拉菜单
- html - 如何停止嵌套列表重叠父列表?
我正在尝试对图像进行简单的裁剪。这是代码
from PIL.Image import Image
def get_image_half(image, half="upper"):
if half not in ["upper", "lower", "right", "left"]:
raise Exception('Not a valid image half')
img = Image.open(image)
width, height = img.size
if half == "upper":
return img.crop(0, 0, width, height//2)
elif half == "lower":
return img.crop(0, height//2, width, height)
elif half == "left":
return img.crop(0, 0, width//2, height)
else:
return img.crop(width//2, 0, width, height)
def get_image_quadrant(image, quadrant=1):
if not (1 <= quadrant <= 4):
raise Exception("Not a valid quadrant")
img = Image.open(image)
width, height = img.size
if quadrant == 2:
return img.crop(0, 0, width//2, height//2)
elif quadrant == 1:
return img.crop(width//2, 0, width, height//2)
elif quadrant == 3:
return img.crop(0, height//2, width//2, height)
else:
return img.crop(width//2, height//2, width, height)
# test code for the functions
if __name__ == "__main__":
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
file = os.path.join(dir_path,"nsuman.jpeg")
image = Image.open(file)
for i in ["upper", "lower", "left", "right"]:
get_image_half(image, i).show()
for i in range(1,5):
get_image_quadrant(image, quadrant=i)
我收到以下错误。
image = Image.open(file)
AttributeError: type object 'Image' has no attribute 'open'
快速谷歌搜索让我找到了this link我将导入更改为
import PIL.Image
并将代码更改为
PIL.Image.open(image)
又出现了一个错误
Traceback (most recent call last):
File "quadrant.py", line 53, in <module>
get_image_half(image, i).show()
File "quadrant.py", line 10, in get_image_half
img = Image.open(image)
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2557, in open
prefix = fp.read(16)
AttributeError: 'JpegImageFile' object has no attribute 'read'
这里的问题是如何解决这些错误,更重要的是什么是 PIL.Image 和 PIL.Image.Image 以及它们的正确使用方法是什么?
最佳答案
PIL.Image
应该打开一个文件类型对象。打开后,您将拥有一个 PIL.Image.Image 对象:
from PIL import Image
image = Image.open('/foo/myfile.png')
open(fp, mode='r')
Opens and identifies the given image file.
This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the PIL.Image.Image.load method). See PIL.Image.new.
fp: A filename (string), pathlib.Path object, or a file object. The file object must implement file.read, file.seek, and file.tell methods, and be opened in binary mode.
returns: a PIL.Image.Image object.
关于python - PIL.Image 和 PIL.Image.Image 之间的混淆以及它们的正确使用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50880443/
我真的很困惑。我已经尝试使用带有 tomcat 的 Jax-rs 并使用所有注释,我能够使用 url 调用我的服务。因此,如果没有 Jax-rs,我可以简单地拥有一个 servlet 并调用我的服务。
是否有任何工具/商业混淆器可以混淆 WPF 控件中的 BAML 资源? 如果没有,就 IP 保护而言,这是一段艰难的时期,因为黑客可以通过使用 BAML 到 XAML 转换器轻松查看 BAML 资源。
嘿大家。我在尝试使用 COBOL 在 zOS 环境中解决的编码项目中遇到了一些麻烦。我需要读入一个文件并将它们放入索引表中(我知道将少于 90 条记录)。 让我感到困扰的是,我们受到项目参数的约束,以
我试图按照这个例子来理解 join() 方法: class PrintDemo { public void printCount() { try { for(int
当我编译我正在编写的代码,然后在 JD Gui 中查看时,方法显示带有如下标题: public void growSurface(Random paramRandom, int paramInt1,
我正在为重新分发准备 Android 库,它的代码必须进行混淆处理。我已经阅读了有关此主题的一些内容,并且决定使用 Android Library Project。它将作为 jar 分发(自动在/bi
两个混淆相关的问题: 1) 是否有任何工具可以将 F# 从 MSIL 目标形式反汇编回其源形式或接近它的形式?这不是通过默默无闻来实现安全性的尝试,但我想保护某些源代码免遭“盗窃”。 2) 我简要地查
谁能向我解释为什么 simulatedCase <- rbinom(100,1,0.5) simDf <- data.frame(CASE = simulatedCase) posterior_m0
我一直无法找到关于使用 AppDomains 时发生的事情的非常清楚的描述,所以希望有人能够启发我。我有一个简单的测试程序(基本上是扯掉了 MSDN example ): using System;
假设我有 2 个分支topic和 master如果我在 topic分支,然后运行 git rebase master它是 rebase master 还是 rebase 主题分支? 做 git r
我有一个类(class): public class LockTest { public void LockThis() { lock (this)
我正在尝试最小化/混淆我的 Angular 代码,但遇到了问题。我在这里阅读“缩小说明”http://docs.angularjs.org/tutorial/step_05但我定义我的 Control
我遇到了一些困惑的操作。 var a = 0.1; var b = 0.2; var c = 0.3; console.log(a); // 0.1 console.log(b); // 0.2 co
感谢您查看我的帖子 - 我正在尝试弄清楚如何在单击链接时关闭此下拉菜单,但我的 JavaScript 技能非常缺乏,而且代码似乎很困惑。这是 HTML:
混淆、哈希和加密之间有什么区别? 这是我的理解: 哈希是一种单向算法;无法逆转 混淆与加密类似,但不需要任何“ secret ”即可理解(ROT13 就是一个例子) 加密是可逆的,但需要“ secre
我有以下代码 my $content = $response->content; $content =~ /username=([\s\S]+?)&/; my $username = $1; prin
我在 .NET 中发现了一些与我预期的有点不同的东西。我粘贴的代码没有意义,但它是我拥有的一个复杂得多的函数的浓缩版。我实际上是在获取匿名类型信息作为参数(尚未创建匿名类型的实例),我需要创建该类型的
我正在努力解决 JavaFX 应用程序的混淆问题。使用此项目作为基础: https://github.com/openjfx/samples/tree/master/IDE/IntelliJ/Non-
是否可以制作一个与此类似的 CSV 阅读器 while((line = reader.readLine()) != null){ String[] values = line.
公共(public)类测试2 { 公共(public)静态无效主(字符串[]参数){ System.out.println("3 + 6"); System.out.println(3
我是一名优秀的程序员,十分优秀!