在Python中获取输入图片的宽度和高度
问题描述:
我们的任务是输入一张图片并创建一个程序,该程序写入一个将在python 2.7.5中绘制该图片的程序。这里是我的代码:在Python中获取输入图片的宽度和高度
from pygame import *
inFile = open("picture.jpg")
out = open('fa3.py','w')
width = inFile.get_width()
height = inFile.get_height()
out.write('''
from pygame import *
screen=display.set_mode((800,600))
running=True
while running:
for evt in event.get():
if evt.type == QUIT:
running = False
''')
for i in range(width,height):
c = inFile.get_at()
out.write('''draw.cricle(screen,(c),x,y,2)''')
我有收到该文件的高度和宽度,也发现图像中每个像素位置的麻烦
。我的老师告诉我使用“.get_width和height”,但它似乎不起作用。我知道我的x,y没有被定义,但是这将是插入每个像素位置的地方。任何帮助,将不胜感激。谢谢。
答
PIL图像模块将能够做你想做的。
http://effbot.org/imagingbook/image.htm
实施例来显示的图像
from PIL import Image
im = Image.open("bride.jpg")
im.show()
下列附加代码将让你操纵像素(只要其保存后)
pixels = im.load()
print pixels[x, y]
pixels[x, y] = value #value is a tuple in this format (normally) (235,154,124) or (Red,Green,Blue)
要获得图像大小
im.size() #width and height, (400,600)
im.size[0] #width, 400
im.size[1] #height, 600
答
您可以使用PIL(Python图像库)做:
从PIL导入图像
IM = Image.image( “image.png”)
普林im.size
宽度,高度= im.size
如果那任何帮助
我不会用pygame的这一点,你可以使用图片或PIL,这将是更有效的。 – Aidan