我有一个python脚本,使用PIL将文本写入图像.这一切都很好,除非我遇到带有回车符的字符串.我需要在文本中保留回车符.而不是将回车写入图像,我得到一个小盒子字符,返回应该是.以下是编写文本的代码:
<code>
draw = ImageDraw.Draw(blankTemplate)
draw.text((35 + attSpacing, 570),str(attText),fill=0,font=attFont)
</code>
Run Code Online (Sandbox Code Playgroud)
attText是我遇到麻烦的变量.我在写它之前将它转换为字符串,因为在某些情况下它是一个数字.
谢谢你的帮助.
我想像这样转换图像,为django中的图片添加效果,如此处所述.

我决定将它作为一个伟大的django -imagekit/photologue 的过程来实现
我对PIL的了解不是很好,所以我的问题是
如何通过PIL中的正弦偏移来打算像素列?
任何提示(代码,lins,一般想法)都受到欢迎
django photologue imagekit python-imaging-library django-imagekit
如何使用黑色背景创建新图像并在其上粘贴另一个图像?
我要做的是将一些128x128透明图标转换为75x75黑色背景图标.
不起作用......
import Image
theFile = "/home/xxxxxx/Pictures/xxxxxx_128.png"
img = Image.open(theFile)
newImage = Image.new(img.mode, img.size, "black")
newImage.paste(img)
newImage.resize((75,75))
newImage.save("out.png")
print "Done"
谢谢!
我在确定保存文件时要给出的路径定义时遇到麻烦-绝对系统路径或静态文件目录的相对路径
我最小化的照片模型-
class Photos(models.Model):
photo = models.ImageField(upload_to=get_photo_storage_path)
Run Code Online (Sandbox Code Playgroud)
我的缩略图模型-
class PhotosThumbnails(models.Model):
photo = models.ForeignKey(Photos)
dp = models.ImageField(upload_to=get_thumbnail_storage_path)
Run Code Online (Sandbox Code Playgroud)
我的看法-
photo_thumbnail_obj = PhotosThumbnails(photo = photos_object)
size = 40, 40
im = Image.open(str(obj.photo.path))
im.thumbnail(size)
im.save( ?? , 'JPEG')
photo_thumbnail_obj.dp = ??
photo_thumbnail_obj.save()
Run Code Online (Sandbox Code Playgroud)
帮助Python人员,我想使用相对路径保存它。
我想用PIL进行图像转换/重写,只需使用RAM内存.我在RAM中有以字节为单位的图像,我想将其转换为其他格式或可能相同.我知道我可以将其保存在具有某些名称的文件系统上,但我想只使用RAM而不触及文件系统.我没有找到任何例子.任何帮助,将不胜感激!谢谢!
我有一个模型,我想在Django Admin中保存
class Product(models.Model):
# other fields
img1 = models.ImageField(upload_to='%s/%s/1/large/' % (category, prod_no))
img1_thumb = models.ImageField(upload_to='%s/%s/1/thumbnail/' % (category, prod_no), editable=False)
def save(self, *args, **kwargs):
newImg1 = resizeImg(self.img1, (75, 112))
self.img1_thumb = newImg1
super(Product, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
调整大小图像功能
def resizeImg(image, size):
try:
if imghdr.what(image) == 'jpeg':
img = Image.open(image)
img.thumbnail(size, Image.ANTIALIAS)
# this is how to save the img
# img.save(filename + '.jpg', 'JPEG', quality=75)
return img
else:
return 'not_jpg'
except Exception, e:
return 'exception'
Run Code Online (Sandbox Code Playgroud)
在Django Admin中保存此错误会产生此错误
AttributeError at /admin/myapp/product/add/
_committed
Run Code Online (Sandbox Code Playgroud)
更新 …
当我尝试在Mac OS X 10.8.1 Mountain Lion上构建PIL时,我得到以下结果:
$ sudo python setup.py install
Password:
running install
running build
running build_py
running build_ext
--- using frameworks at /System/Library/Frameworks
building '_imaging' extension
clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -DHAVE_LIBJPEG -DHAVE_LIBZ -IlibImaging -I/opt/local/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include -I/usr/local/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _imaging.c -o build/temp.macosx-10.8-intel-2.7/_imaging.o
clang: warning: argument unused during compilation: '-mno-fused-madd'
_imaging.c:75:10: fatal error: 'Python.h' file not …Run Code Online (Sandbox Code Playgroud) 我需要打开一个图像,验证图像,然后重新打开它(参见以下PIL文档引用的句子)
im.verify()
尝试确定文件是否已损坏,而不实际解码图像数据.如果此方法发现任何问题,则会引发适当的异常.此方法仅适用于新打开的图像; 如果图像已加载,则结果未定义.此外,如果需要在使用此方法后加载图像,则必须重新打开图像文件.
这就是我在我的代码中picture所拥有的,其中是一个django InMemoryUploadedFile对象:
img = Image.open(picture)
img.verify()
img = Image.open(picture)
Run Code Online (Sandbox Code Playgroud)
前两行工作正常,但我得到第三行的以下错误(我试图"重新打开"图像):
IOError: cannot identify image file
正如文档建议的那样,重新打开图像文件的正确方法是什么?
我有一个带有这样嘈杂背景的图像(爆炸,每个正方形都是一个像素)。我正在尝试规范化黑色背景,以便可以完全替换颜色。

这就是我在想的(伪代码):
for pixel in image:
if is_similar(pixel, (0, 0, 0), threshold):
pixel = (0, 0, 0)
Run Code Online (Sandbox Code Playgroud)
什么样的功能可以让我比较两个颜色值以匹配某个阈值?
我正在使用PIL压缩上传的图片(FileField)。但是我遇到一个错误,我认为这是双重保存的问题?(保存我的图像,然后保存包括该图像的整个表单)。我想commit=False在保存图像时执行,但没有出现,这是可能的。这是我的代码:
...
if form_post.is_valid():
instance = form_post.save(commit=False)
instance.user = request.user
if instance.image:
filename = instance.image
instance.image = Image.open(instance.image)
instance.image.thumbnail((220, 130), Image.ANTIALIAS)
instance.image.save(filename, quality=60)
instance.save()
Run Code Online (Sandbox Code Playgroud)
'JpegImageFile' object has no attribute '_committed'在最后一行(instance.save())返回错误
有人可以找出问题所在吗?-知道我该如何解决吗?
完整回溯:
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/Users/zorgan/Desktop/project/site/post/views.py" in post
68. …Run Code Online (Sandbox Code Playgroud)