d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > What's The Best Way To Approach Converting Pics > Into An Image Data Class Or Matrix?
Add Reply New Topic New Poll
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
Mar 26 2016 07:04pm
I was doing some googling about how images are stored, and it turns out that it's pretty over my head.

From what I understand, if you have, say, a 100 px by 100 px image, it uses 10,000 pixels, each pixel with its own R, G, and B values from 0 to 255. But then there's 8 bit, 16 bit, sRGB, and it gets complicated from there.

Then there's also grayscale:

https://en.wikipedia.org/wiki/Grayscale

Ok, so here's my ultimate goal. I want to take some picture, let's say its 1,000 px by 1,000 px, and convert it into a 30 px by 30 px, grayscale picture. Then, I want to store that new picture as some kind of data type in Python. Ideally, it'd just by some kind of 2D array full of numbers from 0 to n, where 0 means completely black and n means completely white.

Is anyone familiar with this kind of thing? Am I on the right track, or am I going about this in a convoluted way?

EDIT: I was thinking something like the image in the bottom left of this video:

https://youtu.be/ilP4aPDTBPE?t=72

This post was edited by Rejection on Mar 26 2016 07:14pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 26 2016 09:13pm
i think reading an image into a 2D array is as simple as this:

Code
img = Image.open('mypic.jpg')
arr = img.load()


to resize an image:

Code
img.resize((w,h), PIL.Image.ANTIALIAS)


there are also lossy compressions you can use if you prefer that instead of resizing. you can make files smaller by losing more quality. iirc i did SVD (Singular value decomposition) in college. just thought i'd mention it.

This post was edited by carteblanche on Mar 26 2016 09:42pm
Member
Posts: 5,027
Joined: Oct 1 2007
Gold: 2,348.00
Apr 2 2016 02:09am
Do you need to transform the pictures at runtime?

If not you could just do it by using photoshop.
With patch-processing you could perform the same action on all the pics you want to transform.

If you need more "control" over the whole process you could use matlab (or R. Think there are packages for all the actions you need).

In matlab there is the rgb2gray function that uses all 3 collor-matrices to compute a grayscale picture (im pretty sure there is something similar for R).
For the resizing operation you got different approaches, but i think resizing a 1000 x 1000 pic into a 30 x 30 pic wont be very satisfying in many cases because you loose a lot of information.
Lets take the first row of a 1000 x 1000 pixel pic. There can be 1000-1 changes in the pic (black pixe, white pixel, black pixel, white pixel....).
In your 30 pix image you can display 30-1 changes.
In other words: you will loose all the high-frequencies from the original pic which means you loose a lot of details.
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
Apr 16 2016 05:59pm
Quote (Wirbelschwein @ Apr 2 2016 02:09am)
Do you need to transform the pictures at runtime?

If not you could just do it by using photoshop.
With patch-processing you could perform the same action on all the pics you want to transform.

If you need more "control" over the whole process you could use matlab (or R. Think there are packages for all the actions you need).

In matlab there is the rgb2gray function that uses all 3 collor-matrices to compute a grayscale picture (im pretty sure there is something similar for R).
For the resizing operation you got different approaches, but i think resizing a 1000 x 1000 pic into a 30 x 30 pic wont be very satisfying in many cases because you loose a lot of information.
Lets take the first row of a 1000 x 1000 pixel pic. There can be 1000-1 changes in the pic (black pixe, white pixel, black pixel, white pixel....).
In your 30 pix image you can display 30-1 changes.
In other words: you will loose all the high-frequencies from the original pic which means you loose a lot of details.


That's completely fine, I'm ok with the detail loss. I haven't googled it yet, but I really hope there's a rgb2gray function in python as well (maybe in some library). I'm sure there is, I just need to look.

Quote (carteblanche @ Mar 26 2016 09:13pm)
i think reading an image into a 2D array is as simple as this:

Code
img = Image.open('mypic.jpg')
arr = img.load()


to resize an image:

Code
img.resize((w,h), PIL.Image.ANTIALIAS)


there are also lossy compressions you can use if you prefer that instead of resizing. you can make files smaller by losing more quality. iirc i did SVD (Singular value decomposition) in college. just thought i'd mention it.


Yeah, that actually works. I'm specifically looking to resize it though, since I plan on taking each pixel's grayscale value individually, and I want a consistent amount of pixels.
Member
Posts: 3,028
Joined: Mar 23 2016
Gold: 7,568.50
Apr 16 2016 06:53pm
There are many different algorithms to convert to grayscale. The simplest is just to average the RGB values of each pixel. So say the first pixel has RGB values of 45;244;171, its new RGB value would be (45+244+171)/3 = 153;153;153.

For me the interesting thing is, as far as image quality goes, would it be better to convert to grayscale first then reduce size or reduce size then convert to grayscale?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll