Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6652087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:05:30+00:00 2026-05-26T01:05:30+00:00

I am interested in converting a matrix to an image (element value = pixel

  • 0

I am interested in converting a matrix to an image (element value = pixel intensity), a bit like:
R – image of a pixel matrix?

However I need the size of the image to be exactly (in pixels) the size of the matrix. So if the matrix is 234×14 so should be the generated image.

Preference for a solution using no additional R package (like in answer to question lined above), but everything would be fine as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:05:31+00:00Added an answer on May 26, 2026 at 1:05 am

    One way is to avoid the R devices completely, and rely on the GDAL drivers in rgdal.

    m <- matrix(rep(1:234, each = 14), ncol = 14, byrow = TRUE)
    l <- list(x = 1:nrow(m), y = 1:ncol(m), z = m)
    
    library(rgdal)
    x <- image2Grid(l)
    writeGDAL(x, "out.tif")
    

    There are other drivers in GDAL, but “GeoTIFF” is the default, and the defaults will preserve the values accurately (within the numeric limits of R and GDAL). This is just a single band raster, but it will work the same for multiple attributes.

    The real limit here is the target you have for the file, and whether it can read the resulting image in the way that you want. GDAL has all the options you would need, but whether the defaults are right and what you really need depends on details.

    y <- readGDAL("out.tif")
    all.equal(as.image.SpatialGridDataFrame(y)$z, m)
    [1] TRUE
    

    Here is a floating point example that shows the numeric limits more realistically:

    set.seed(1)
    
    m <- matrix(runif(234 * 14), ncol = 14)
    l <- list(x = 1:nrow(m), y = 1:ncol(m), z = m)
    
    library(rgdal)
    x <- image2Grid(l)
    writeGDAL(x, "out.tif")
    
    y <- readGDAL("out.tif")
    
    all.equal(as.image.SpatialGridDataFrame(y)$z, m)
    [1] "Mean relative difference: 1.984398e-08"
    

    Another way is the pnm format that is used by pixmap. See

    library(pixmap)
    
    ?write.pnm
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm interested in doing comparisons between the date string and the MySQL timestamp. However,
As title. I'm not interested in converting between FillOrder=2 and FillOrder=1. Rather, I have
Any tools to convert C code into Java code? I am interested in converting
I am looking for some information on pixel processing. I am interested in the
I am interested in converting the dates to null in SQL server. Here is
I would like to know a way to generate a single pixel in JavaScript
I'm interested in taking these census cartographic files and converting them into SVG files.
I'm converting a PHP app written in CodeIgniter to Rails. Due to the size
Can somebody share an experience concerning scaling vertically the ActiveMQ? I'm particularly interested how
Interested if anyone has used VSTS Database Edition extensively and, if so, which features

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.