0

Hello I'm trying to do school project using Python and I'm getting stuck so I would like some help. I'm trying to make a import a image and read it and export a text file in hexademicals.

For example with this image I made with MS Paintenter image description here

Let's pretend this is a 9 pixel image. I would like to import this image and turn it into a text file that reads somethings like

FF0000 0000FF 00FF00
0000FF FF0000 00FF00
00FF00 FF0000 0000FF

Would this be possible using Python? Or do I need another programming language to do it? Thanks in advance guys!(Sorry If i have bad English)

John Won
  • 55
  • 1
  • 8
  • *"Would this be possible using Python?"* - Yes – klutt Mar 28 '18 at 08:43
  • https://stackoverflow.com/questions/138250/how-can-i-read-the-rgb-value-of-a-given-pixel-in-python – klutt Mar 28 '18 at 08:44
  • This post help to convert image to hex format link(https://stackoverflow.com/a/45544814/5855946) – Ganesh Patel Mar 28 '18 at 08:47
  • Possible duplicate of [Convert image into hexadecimal format with Python](https://stackoverflow.com/questions/45544755/convert-image-into-hexadecimal-format-with-python) – klutt Mar 28 '18 at 08:50

3 Answers3

2

You can do it at the command-line without any need for Python, using ImageMagick which is installed on most Linux distros and is available for macOS and Windows.

It will work for PNG, JPEG and around 200 other image types. Let's make a sample image:

convert xc:red  xc:blue xc:lime +append    \
     \( xc:blue xc:red  xc:lime +append \) \
     \( xc:lime xc:red  xc:blue +append \) \
     -append start.png

And let's enlarge it so you can see it (I could have done it one go):

convert start.png -scale 400x start.png

enter image description here

Now to your question, I can convert that to raw RGB (by interpreting all the image sizes, palettes, compression, and headers) and pass that through xxd for hex formatting:

convert start.png -depth 8 rgb: | xxd -g3 -c9 -u
00000000: FF0000 0000FF 00FF00  .........
00000009: 0000FF FF0000 00FF00  .........
00000012: 00FF00 FF0000 0000FF  .........

If you want to strip the address off the front and the dots off the end, you can use:

convert start.png -depth 8 rgb: | xxd -g3 -c9 -u | sed 's/  .*//; s/^.*: //'
FF0000 0000FF 00FF00
0000FF FF0000 00FF00
00FF00 FF0000 0000FF

The sed command says... "Anywhere you see two spaces, remove them and all that follows. Remove anything up to and including a colon followed by a space.".

Or maybe plain dump is better:

convert start.png -depth 8 rgb: | xxd -g3 -c9 -u -p
FF00000000FF00FF00
0000FFFF000000FF00
00FF00FF00000000FF

Note: If using ImageMagick v7+, replace convert with magick in all examples above.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
2

I don't really speak Python, but I have tried to learn some and this seems to do what you ask. I may not be very Pythonic, but at least it only uses PIL, which is a lot smaller and easier to install than OpenCV!

#!/usr/local/bin/python3
import sys
from PIL import Image

# Check image supplied as argument
if len(sys.argv)!=2:
    sys.exit("ERROR: Please specify a file")

# Load image
img=Image.open(sys.argv[1])

# Retrieve dimensions
w,h=img.size

# Make RGB if palettised
if img.mode=="P":
    img=img.convert('RGB')

p=img.load()
for y in range(0,h):
    for x in range(0,w):
        r,g,b=p[x,y]
        str="%02x%02x%02x "%(r,g,b)
        print(str,end='')
    print()
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
1

opencv can read image simply.

import cv2

img = cv2.imread('image.png')
print(img.shape)

cy=img.shape[0]
cx=img.shape[1]
f = open('img.txt', 'w')
for row in img:
    for col in row:
        str="%02x%02x%02x"%(col[0], col[1], col[2])
        f.write(str)
f.close()
Junhee Shin
  • 748
  • 6
  • 8