Вот пример программы с массивом пикселей, которую я обещал. Он основан на популярном пакете Pygame. Я работаю с операционной системой Windows, поэтому, если вы работаете с какой-либо другой ОС, вам нужно будет прокомментировать или удалить команды Window-Speci c. Пиксельные массивы являются родными для Pygame, поэтому эта программа должна работать на других системах, совместимых с pygame, после отключения windows. Windows может связываться с ничего не подозревающим кодером, если только не будут устранены причуды, так что простите мне эти строки кода Windows, если вы используете хорошо функционирующую ОС. Я пытался удержать материал Windows в верхней части программы.
from os import system
import ctypes
from time import sleep
import pygame
from pygame import Color
pygame.init()
system('cls') # Windows-specific command to clear the Command Console Window.
# Windows can play weird tricks related to scaling when you try to position or show images with pygame.
# The following will disable the Windows Auto-scaling feature.
# Query DPI Awareness (Windows 10 and 8)
awareness = ctypes.c_int()
errorCode = ctypes.windll.shcore.GetProcessDpiAwareness(0, ctypes.byref(awareness))
# Set DPI Awareness (Windows 10 and 8)
errorCode = ctypes.windll.shcore.SetProcessDpiAwareness(2) # the argument is the awareness level, which can be 0, 1 or 2:
# (for 1-to-1 pixel control it seems to need a non-zero value.)
pygame.mouse.set_visible(False) # Hide the mouse
# Use a Rectangle object to find your system's maximum screen size.
screen_rec = pygame.display.set_mode((0,0),pygame.FULLSCREEN,32).get_rect()
# Create a light blue screen and show it.
screen = pygame.display.set_mode(screen_rec.size,pygame.FULLSCREEN,32)
screen.fill(Color("CornflowerBlue"))
pygame.display.flip()
sleep(2) # Pause for effect.
# Now let's create an example image using a pygame surface.
# The image will be our stand-in for an image loaded from a .bmp or .png
# file from disk. (You can also think of the image as a sprite.) We will
# display the image on the screen, but we will work on the image surface
# using a pixel array. The image could be created using a pixelarray too,
# but pygame's draw methods are much faster so we will create the image
# using those. Then, once the image is created, we will use
# a pixel array to sample the colors and then to modify that image.
image = pygame.Surface((300,100))
image.fill(Color("IndianRed"))
image_rec = image.get_rect() # Pygame Rectangle Objects are So useful!
pygame.draw.rect(image,Color("GhostWhite"),image_rec,7)
pygame.draw.line(image,Color("GhostWhite"),image_rec.topleft,image_rec.bottomright,4)
image_rec.center = screen_rec.center
screen.blit(image,image_rec)
pygame.display.update()
sleep(2) # Another dramatic pause!
# We now have an example image in 'image' which we will pretent was
# loaded from an image file. We want to sample the colors using
# a pixel array.
''' ================================================================='''
# The key thing to remember about pixel arrays is that the image is
# locked out when the array is created, and is only releasod when
# the array is deleted. You are working on the image at the pixel
# level through the array and will not be able to do anything else
# with the image while the pixel array exists.
''' ================================================================='''
my_pixel_array = pygame.PixelArray(image) # The image is locked out.
color_log = {} # Create a dictionary object. It will contain each unique
# color and the pixel count having that color.
for row in my_pixel_array:
for pix in row:
a,r,g,b = Color(pix) # ignore 'a', the 'alpha' value. It is always '0'
color = (r,g,b) # Create a new color tuple for our log. (We do this only for the sake of esthetics in the final output to the CCW.)
cnt = color_log.setdefault(color,0) # If the color is not in our log, add it, with a count value of 0. Always returns the count value.
color_log[color] = cnt+1 # Add one to the pixel count for that color
# Finished! But before we exit pygame and show the final counts,
# let's see how the pixel array can be used to change individual pixels.
# We could draw images or set random pixels to random colors, but let's
# change all of the GhostWhite pixels to black, then move the image
# across the screen.
for row in range(len(my_pixel_array)):
for pix in range(len(my_pixel_array[row])):
a,r,g,b = Color(my_pixel_array[row][pix])
color = (r,g,b)
if color == Color("GhostWhite"):
my_pixel_array[row][pix] = Color("BLACK")
del my_pixel_array # <-- THIS releases our image back to us!
screen.fill(Color("CornflowerBlue")) # Start with a fresh screen.
for c in range(-100,screen_rec.height+100,2): # Let's move image diagonally
pygame.draw.rect(screen,Color("CornflowerBlue"),image_rec,0) # Erase previous image.
image_rec.topleft = (c,c) # Use rectangle to move the image.
screen.blit(image,image_rec) # Draw image in new location.
pygame.display.update() # Show image in new location.
pygame.quit()
print("\n\n Finished.\n\n Here is the final tally:\n")
# Now show the results of our earlier pixel count and exit program.
for key,value in color_log.items():
print(" Color: "+str(key).ljust(20," "),"Number of Pixels with that color:",value)
input("\n Press [Enter] to Quit: ")
print("\n\n ",end="")
exit()
Последний сюрприз, о котором вы должны знать. Иногда, если у вас есть какая-то ошибка кодирования в блоке, использующем массив пикселей, блок может прерваться, если исходное изображение все еще заблокировано. Затем, в следующий раз, когда вы попытаетесь отобразить или использовать свое изображение, вы получите сообщение об ошибке. что-то вроде «Вы не можете отобразить заблокированную поверхность». Go вернитесь и проверьте свой код, работающий с массивом пикселей. Вероятно, это и произошло.
Надеюсь, вы найдете мой пример интересным. Удачного кодирования !!
-Science_1