标签: python-imaging-library

如何在 Flask 中返回图像作为 GET 请求响应的一部分?

我设置了一个基本的 Python Flask API,它没有将 ID 作为 GET 请求的参数。在 get 请求逻辑中,我正在对另一个资源执行 get 请求,以检索与传入的 ID 关联的图像:

image = requests.get(url = image_url)
image = Image.open(BytesIO(image.content))
print(image.format)
Run Code Online (Sandbox Code Playgroud)

它返回的图像格式为JPEG.

我想将其添加为 API 响应的一部分。这是我的 GET 端点的定义:

@app.route('/getmsg/', methods=['GET']) 
def respond():
Run Code Online (Sandbox Code Playgroud)

这就是我尝试返回从其他资源获得的图像的方式:

return {"image": send_file(image, mimetype='image/jpeg'), "data": jsonify(return_list)}
Run Code Online (Sandbox Code Playgroud)

我已经尝试遵循这个 stackoverflow 问题:How to return images inflask response?

这就是我如何到达现在的位置。我通过此端点发出 get 请求:http://localhost:5000/getmsg/?id=7187167507933759112

我也尝试过只返回图像,如下所示:

return send_file(image, mimetype='image/jpeg')
Run Code Online (Sandbox Code Playgroud)

但它给了我同样的错误。

除了返回图像之外,一切正常。

我希望能够在 get 请求的响应中看到图像,但现在它给出 500 内部服务器错误。

这是我在终端中得到的响应:

TypeError: <Response 693 bytes [200 OK]> is not JSON serializable
Run Code Online (Sandbox Code Playgroud)

任何有关我做错了什么的指导将不胜感激。谢谢。

python api python-imaging-library flask

2
推荐指数
1
解决办法
9260
查看次数

PIL getpalette() 返回 None

我用 Matlab 编写了一个 .png 图像。当我看到它的大小时size(imageName),我明白了480 720 3。现在我需要用 Python 获取它的调色板。我尝试了以下方法:

from PIL import Image
Image.open('path\\to\\image.png').getpalette()
Run Code Online (Sandbox Code Playgroud)

当我打印这个输出时,我得到了None。我在 Matlab 中使用imwrite. 这里有什么问题吗?我应该以其他方式在 Matlab 中创建 .png 吗?有人可以帮忙吗?

python matlab python-imaging-library

2
推荐指数
1
解决办法
1918
查看次数

从图像中删除抗锯齿功能

我想从图像中删除抗锯齿功能。此代码将从图像中获取 4 种主要颜色,将每个像素与 4 种主要颜色进行比较并分配最接近的颜色。

import numpy as np
from PIL import Image

image = Image.open('pattern_2.png')
image_nd = np.array(image)
image_colors = {}

for row in image_nd:
    for pxl in row:
        pxl = tuple(pxl)
        if not image_colors.get(pxl):
            image_colors[pxl] = 1
        else:
            image_colors[pxl] += 1

sorted_image_colors = sorted(image_colors, key=image_colors.get, reverse=True)
four_major_colors = sorted_image_colors[:4]


def closest(colors, color):
    colors = np.array(colors)
    color = np.array(color)
    distances = np.sqrt(np.sum((colors - color) ** 2, axis=1))
    index_of_smallest = np.where(distances == np.amin(distances))
    smallest_distance = colors[index_of_smallest]
    return smallest_distance[0]


for y, …
Run Code Online (Sandbox Code Playgroud)

python numpy image-processing antialiasing python-imaging-library

2
推荐指数
1
解决办法
2850
查看次数

如何使用 PIL 加载 16 位/通道 RGB(A) 图像?

我正在尝试使用 PIL 加载 16 位颜色 RGBA 图像。

我从以下链接下载了图像 pnggrad16rgba.png: https://www.fnordware.com/superpng/samples.html 在此输入图像描述 我检查了一下它确实有每像素 16 位的颜色通道:

Windows 资源管理器中文件属性的屏幕截图

但是当我尝试在 PIL 中加载数据时,我得到uint8数据:

>>> from PIL import Image
>>> import numpy
>>> im = Image.open("D:/pnggrad16rgba.png")
>>> arr = numpy.array(im)
>>> arr.dtype
dtype('uint8')
>>> arr[0, 0]
array([  0,   0, 255, 255], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)

有没有办法访问 16 位数据而不将其向下转换为uint8with PIL

如果没有,还有什么其他库可以处理这个问题?

python numpy python-imaging-library

2
推荐指数
1
解决办法
3118
查看次数

如何使用 Python OpenCV 创建这种桶形/径向畸变?

我正在使用 python / opencv 代码制作自定义虚拟现实耳机。我需要能够扭曲图像以创建“桶形扭曲”/“径向扭曲”效果。

一些图片来解释:

在此输入图像描述

