我正在尝试使用Python Imaging Library将图像粘贴到backgorund上,如下所示:
card = Image.new("RGB", (220, 220), (255, 255, 255))
img = Image.open("/Users/paulvorobyev/test.png")
...
x, y = img.size
card.paste(img, (0, 0, x, y), img)
card.save("test.png")
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我得到:
"ValueError: bad transparency mask"
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
我有一个(n,m)数组,我一直在想象matplotlib.pyplot.imshow.我想将这些数据保存在某种类型的光栅图形文件中(例如png),以便:
imshowinterpolation='nearest'的imshow).我怎样才能做到这一点?
我已经看到一些代码可以做到这一点,通过使用interpolation='nearest'和强制matplotlib(勉强)关闭轴,空格等.但是,必须有一些方法来更直接地做到这一点 - 也许与PIL?毕竟,我有基础数据.如果我可以为底层数组的每个元素获取RGB值,那么我可以使用PIL保存它.有没有办法从中提取RGB数据imshow?我可以编写自己的代码来将数组值映射到RGB值,但我不想重新发明轮子,因为matplotlib中已经存在该功能.
我安装了Pillow,之后我想做:
from PIL import Image
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 61, in <module>
ImportError: cannot import name _imaging
Run Code Online (Sandbox Code Playgroud)
但是,如果我单独导入这些,一切都很好,即:
import _imaging
import Image
Run Code Online (Sandbox Code Playgroud)
你知道问题可能是什么吗?
我正在学习使用'枕头5.0'后面的书'用蟒蛇自动化无聊的东西'
有关图像对象的信息
In [79]: audacious = auda
In [80]: print(audacious.format, audacious.size, audacious.mode)
PNG (1094, 960) RGBA
Run Code Online (Sandbox Code Playgroud)
当我尝试转换文件类型时,它报告错误.
In [83]: audacious.save('audacious.jpg')
OSError: cannot write mode RGBA as JPEG
Run Code Online (Sandbox Code Playgroud)
书中没有这样的错误.
我正在创建中国篆书的图像.我有三种真正的类型字体用于此任务(Jin_Wen_Da_Zhuan_Ti.7z,Zhong_Guo_Long_Jin_Shi_Zhuan.7z,Zhong_Yan_Yuan_Jin_Wen.7z,仅用于测试目的).以下是Microsoft Word中的外观

