标签: python-imaging-library

Gif 帧持续时间似乎比预期慢

我正在使用枕头图像库创建 GIF。我遇到了一个问题,我试图使某些帧以尽可能短的帧持续时间快速闪烁,但是当我将帧的持续时间设置为 1(可能的最低持续时间,GIF 的持续时间为 100) ),实际上它的持续时间似乎比预期的要长。

\n

我使用类似于以下的简单Image.save()GIF 格式)命令保存 gif :

\n
# durations are actually in milliseconds in the pillow library, but\n# they translate to 100ths of a second (1/10 of the value input here)\ndurations = [650, 10, 900, 750, 10, 800, 10, ... ]\nmy_gif.save(filename, format=\'GIF\', save_all=True, duration=durations, loop=0, disposal=2)\n
Run Code Online (Sandbox Code Playgroud)\n

我不确定这个问题是特定于枕头库还是仅特定于 GIF 格式。但我注意到以下几点:

\n
    \n
  • gif 视觉上看起来比预期慢
  • \n
  • 当我将生成的 GIF 加载到 GIF 编辑工具(例如 EZGif.com)中时,它报告帧持续时间符合预期 - “闪烁帧”的持续时间为 1,这是我保存文件时使用的帧持续时间。
  • \n
  • 当我使用 EZGif加速GIF 时,奇怪的是,EZGif 生成的 …

python gif python-imaging-library

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

如何将“文本框”中的文本添加到图像中?

我正在开发一种工具,可以将文本添加到图像的特定区域,例如在对话气泡内。我正在生成一个边界框,并希望将文本限制为仅位于该边界框内,但到目前为止,查看 cv2 和 PIL 库,它们似乎只采用文本的起点,而不是边界框。

简历2:

import cv2

myimg = "471.jpg"

img = cv2.imread(myimg)

f_face = cv2.FONT_HERSHEY_PLAIN
f_size = 1
f_color = (255,255,255)
f_thickness = 2
text = 'Hello World'
x,y = 400,800
img = cv2.putText(img, text, (x,y), f_face, f_size, f_color, f_thickness)

cv2.imshow("Test", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

船级社:

from PIL import Image
from PIL import ImageDraw

myimg = "471.jpg"

img = Image.open(myimg)

id = ImageDraw.Draw(img)

id.text((400, 800), "Hello World", fill=(255, 255, 255))

img.show()
Run Code Online (Sandbox Code Playgroud)

理想情况下,我正在寻找一个库,它可以让我对文本进行与使用 Photoshop 时相同的控制,这意味着:字体大小、字体、垂直中心(到边界框/文本框)和水平居中(到边界框/文本框)以及自动调整字体大小或边界框的大小以适应文本,不会被切断(或者至少给出某种警告,表明文本已溢出)文本框,以便我可以制定一个减小字体大小并重试的过程)。

还有其他库有这种控制吗?或者有没有办法在 cv2 或 PIL 内部执行此操作? …

python image image-processing python-imaging-library python-3.x

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

如何在 python 中将多图像 TIFF 转换为 PDF?

我想在 python 中将多图像 TIFF 转换为 PDF。

我是这样写的代码。然而这段代码不起作用。我应该如何改变它?


images = []
img = Image.open('multipage.tif')

for i in range(4):
    try:
        img.seek(i)
        images.append(img)
    except EOFError:
        # Not enough frames in img
        break
images[0].save('multipage.pdf',save_all=True,append_images=images[1:])
Run Code Online (Sandbox Code Playgroud)

python python-imaging-library

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

Python 图像“字节” - 获取高度、宽度

我试图在将图像保存到数据库和 S3 之前检测widthheight。该图像位于bytes.

这是保存到之前的图像示例Django ImageField

在此输入图像描述

注意:我不想使用ImageFields height_fieldwidth_field因为它由于某种原因极大地减慢了服务器速度,所以我想手动执行。

使用请求下载图像:

def download_image(url):
    r = requests.get(url, stream=True)
    r.raw.decode_content = True
    return r.content
Run Code Online (Sandbox Code Playgroud)

python django python-imaging-library bytestream

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

Linux Mint Cinnamon 打开设置时出错(没有名为“PIL”的模块)

自从我将 Cinnamon 更新到 5.0.6 后,我无法访问“首选项”下的任何条目,什么也没有发生。当我尝试从 CLI 打开时,出现以下信息:

#> cinnamon-settings
No module named 'PIL'
Run Code Online (Sandbox Code Playgroud)

我搜索了很多,但没有适合我的问题。我知道这是 Python 和 PIL 模块或较新模块 Pillow 的问题,但提供的解决方案都不适合我。

一些信息:


#> python --version
Python 3.8.10

#> /usr/bin/env python
Python 3.8.10 (default, Sep 28 2021, 16:10:42) 

#> pip -V
pip 21.3 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)

