سؤال

إذا قمت بفتح الصورة مع open("image.jpg"), كيف يمكنني الحصول على قيم RGB للبكسل على افتراض أن لدي إحداثيات البكسل؟

ثم كيف يمكنني أن أفعل عكس ذلك؟البدء برسم فارغ، هل تريد "كتابة" بكسل بقيمة RGB معينة؟

أفضّل ألا أضطر إلى تنزيل أي مكتبات إضافية.

هل كانت مفيدة؟

المحلول

ربما يكون من الأفضل استخدام مكتبة صور بايثون للقيام بذلك والذي أخشى أنه تنزيل منفصل.

أسهل طريقة للقيام بما تريد هي عبر طريقة التحميل () على كائن الصورة الذي يُرجع كائن وصول إلى البكسل والذي يمكنك معالجته مثل المصفوفة:

from PIL import Image

im = Image.open('dead_parrot.jpg') # Can be many different formats.
pix = im.load()
print im.size  # Get the width and hight of the image for iterating over
print pix[x,y]  # Get the RGBA Value of the a pixel of an image
pix[x,y] = value  # Set the RGBA Value of the image (tuple)
im.save('alive_parrot.png')  # Save the modified pixels as .png

بدلا من ذلك، انظر رسم الصورة مما يوفر واجهة برمجة تطبيقات أكثر ثراءً لإنشاء الصور.

نصائح أخرى

PyPNG - وحدة فك ترميز/تشفير PNG خفيفة الوزن

على الرغم من أن السؤال يشير إلى JPG، إلا أنني آمل أن تكون إجابتي مفيدة لبعض الأشخاص.

فيما يلي كيفية قراءة وكتابة وحدات بكسل PNG باستخدام وحدة بي بي إن جي:

import png, array

point = (2, 10) # coordinates of pixel to be painted red

reader = png.Reader(filename='image.png')
w, h, pixels, metadata = reader.read_flat()
pixel_byte_width = 4 if metadata['alpha'] else 3
pixel_position = point[0] + point[1] * w
new_pixel_value = (255, 0, 0, 0) if metadata['alpha'] else (255, 0, 0)
pixels[
  pixel_position * pixel_byte_width :
  (pixel_position + 1) * pixel_byte_width] = array.array('B', new_pixel_value)

output = open('image-with-red-dot.png', 'wb')
writer = png.Writer(w, h, **metadata)
writer.write_array(output, pixels)
output.close()

PyPNG عبارة عن وحدة Python نقية واحدة يبلغ طولها أقل من 4000 سطر، بما في ذلك الاختبارات والتعليقات.

بيل هي مكتبة تصوير أكثر شمولاً، ولكنها أيضًا أثقل بكثير.

استخدام وسادة (الذي يعمل مع Python 3.X وكذلك Python 2.7+)، يمكنك القيام بما يلي:

from PIL import Image
im = Image.open('image.jpg', 'r')
width, height = im.size
pixel_values = list(im.getdata())

الآن لديك جميع قيم البكسل.إذا كان RGB أو يمكن قراءة وضع آخر من خلاله im.mode.ثم يمكنك الحصول على بكسل (x, y) بواسطة:

pixel_values[width*y+x]

بدلًا من ذلك، يمكنك استخدام Numpy وإعادة تشكيل المصفوفة:

>>> pixel_values = numpy.array(pixel_values).reshape((width, height, 3))
>>> x, y = 0, 1
>>> pixel_values[x][y]
[ 18  18  12]

الحل الكامل وسهل الاستخدام هو

def get_image(image_path):
    """Get a numpy array of an image so that one can access values[x][y]."""
    image = Image.open(image_path, 'r')
    width, height = image.size
    pixel_values = list(image.getdata())
    if image.mode == 'RGB':
        channels = 3
    elif image.mode == 'L':
        channels = 1
    else:
        print("Unknown mode: %s" % image.mode)
        return None
    pixel_values = numpy.array(pixel_values).reshape((width, height, channels))
    return pixel_values

كما قال ديف ويب:

فيما يلي مقتطف رمز العمل الخاص بي يطبع ألوان البكسل من صورة:

import os, sys
import Image

im = Image.open("image.jpg")
x = 3
y = 4

pix = im.load()
print pix[x,y]
photo = Image.open('IN.jpg') #your image
photo = photo.convert('RGB')

width = photo.size[0] #define W and H
height = photo.size[1]