中文字符"我"(我/我).这是我的Python脚本:
import numpy as np
from PIL import Image, ImageFont, ImageDraw, ImageChops
import itertools
import os
def grey2binary(grey, white_value=1):
grey[np.where(grey <= 127)] = 0
grey[np.where(grey > 127)] = white_value
return grey
def create_testing_images(characters,
font_path,
save_to_folder,
sub_folder=None,
image_size=64):
font_size = image_size * 2
if sub_folder is None:
sub_folder = os.path.split(font_path)[-1]
sub_folder = os.path.splitext(sub_folder)[0]
sub_folder_full = os.path.join(save_to_folder, sub_folder)
if not os.path.exists(sub_folder_full):
os.mkdir(sub_folder_full)
font = ImageFont.truetype(font_path,font_size)
bg = Image.new('L',(font_size,font_size),'white')
for char in characters: …Run Code Online (Sandbox Code Playgroud) 我尝试使用pip/easy_install安装Pillow时遇到一个奇怪的错误:
cc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -arch i386 -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 -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch x86_64 -arch i386 -pipe -DHAVE_LIBJPEG -DHAVE_LIBZ -DHAVE_LIBTIFF -I/System/Library/Frameworks/Tcl.framework/Versions/8.5/Headers -I/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -I/usr/local/Cellar/freetype/2.5.3/include/freetype2 -I/private/var/folders/c_/r7sp373509jdb6_1xmmzvl9c0000gn/T/pip_build_tills13/Pillow/libImaging -I/System/Library/Frameworks/Python.framework/Versions/2.7/include -I/usr/local/include -I/usr/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _imaging.c -o build/temp.macosx-10.9-intel-2.7/_imaging.o
clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]
clang: note: this will be a hard error (cannot be downgraded to a warning) in the future
error: command 'cc' failed with exit status 1 …Run Code Online (Sandbox Code Playgroud) 我使用Pillow和Python 3.3.2并没有太多运气,我会感激一些帮助.我的问题是在安装Pillow之后,我无法导入Image.
我的设置:我安装了Linux Mint 16(在HP Pavilion dv7笔记本电脑上).我安装了Python 3.3.2+,它运行正常.我安装了Python 2.7.5+,它工作正常.
我做了什么:我按照http://pillow.readthedocs.org/en/latest/index.html上的说明安装了Pillow v2.4.0(PIL fork):
我开始时:
~$ pip install Pillow
Run Code Online (Sandbox Code Playgroud)
我安装了python-setuptools:
~$ sudo apt-get install python-dev python-setuptools
Run Code Online (Sandbox Code Playgroud)
而且,对于python 3:
~$ sudo apt-get install python3-dev python3-setuptools
Run Code Online (Sandbox Code Playgroud)
我在Ubuntu 12.04 LTS上安装了"先决条件",因此:
~$ sudo apt-get install libtiff4-dev libjpeg8-dev zlib1g-dev \
libfreetype6-dev liblcms2-dev libwebp-dev tcl8.5-dev tk8.5-dev python-tk
Run Code Online (Sandbox Code Playgroud)
(Mint 16基于Ubuntu 13.10,但我没有找到任何更新版本的先决条件列表)
发生了什么:使用python 2,导入图像工作,我可以打开一个.png图像并显示它.但是使用python 3,'import image'给出了'没有名为Image的模块'和'来自PIL import Image'给了'没有名为PIL的模块'
任何帮助将非常感激.
我对python很新,并且savePIL的Pillow fork 的功能有问题.
用这个最小的例子
import Image
im = Image.new("RGB", (200, 30), "#ddd")
im.save("image.png")
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 1667, in save
raise KeyError(ext) # unknown extension
KeyError: '.png'
Run Code Online (Sandbox Code Playgroud)
save函数中的相应行是
preinit()
[...]
try:
format = EXTENSION[ext]
except KeyError:
raise KeyError(ext) # unknown extension
Run Code Online (Sandbox Code Playgroud)
我查看了EXTENSION数组并检测到它是空的,尽管它应该preinit()由例如初始化from PIL import PngImagePlugin.PngImagePlugin.py电话Image.register_extension("PNG", ".png").观察此函数内部或其内部的数组PngImagePlugin确实充满了文件扩展名.
print(EXTENSION)然后在try-except-block之前放置一个空EXTENSION数组.
(与函数中SAVE的几行相同的问题相同save.)
任何帮助表示赞赏.
编辑:我最近从OpenSuse 13.1升级.到13.2.它在13.1中运行良好,但在13.2中没有运行.
我正在使用PIL的Pillow fork并继续收到错误
OSError:无法在0x103a47468>处识别图像文件<_io.BytesIO对象
当试图打开图像时.我使用virtualenv与python 3.4并没有安装PIL.
我试图根据遇到同样问题的其他人找到解决方案,然而,这些解决方案对我不起作用.这是我的代码:
from PIL import Image
import io
# This portion is part of my test code
byteImg = Image.open("some/location/to/a/file/in/my/directories.png").tobytes()
# Non test code
dataBytesIO = io.BytesIO(byteImg)
Image.open(dataBytesIO) # <- Error here
Run Code Online (Sandbox Code Playgroud)
图像存在于文件的初始打开中,并转换为字节.这似乎适用于几乎所有人,但我无法弄清楚为什么它失败了.
编辑:
dataBytesIO.seek(0)
Run Code Online (Sandbox Code Playgroud)
因为我不是通过流保存图像,所以我不是通过流保存图像,因此我只是用数据实例化BytesIO,因此(如果我正确地想到这一点),搜索应该已经为0.
我有一个名为"hindi.txt"的文件.它的内容如下.我正在使用Python3.5.
??????? ??????? ?? ??? ??? ??? ?????? ?????????, ??????? ???? ?????: ???????
9 ??? ?? ???? ????? ??? ?????, 59000 Cr ??? ???? ??????? 36 ????? ?????
WhatsApp ?? ????? ???? ??????????? ?????????? ?? ??? ??? ???? ?? Allo ???????
???? ???? ?? 10 ??????: ????? ??? ??? 150 ???? ???? ?? ??? ?? ?? ??? ?? ?????
???? ????? ???? ?? ???? ??? ????? PAK ?? LoC ?? ??? ????? ??? ?????
PAK ?? ????? ??? ???? ???? …Run Code Online (Sandbox Code Playgroud) pillow ×10
python ×7
bytesio ×1
django ×1
easy-install ×1
hindi ×1
imaging ×1
matplotlib ×1
numpy ×1
pip ×1
pycairo ×1
python-3.3 ×1
python-3.6 ×1
truetype ×1
unicode ×1