[math-fun] Changing photos into matrices
Does anyone know a way to turn black and white jpeg images into rectangular arrays of pixel-by-pixel grayscale levels? My Mac has Adobe Photoshop CS6 and Mathematica, but I can't figure out a way to get either of them to do it. The reason I want to do this has to do with this year's Gathering for Gardner, so I believe it (just barely) qualifies as a math-fun topic. Jim Propp
Jim, This might be overkill, but I recommend ImageMagick: http://www.imagemagick.org which is a free suite of tools for manipulating images. You'd want to convert to to gray format as in the command line convert yourimage.jpg yourimage.gray On Tue, Feb 25, 2014 at 2:54 PM, James Propp <jamespropp@gmail.com> wrote:
Does anyone know a way to turn black and white jpeg images into rectangular arrays of pixel-by-pixel grayscale levels?
My Mac has Adobe Photoshop CS6 and Mathematica, but I can't figure out a way to get either of them to do it.
The reason I want to do this has to do with this year's Gathering for Gardner, so I believe it (just barely) qualifies as a math-fun topic.
Jim Propp _______________________________________________ math-fun mailing list math-fun@mailman.xmission.com http://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun
Save as .bmp with no compression....then apart from the bmp header the file will be in straightforward rectangular pixel format. On 25 Feb 2014, at 19:54, James Propp wrote:
Does anyone know a way to turn black and white jpeg images into rectangular arrays of pixel-by-pixel grayscale levels?
My Mac has Adobe Photoshop CS6 and Mathematica, but I can't figure out a way to get either of them to do it.
The reason I want to do this has to do with this year's Gathering for Gardner, so I believe it (just barely) qualifies as a math-fun topic.
Jim Propp _______________________________________________ math-fun mailing list math-fun@mailman.xmission.com http://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun
The meaning and purpose of life is to give life purpose and meaning. The instigation of violence indicates a lack of spirituality.
Also I'm not sure about Photoshop but Paint shop Pro used to allow you to save in "raw" format - this just let you specify the image format (resolution etc) and would save it out from the source based on the output format you specify as a ".raw" file of straight rectangular pixels i.e. no header etc. so to reproduce you'd have to remember the source format (res and pixel format) yourself. On 25 Feb 2014, at 19:54, James Propp wrote:
Does anyone know a way to turn black and white jpeg images into rectangular arrays of pixel-by-pixel grayscale levels?
My Mac has Adobe Photoshop CS6 and Mathematica, but I can't figure out a way to get either of them to do it.
The reason I want to do this has to do with this year's Gathering for Gardner, so I believe it (just barely) qualifies as a math-fun topic.
Jim Propp _______________________________________________ math-fun mailing list math-fun@mailman.xmission.com http://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun
The meaning and purpose of life is to give life purpose and meaning. The instigation of violence indicates a lack of spirituality.
I was able to do what I wanted quite simply in the end with the following Mathematica commands: SetDirectory[<directory containing Photo.jpg>] photo = Import["Photo.jpg"] {n, m} = ImageDimensions[photo] Photo = Table[ Table[1 - ImageData[photo][[i]][[j]][[1]], {j, 1, n}], {i, 1, m}]; (The "1 -" is in there because ArrayPlot[...] uses a reverse convention where 0 means black and 1 means white.) Thanks to all who replied, especially George Hart. Jim On Tue, Feb 25, 2014 at 4:28 PM, David Makin <makinmagic@tiscali.co.uk>wrote:
Also I'm not sure about Photoshop but Paint shop Pro used to allow you to save in "raw" format - this just let you specify the image format (resolution etc) and would save it out from the source based on the output format you specify as a ".raw" file of straight rectangular pixels i.e. no header etc. so to reproduce you'd have to remember the source format (res and pixel format) yourself.
On 25 Feb 2014, at 19:54, James Propp wrote:
Does anyone know a way to turn black and white jpeg images into rectangular arrays of pixel-by-pixel grayscale levels?
My Mac has Adobe Photoshop CS6 and Mathematica, but I can't figure out a way to get either of them to do it.
The reason I want to do this has to do with this year's Gathering for Gardner, so I believe it (just barely) qualifies as a math-fun topic.
Jim Propp _______________________________________________ math-fun mailing list math-fun@mailman.xmission.com http://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun
The meaning and purpose of life is to give life purpose and meaning. The instigation of violence indicates a lack of spirituality.
_______________________________________________ math-fun mailing list math-fun@mailman.xmission.com http://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun
Convert to the pgm format (the P2 version, not the P5 one; see "man pgm"). Then you have the pixels in a format that is trivial to work with. Example from the man page: ------------------ start file feep.pgm ------------- P2 # feep.pgm 24 7 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0 0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0 0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ------------------ end file feep.pgm ------------- You should possibly use max value 255 (not 15 as in the example) with your project. This nice format also makes it utterly trivial to create images using a few lines of about any computer language in existence. Helpful example: http://jjj.de/pari/write-pgm.gpi A warning: when starting with the jpeg format you get spurious results when doing anything in the frequency domain (unless the jpeg did not compress at all). Best, jj * James Propp <jamespropp@gmail.com> [Feb 26. 2014 08:29]:
Does anyone know a way to turn black and white jpeg images into rectangular arrays of pixel-by-pixel grayscale levels?
My Mac has Adobe Photoshop CS6 and Mathematica, but I can't figure out a way to get either of them to do it.
The reason I want to do this has to do with this year's Gathering for Gardner, so I believe it (just barely) qualifies as a math-fun topic.
Jim Propp _______________________________________________ math-fun mailing list math-fun@mailman.xmission.com http://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun
participants (4)
-
David Makin -
James Propp -
Joerg Arndt -
Victor Miller