#> pip show Pillow
Name: Pillow
Version: 8.4.0
Summary: Python Imaging Library (Fork)
Home-page: https://python-pillow.org
Author: Alex Clark (PIL Fork Author)
Author-email: aclark@python-pillow.org
License: HPND
Location: /home/xxx/.local/lib/python3.8/site-packages
Requires: 
Required-by: image, imageio, img2pdf, matplotlib, ocrmypdf, pikepdf, …
Run Code Online (Sandbox Code Playgroud)

python python-imaging-library linux-mint cinnamon

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

使用最近邻插值调整 1 通道 numpy(图像)数组的大小

我得到了一个形状为 388x388x1 的 1 通道 numpy 数组,其值范围为 1-150,作为 ML 推理的输出。我需要使用最近邻插值将数组大小调整为 5000x4000x1。

目前我正在使用 PIL 调整大小。它可以工作,但为此必须导入 PIL 感觉过于复杂。

output = np.random.randint(150, size=(388, 388))
width, height = 4000, 5000

pilImage = Image.fromarray(pred.astype(np.uint8))
pilImageResized = pilImage.resize((width, height), Image.NEAREST)

resizedOutput = np.array(pilImageResized).astype(np.uint8)
Run Code Online (Sandbox Code Playgroud)

在 numpy 中是否有更简单的方法可以实现我想要的目标?(不使用cv2.resize,scipy.interpolatePIL

python numpy nearest-neighbor scipy python-imaging-library

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

cv2.imencode() 的作用是什么?我如何使用 PIL 实现相同的目的?

我正在尝试制作一个 REST API,我遇到了这行代码-

_, img_encoded = cv2.imencode('.jpg', image)
Run Code Online (Sandbox Code Playgroud)

这是做什么的?不幸的是,我无法在 m 项目中使用 OpenCV,那么有什么方法可以使用 PIL 实现同样的效果呢?提前致谢!

python opencv python-imaging-library

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

使用 PIL,我可以以可定制的方式组合两个(或更多)频段吗?

我想“分解”一个Image(即split()),通过将其中一个通道与另一个通道组合来修改它,然后再次重新“组合”(即merge())它们。

此示例应使用我希望存在的功能将边缘映射到红色通道来突出显示边缘:

from PIL import Image, ImageFilter
base = Image.open("example.jpg")
base.show()
Run Code Online (Sandbox Code Playgroud)

基础图像

edges = (base
    .convert("L")
    .filter(ImageFilter.Kernel((3, 3), (-1, -1, -1, -1, 8,-1, -1, -1, -1), 1, 0))
    .point(lambda p: 255 if p > 10 else 0))
edges.show()
Run Code Online (Sandbox Code Playgroud)

边缘

r, g, b = base.split()

# now I want the blue channel/band for each pixel to be whatever respective
# pixel is brighter on a given set of bands

# this is what I want …
Run Code Online (Sandbox Code Playgroud)

python python-imaging-library

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

如何使用 Python Imaging Library (PIL) 将上传的图像保存到 FastAPI?

我正在使用图像压缩来减小图像大小。提交帖子请求时,我没有收到任何错误,但无法弄清楚为什么图像没有保存。这是我的代码:

@app.post("/post_ads")
async def create_upload_files(title: str = Form(),body: str = Form(), 
    db: Session = Depends(get_db), files: list[UploadFile] = File(description="Multiple files as UploadFile")):
    for file in files:
        im = Image.open(file.file)
        im = im.convert("RGB")
        im_io = BytesIO()
        im = im.save(im_io, 'JPEG', quality=50) 
Run Code Online (Sandbox Code Playgroud)

python python-imaging-library fastapi

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

Python 中的 DICOM 切片厚度

我正在开发一个项目,需要将 3D .stl 文件切片为一堆 DICOM 切片,并且我需要获取切片之间的距离。我已经弄清楚如何使用 Autodesk Meshmixer 在每个 DICOM 文件中包含真实的 .stl 尺寸(以毫米为单位);但是,我似乎无法获得切片之间的正确距离/厚度。

我使用 ImageJ 读取 DICOM 堆栈,体素深度(切片之间的距离,如果我没记错的话)与 X 轴上的像素尺寸相同。我想让体素深度等于 Z 轴上的切片高度

图片1 图2

正如您在最后一个屏幕截图中看到的,z 值是任意的。

这是我迄今为止所做的代码的摘要:

import numpy as np
import pyvista as pv
import os
import cv2
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from PIL import Image
import pydicom
import datetime
import time

# Load .stl
stl_file = pv.read(stl_path)

# Dimensions of the 3d file
xmin, xmax, ymin, ymax, zmin, zmax = stl_file.bounds

# Number of slices
num_slices = …
Run Code Online (Sandbox Code Playgroud)

python dicom slice python-imaging-library pydicom

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