简体   繁体   English

需要为jpg添加标题,python找不到图像

[英]Need to add a caption to a jpg, python can't find the image

I am trying to add a caption - the file name - under the bottom edge of a jpg. 我正在尝试在jpg底部边缘下方添加标题-文件名。

I have the following Python code: 我有以下Python代码:

    import Image, ImageDraw, ImageFont, os, glob

list = glob.glob('*.jpg')

for infile in list:
    print infile
    file, ext = os.path.splitext(infile)
    outname = file + "_test.jpg"
    print outname
    im = Image.open(infile)
    d = ImageDraw.Draw(im)
    f = ImageFont.truetype("Arial.ttf", 16)
    d.text((4,0), file, font=f)
    im.save(outname)

It prints out the name of the file, so the file is there - so why can't it open it? 它会打印出文件名,因此文件就在其中-为什么它不能打开它?

The file exists, but I get the following error: 该文件存在,但出现以下错误: 在此处输入图片说明

Is there an easier way to add a caption to the bottom of the image? 有没有更简单的方法在图像底部添加标题?

That exception, Cannot identify image file , means that it was able to open the file, but then could not determine what type of image it is. Cannot identify image file这一异常表示它能够打开文件,但随后无法确定它是什么类型的图像。 PIL does this by reading the first 16 bytes and seeing if they match any of the well known magic numbers . PIL通过读取前16个字节并查看它们是否与任何众所周知的幻数相匹配来做到这一点。

Could you verify that the file you're trying to read is actually a jpeg? 您能否验证您尝试读取的文件实际上是jpeg?

If so, how did you get the version of PIL? 如果是这样,您如何获得PIL的版本? If it was compiled on a machine without libjpeg (or whatever the windows equivalent is), it might not support jpegs. 如果它是在没有libjpeg的机器上编译的(或Windows等效的机器),则它可能不支持jpegs。 See this other question for a possible solution to that issue, though I don't have a windows machine to test with. 请参阅此其他问题 ,以寻求对该问题的可能解决方案,尽管我没有要测试的Windows机器。

"You can use either a string (representing the filename) or a file object as the file argument. In the latter case, the file object must implement read, seek, and tell methods, and be opened in binary mode." “您可以使用字符串(代表文件名)或文件对象作为文件参数。在后一种情况下,文件对象必须实现读取,查找和告诉方法,并以二进制模式打开。”

Try this: 尝试这个:

im = Image.open(open(infile, 'rb'))

or: 要么:

from StringIO import StringIO
im = Image.open(StringIO(infile))

Thanks to the help of Nick & Manu pointing me in the right dierction, I worked out a solution that works: 多亏了尼克和马努(Nick&Manu)的帮助,我指出了正确的方向,所以我制定了一个可行的解决方案:

    import Image, ImageDraw, ImageFont
from PIL import Image
import glob, os
import Tkinter
import ImageTk
import fnmatch

list = glob.glob('*.jpg')

cnt = 0
for infile in list:
    cnt += 1
    print infile
    file, ext = os.path.splitext(infile)
    outname = file + "_test.jpg"
    print outname
    #im = Image.open(infile)
    im = Image.open(open(infile, 'rb'))
    d = ImageDraw.Draw(im)
    f = ImageFont.truetype("c:/windows/fonts/arial.ttf", 200)
    x = 10
    y = 550
    d.text((x, y), str(cnt), font=f)
    im.save(outname)
    print "==========="

Note that this uses the direct path to the font, and I set the x, y location on the image. 请注意,这使用了字体的直接路径,并且我在图像上设置了x,y位置。

I also had to use a very large font size since the original file is a very large full image from the camera. 我还必须使用非常大的字体,因为原始文件是相机的非常大的完整图像。

I used the cnt as a string for continuity - the originals have a number of names. 我将cnt用作连续性的字符串-原稿具有许多名称。

All I need to do now is locate the bottom of the image and place the string there... 我现在要做的就是找到图像的底部并将字符串放在此处...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM