Home > database >  Convert Image to array and array to image using python, does the array contain metadata or other inf
Convert Image to array and array to image using python, does the array contain metadata or other inf

Time:02-05

Sorry for my english but it's not my first language.

I would like to create a program that:

  • Transform a jpeg or png image into an array (very important: I would like an array composed only of the values that the pixels of the image have and not metadata or other information. Where I can select each specific pixel of the image).
  • Save this array in a txt file.
  • Transform this array composed of only the pixel values of the image back into jpg or png image and save it in a file.

Requests:

  • Is the array I created with the program I wrote composed only of the pixel values of the image? is there also metadata or other information?
  • Is this a valid way to remove metadata from an image?
  • Is this a valid way to create the array representing that image pixel by pixel?
  • Is this a valid way to convert png images to jpeg or jpeg to png?

Thank you!

This is the program I created, any opinion?

import numpy as np
from PIL import Image
import sys

img_data = Image.open("imagea.jpeg")
img_arr = np.array(img_data)
np.set_printoptions(threshold=sys.maxsize)

print(img_arr.shape)

new_img = Image.fromarray(img_arr)
new_img.save("imageb.jpeg")

print("Image saved!")

file = open("file1.txt", "w ")
content = str(img_arr)
file.write(content)
file.close()

print("Finished!")

CodePudding user response:

Loading an image and converting it to a Numpy array is a perfectly legitimate way of discarding all metadata including:

  • EXIF data, copyright data,
  • IPTC and XMP data,
  • ICC colour profile data

You can tell it's all gone by thinking about the Numpy array you hold and its dimensions and data type.


Note that you need to be careful with PNG palette images and images with an alpha channel.


Note that you can achieve this more simply on the command-line with ImageMagick using:

magick mogrify -strip IMAGE.JPG

Or with exiftool.


Note that you can achieve this by using a format that doesn't support metadata, such as NetPBM, with extension .ppm e.g.:

magick INPUT.JPG -strip -compress none RESULT.PPM    # gives P3/plain ASCII file
magick INPUT.JPG -strip RESULT.PPM  # gives P6/binary file

You can also read/write PPM files with PIL.

  • Related