Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 选择一个随机图像,并对水印进行PIL_Python_Python Imaging Library - Fatal编程技术网

Python 选择一个随机图像,并对水印进行PIL

Python 选择一个随机图像,并对水印进行PIL,python,python-imaging-library,Python,Python Imaging Library,当我在一个文件夹中随机选取一个图像并想用PIL进行编辑时,出现了一个错误 我的代码是 import os import random from PIL import Image from PIL import ImageDraw from PIL import ImageFont def watermark_text(input_image_path, output_image_path, text, pos):

当我在一个文件夹中随机选取一个图像并想用PIL进行编辑时,出现了一个错误

我的代码是

import os
import random
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

def watermark_text(input_image_path,
                   output_image_path,
                   text, pos):
    photo = Image.open(input_image_path)

    # make the image editable
    drawing = ImageDraw.Draw(photo)

    black = (255, 255, 255)
    font = ImageFont.truetype("font.ttf", 40)
    drawing.text(pos, text, fill=black, font=font)
    photo.show()
    photo.save(output_image_path)

if __name__ == '__main__':
    path="./bg"
    files=os.listdir(path)
    d=random.choice(files)
    img = d
    watermark_text(img, '1.jpg',
                   text='Risna Fadillah',
                   pos=(0, 0))
错误显示如下

import os
import random
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

def watermark_text(input_image_path,
                   output_image_path,
                   text, pos):
    photo = Image.open(input_image_path)

    # make the image editable
    drawing = ImageDraw.Draw(photo)

    black = (255, 255, 255)
    font = ImageFont.truetype("font.ttf", 40)
    drawing.text(pos, text, fill=black, font=font)
    photo.show()
    photo.save(output_image_path)

if __name__ == '__main__':
    path="./bg/"
    files=os.listdir(path)
    d=random.choice(files)
    img = path + d
    watermark_text(img, '1.jpg',
                   text='Risna Fadillah',
                   pos=(0, 0))
回溯(最近一次呼叫最后一次): 文件“quotes.py”,第26行,在 水印文本(img,'1.jpg', 文件“quotes.py”,第10行,水印文本 照片=图像。打开(输入图像路径) 文件“/data/data/com.termux/files/usr/lib/python3.8/site packages/PIL/Image.py”,第2878行,打开 fp=builtins.open(filename,“rb”)FileNotFoundError:[Errno 2]没有这样的文件或目录:“1qqq.jpg”


如何修复它?

我对此很粗心,我应该这样写

import os
import random
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

def watermark_text(input_image_path,
                   output_image_path,
                   text, pos):
    photo = Image.open(input_image_path)

    # make the image editable
    drawing = ImageDraw.Draw(photo)

    black = (255, 255, 255)
    font = ImageFont.truetype("font.ttf", 40)
    drawing.text(pos, text, fill=black, font=font)
    photo.show()
    photo.save(output_image_path)

if __name__ == '__main__':
    path="./bg/"
    files=os.listdir(path)
    d=random.choice(files)
    img = path + d
    watermark_text(img, '1.jpg',
                   text='Risna Fadillah',
                   pos=(0, 0))

抱歉。

错误是python找不到
1.jpg
。映像必须与python脚本位于同一目录中。如果仍然无法工作,则使用映像的完整路径应该可以工作。