Try   HackMD

Робота з картинками в Python

Установка потрібних бібліотек

pip install numpy pip install pygame pip install pillow pip install pyopengl pip install opencv-python pip install requests # установити https://www.microsoft.com/en-us/download/details.aspx?id=48145

Показати картинку через Pillow

from PIL import Image im = Image.open("sample-image.png") im.show()

Показати вікно через Pygame

import pygame pygame.init() (width, height) = (300, 200) screen = pygame.display.set_mode((width, height)) screen.fill((0,0,0)) pygame.display.update() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit()

Показати картинку через Pygame

import pygame from PIL import Image im = Image.open("/home/danbst/Pictures/readbooks.png") pygameim = pygame.image.fromstring(im.tobytes(), im.size, im.mode) pygameim = pygame.transform.scale(pygameim, (500, 400)) pygame.init() (width, height) = (700, 500) screen = pygame.display.set_mode((width, height)) screen.fill((0,0,0)) screen.blit(pygameim, (50, 50)) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.display.update() pygame.quit()

Скачати картинку з інтернету та показати через Pygame

import pygame from PIL import Image import requests from io import BytesIO import time # Скачування start_time = time.time() print('Downloading image....') response = requests.get('https://i.imgur.com/y2mYjYl.png') im = Image.open(BytesIO(response.content)) print('Image downloaded in {:.2f} seconds' .format(time.time() - start_time)) # перетворення у формат pygame pygameim = pygame.image.fromstring(im.tobytes(), im.size, im.mode) # маштабування pygameim = pygame.transform.scale(pygameim, (500, 400)) # ініціалізація pygame pygame.init() (width, height) = (700, 500) screen = pygame.display.set_mode((width, height)) screen.fill((0,0,0)) # >>>> МАЛЮВАННЯ <<<<< screen.blit(pygameim, (50, 50)) pygame.display.update() # Вічний цикл для обробки подій running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.display.update() pygame.quit()

Показати 3D куб

import pygame as pg from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import * cubeVertices = ((1,1,1),(1,1,-1),(1,-1,-1),(1,-1,1),(-1,1,1),(-1,-1,-1),(-1,-1,1),(-1,1,-1)) cubeEdges = ((0,1),(0,3),(0,4),(1,2),(1,7),(2,5),(2,3),(3,6),(4,6),(4,7),(5,6),(5,7)) cubeQuads = ((0,3,6,4),(2,5,6,3),(1,2,5,7),(1,0,4,7),(7,4,6,5),(2,3,0,1)) def wireCube(): glBegin(GL_LINES) for cubeEdge in cubeEdges: for cubeVertex in cubeEdge: glVertex3fv(cubeVertices[cubeVertex]) glEnd() def solidCube(): glBegin(GL_QUADS) for cubeQuad in cubeQuads: for cubeVertex in cubeQuad: glVertex3fv(cubeVertices[cubeVertex]) glEnd() def main(): pg.init() display = (300, 200) pg.display.set_mode(display, DOUBLEBUF|OPENGL) gluPerspective(45, (display[0]/display[1]), 0.1, 50.0) glTranslatef(0.0, 0.0, -5) while True: for event in pg.event.get(): if event.type == pg.QUIT: pg.quit() quit() glRotatef(1, 1, 1, 1) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #solidCube() wireCube() pg.display.flip() pg.time.wait(10) if __name__ == "__main__": main()

Показати картинку на на 3D кубі

import pygame as pg from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import * from PIL import Image import numpy cubeVertices = ((1,1,1),(1,1,-1),(1,-1,-1),(1,-1,1),(-1,1,1),(-1,-1,-1),(-1,-1,1),(-1,1,-1)) cubeEdges = ((0,1),(0,3),(0,4),(1,2),(1,7),(2,5),(2,3),(3,6),(4,6),(4,7),(5,6),(5,7)) cubeQuads = ((0,3,6,4),(2,5,6,3),(1,2,5,7),(1,0,4,7),(7,4,6,5),(2,3,0,1)) def wireCube(): glBegin(GL_LINES) for cubeEdge in cubeEdges: for cubeVertex in cubeEdge: glVertex3fv(cubeVertices[cubeVertex]) glEnd() def solidCube(tex): glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, tex) glBegin(GL_QUADS) for cubeVertex, texVertex in zip(cubeQuads[0], [(1.0,1.0), (1.0, 0.0), (0.0, 0.0), (0.0, 1.0)]): glTexCoord2f(texVertex[0],texVertex[1]) glVertex3fv(cubeVertices[cubeVertex]) glEnd() glDisable(GL_TEXTURE_2D) glBegin(GL_LINES) for cubeQuad in cubeQuads[1:]: for cubeVertex in cubeQuad: glVertex3fv(cubeVertices[cubeVertex]) glEnd() def main(): pg.init() display = (700, 500) pg.display.set_mode(display, DOUBLEBUF|OPENGL) gluPerspective(45, (display[0]/display[1]), 0.1, 50.0) glTranslatef(0.0, 0.0, -5) tex = ReadTexture('/home/danbst/Pictures/readbooks.png') while True: for event in pg.event.get(): if event.type == pg.QUIT: pg.quit() quit() glRotatef(1, 1, 1, 1) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) solidCube(tex) #wireCube() pg.display.flip() pg.time.wait(10) def ReadTexture(filename): # PIL can open BMP, EPS, FIG, IM, JPEG, MSP, PCX, PNG, PPM # and other file types. We convert into a texture using GL. print('trying to open', filename) try: image = Image.open(filename) imgae = image.convert("RGB") except IOError as ex: print('IOError: failed to open texture file') message = template.format(type(ex).__name__, ex.args) print(message) return -1 print('opened file: size=', image.size, 'format=', image.format) imageData = numpy.array(list(image.getdata()), numpy.uint8) textureID = glGenTextures(1) glPixelStorei(GL_UNPACK_ALIGNMENT, 4) glBindTexture(GL_TEXTURE_2D, textureID) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.size[0], image.size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData) image.close() return textureID if __name__ == "__main__": main()