for y in range(0, height): #each pixel has coordinates
    row = ""
    for x in range(0, width):

        RGB = photo.getpixel((x,y))
        R,G,B = RGB  #now you can use the RGB value

هناك مقالة جيدة حقًا على wiki.wxpython.org بعنوان العمل مع الصور.تشير المقالة إلى إمكانية استخدام wxWidgets (wxImage) أو PIL أو PythonMagick.شخصيًا، استخدمت PIL وwxWidgets وكلاهما يجعل معالجة الصور سهلة إلى حد ما.

يمكنك استخدام pygameوحدة Surfarray الخاصة بـ.تحتوي هذه الوحدة على طريقة إرجاع مصفوفة بكسل ثلاثية الأبعاد تسمى Pixels3d (السطح).لقد أظهرت الاستخدام أدناه:

from pygame import surfarray, image, display
import pygame
import numpy #important to import

pygame.init()
image = image.load("myimagefile.jpg") #surface to render
resolution = (image.get_width(),image.get_height())
screen = display.set_mode(resolution) #create space for display
screen.blit(image, (0,0)) #superpose image on screen
display.flip()
surfarray.use_arraytype("numpy") #important!
screenpix = surfarray.pixels3d(image) #pixels in 3d array:
#[x][y][rgb]
for y in range(resolution[1]):
    for x in range(resolution[0]):
        for color in range(3):
            screenpix[x][y][color] += 128
            #reverting colors
screen.blit(surfarray.make_surface(screenpix), (0,0)) #superpose on screen
display.flip() #update display
while 1:
    print finished

آمل أن تكون مفيدة.الكلمة الأخيرة:تم قفل الشاشة طوال عمر Screenpix.

يعد التلاعب بالصور موضوعًا معقدًا، ومن الأفضل أن تفعل ذلك يفعل استخدم مكتبة.يمكنني أن أوصي gdmodule والذي يوفر سهولة الوصول إلى العديد من تنسيقات الصور المختلفة من داخل Python.

قم بتثبيت PIL باستخدام الأمر "sudo apt-get install python-imaging" وقم بتشغيل البرنامج التالي.سيتم طباعة قيم RGB للصورة.إذا كانت الصورة كبيرة، فأعد توجيه الإخراج إلى ملف باستخدام ">" ثم افتح الملف لاحقًا لرؤية قيم RGB

import PIL
import Image
FILENAME='fn.gif' #image can be in gif jpeg or png format 
im=Image.open(FILENAME).convert('RGB')
pix=im.load()
w=im.size[0]
h=im.size[1]
for i in range(w):
  for j in range(h):
    print pix[i,j]

يمكنك استخدام وحدة Tkinter، وهي واجهة Python القياسية لمجموعة أدوات Tk GUI ولا تحتاج إلى تنزيل إضافي.يرى https://docs.python.org/2/library/tkinter.html.

(بالنسبة لـ Python 3، تمت إعادة تسمية Tkinter إلى tkinter)

إليك كيفية تعيين قيم RGB:

#from http://tkinter.unpythonic.net/wiki/PhotoImage
from Tkinter import *

root = Tk()

def pixel(image, pos, color):
    """Place pixel at pos=(x,y) on image, with color=(r,g,b)."""
    r,g,b = color
    x,y = pos
    image.put("#%02x%02x%02x" % (r,g,b), (y, x))

photo = PhotoImage(width=32, height=32)

pixel(photo, (16,16), (255,0,0))  # One lone pixel in the middle...

label = Label(root, image=photo)
label.grid()
root.mainloop()

واحصل على RGB:

#from http://www.kosbie.net/cmu/spring-14/15-112/handouts/steganographyEncoder.py
def getRGB(image, x, y):
    value = image.get(x, y)
    return tuple(map(int, value.split(" ")))
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img=mpimg.imread('Cricket_ACT_official_logo.png')
imgplot = plt.imshow(img)
from PIL import Image
def rgb_of_pixel(img_path, x, y):
    im = Image.open(img_path).convert('RGB')
    r, g, b = im.getpixel((x, y))
    a = (r, g, b)
    return a

إذا كنت تتطلع إلى الحصول على ثلاثة أرقام في شكل رمز لون RGB، فيجب أن يقوم الكود التالي بذلك.

i = Image.open(path)
pixels = i.load() # this is not a list, nor is it list()'able
width, height = i.size

all_pixels = []
for x in range(width):
    for y in range(height):
        cpixel = pixels[x, y]
        all_pixels.append(cpixel)

هذا قد يعمل من أجلك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top