A function is written in Python that converts a picture into HEX.

def img_to_hex(): string = '' image = '' with open(image, 'rb') as f: binValue = f.read(1) while len(binValue) != 0: hexVal = hex(ord(binValue)) string += ', ' + hexVal binValue = f.read(1) return string 

Unfortunately, I receive in HEX, color palett, image bytes and information about it (size, etc.).

Help disassemble it into 3 different "layers" - collor palett, bytes of maps and information.

  • 1) How have you tried to solve the problem, what did not work out? 2) In what format is the picture? 3) Why read it yourself. and not with the help of any library? - Arnial
  • @Arnial 1. I tried not much, because there is simply no idea. 2. BMP 3. Tried to read the PIL documentation, but did not understand how to use it for this. - Insider

1 answer 1

Used Pillow v3.1.1 (fork PIL)

 from PIL import Image img = Image.open( "test_image.bmp", 'r' ) # создаст ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ ΠΈΠ· Ρ„Π°ΠΉΠ»Π° img.load() # Ρ„ΠΎΡ€ΡΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Ρ‡Ρ‚Π΅Π½ΠΈΠ΅ Ρ„Π°ΠΉΠ»Π° print( img.format, img.size, img.mode ) # Π΄Π°Π½Π½Ρ‹Π΅ ΠΏΠΎ Ρ„Π°ΠΉΠ»Ρƒ if img.mode != 'RGB': # bmp ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΠΎ Ρ€Π°Π·Π½ΠΎΠΌΡƒ ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Ρ†Π²Π΅Ρ‚Π° # Ссли ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ΡΡ Ρ€Π΅ΠΆΠΈΠΌ # ΠΎΡ‚Π»ΠΈΡ‡Π½Ρ‹ΠΉ ΠΎΡ‚ RGB, Ρ‚ΠΎ Π² Π΄Π°Π½Π½Ρ‹Ρ… Π±ΡƒΠ΄ΡƒΡ‚ # Π»ΠΈΠ±ΠΎ индСкс ΠΏΠ°Π»ΠΈΡ‚Ρ€Ρ‹ (mode=p) # Π»ΠΈΠ±ΠΎ ΠΎΡ‚Ρ‚Π΅Π½ΠΎΠΊ сСрого (ΠΎΠ΄ΠΈΠ½ Π±Π°ΠΉΡ‚ Π½Π° пиксСль mode=L) # Π»ΠΈΠ±ΠΎ Π΅Ρ‰Ρ‘ Ρ‡Ρ‚ΠΎ Ρ‚ΠΎ (mode=1 Ρƒ мСня Π½Π΅Ρ‚ Ρ‚Π°ΠΊΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ) img = img.convert( 'RGB' ) # сконвСртируСт эти Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Ρ‹ Π² 'RGB' pixels = img.getdata() # ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ пиксСли (Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ класс `ImagingCore`) pix3 = list( pixels )[0:3] # ΠΏΠ΅Ρ€Π²Ρ‹Π΅ 3 пиксСля print([ hex( (r << 16) + (g << 8) + b ) for r,g,b in pix3 ]) #hex ΠΏΠ΅Ρ€Π²Ρ‹Ρ… пиксСлСй 

UPDATE:

  def print_hex( path ): img = Image.open( path, 'r' ) img.load() # ΠΏΠ°Π»ΠΈΡ‚Ρ€Ρƒ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ Ρ‚Π°ΠΊ (Π½Π΅ всС ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ ΠΈΠΌΠ΅ΡŽΡ‚ ΠΏΠ°Π»ΠΈΡ‚Ρ€Ρƒ) palette = img.getpalette() # Π²Π΅Ρ€Π½Ρ‘Ρ‚ массив Π² Π²ΠΈΠ΄Π΅ [r,g,b,r,g,b...] ΠΈΠ»ΠΈ None hex_palette = [] if palette: hex_palette = [ hex( color_component ) for color_component in palette ] if img.mode == 'P': # изобраТСния ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡŽΡ‚ ΠΏΠ°Π»ΠΈΡ‚Ρ€Ρƒ # хранят Ρ†Π²Π΅Ρ‚Π° Π² Π²ΠΈΠ΄Π΅ индСксов ΠΏΠ°Π»ΠΈΡ‚Ρ€Ρ‹ # Π² Ρ‚Π°ΠΊΠΎΠΌ случаС hex пиксСлСй ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ Ρ‚Π°ΠΊ. pixels = list( img.getdata() ) hex_palette_pixels = [ hex( pix ) for pix in pixels ] if img.mode != 'RGB': # Ссли Π½ΡƒΠΆΠ½ΠΎ ΠΏΠΎΠ»ΡƒΡ‡Π°Ρ‚ΡŒ Ρ†Π²Π΅Ρ‚ пиксСля Π² rgb Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (ΠΊΠ°ΠΊ ΠΎΠ½ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π³Π»ΡΠ΄Π΅Ρ‚ΡŒ Π½Π° экранС) # Ρ‚ΠΎ ΠΌΠΎΠΆΠ½ΠΎ ΡΠΊΠΎΠ½Π²Π΅Ρ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ ΠΈΠ· ΠΏΠ°Π»ΠΈΡ‚Ρ€ΠΎΠ²ΠΎΠΉ Π² RGB img = img.convert( 'RGB' ) pixels = list( img.getdata() ) hex_rgb_pixels = [ [ hex( r ), hex( g ), hex( b ) ] for r,g,b in pixels ] hex_rgb_pixels_merged = [ color_component for color in hex_rgb_pixels for color_component in color ] print( hex_palette_pixels[0:100], hex_rgb_pixels[0: 100], hex_palette[0:100] ) 
  • I will try tomorrow and see if I can come to the result - Insider
  • when executing the last command - hex of the first pixels, the python gives an error. 'int' object is not iterable - Insider
  • Ie list( pixels )[0:3] gave you an int ? I do not predict how this is possible. Can you add the python version number, pil and the current code for getting the hex into the question? - Arnial
  • one
    hex_rgb_pixels[0:100] - 100 pixels in rgb format, just remove the slice for everyone: hex_rgb_pixels - Arnial
  • one
    Bmp has several ways to represent pixels . Because Your file has a palette, then each pixel is encoded with one byte, the byte contains the index color in the palette. hex_palette_pixels are the hexes of the color numbers of the pixels (how the pixels are represented in the file), hex_rgb_pixels are the hexes of the colors of the pixels (real colors of pixels). - Arnial