在此输入图像描述

在此输入图像描述

我已经拥有source_image想要使用并向用户展示的内容,并且已经将它们并排放置。现在我只需要类似的东西out = cv2.createBarrelDistortion(source_image, params)。(我不介意能够调整一些参数,如畸变中心、畸变幅度等,这样我就可以让它看起来适合我得到的任何定制镜头。)

非常感谢任何帮助!

python opencv image-processing computer-vision python-imaging-library

2
推荐指数
2
解决办法
1万
查看次数

使用 PIL 为高维图像绘制矩形时,draw.rectangle([x1, y1, x2, y2], fill="Black") 中出现错误

当使用 PIL python 库为高维 png 文件(770x1024)绘制矩形时,我在 draw.rectangle([x1, y1, x2, y2], fill="Black") 中遇到错误。但它适用于中等尺寸的图像。

img = Image.open(BytesIO(file_byte_string))
width, height = img.size
.
.
if(doc.pages):
  page = doc.pages[0]
.
.
for field in page.form.fields: 
  if(field.key and field.value):

.
.    
  x1 = field.value.geometry.boundingBox.left*width
  y1 = field.value.geometry.boundingBox.top*height-2
  x2 = x1 + (field.value.geometry.boundingBox.width*width)+5
  y2 = y1 + (field.value.geometry.boundingBox.height*height)+2
  draw = ImageDraw.Draw(img) 
  draw.rectangle([x1, y1, x2, y2], fill="Black")
.
.
Run Code Online (Sandbox Code Playgroud)

示例 x1, y1, x2, y2 抛出错误:x1: 504.6949750185013 y1: 243.70870971679688 x2: 557.9484252631664 y2: 255.90338134765625 我该如何处理这个问题?我是否需要以编程方式调整大小或任何其他替代解决方案?

这是堆栈跟踪:

======================================================================
ERROR: …
Run Code Online (Sandbox Code Playgroud)

python python-imaging-library python-3.x redaction

2
推荐指数
1
解决办法
1275
查看次数

使用python的pillow库:如何在不创建图像的绘制对象的情况下绘制文本

下面的代码显示了如何通过创建绘图对象在图像上写入文本

from PIL import Image, ImageFont, ImageDraw

image =  Image.new(mode = "RGBA" , size= (500, 508) )

draw = ImageDraw.Draw(image)

font = ImageFont.load("arial.pil")

draw.text((10, 10), "hello", font=font)

Run Code Online (Sandbox Code Playgroud)

我要求的是如何将文本作为枕头对象返回,这样我就可以将其粘贴到另一个图像上,而不必创建图像对象并绘制对象,然后在其上写入文本,我只想将原始文本作为枕头对象稍后在 .paste() 函数中使用它。类似的东西(代码不存在,但这是我可以想象的):

from PIL import Image, ImageFont, ImageDraw

image =  Image.new(mode = "RGBA" , size= (500, 508) )

font = ImageFont.load("arial.pil")

text = font.loadtext("hello") #imaginary part (a pillow object)

image.paste(text, (10,10))
Run Code Online (Sandbox Code Playgroud)

python-imaging-library python-3.x

2
推荐指数
1
解决办法
2946
查看次数

如何使用Python图像库(PIL)突出显示图像的一部分?

如何突出显示图像的一部分?(位置定义为 4 个数字的元组)。你可以想象它就像我有电脑主板的图像,我需要突出显示例如CPU插槽所在的部分。

python image highlight python-imaging-library

2
推荐指数
1
解决办法
3243
查看次数

在 Python 中更改图片的对比度和清晰度

我是编码新手,有些代码不是最干净的(如下所示)。有没有更有效的方法来改变 python 中 Pillow 的清晰度和对比度,而无需保存和打开文件两次?

我的可怕的代码:

        im = Image.open(image.jpg)
        enhancer = ImageEnhance.Contrast(im)
        im_output = enhancer.enhance(factor)
        im_output.save(image.jpg)

        im = Image.open(image.jpg)
        enhancer = ImageEnhance.Sharpness(im)
        im_output = enhancer.enhance(factor)
        im_output.save(updated image.jpg)
Run Code Online (Sandbox Code Playgroud)

python python-imaging-library

2
推荐指数
1
解决办法
1911
查看次数

从 PIL 中绘制点线或虚线矩形

我想用PIL画一个虚线矩形。

我知道我可以绘制 4 条不同的虚线,使其看起来像一个矩形:

for x in range(0, Width_of_image, 5):
    d.line([(x, 30), (x + 2, 30)],fill ="black", width =2) 
Run Code Online (Sandbox Code Playgroud)

但是有没有办法使用 绘制一个矩形draw.rectangle,即不绘制 4 条不同的线?

python python-imaging-library python-3.x

2
推荐指数
1
解决办法
4998
查看次数