Adafruit MagTag: Dither a jpg from placekitten.com and display on MagTag - adafruit-circuitpython

Pimoroni has an example that grabs a jpg online and displays it on one of their Inky e-ink displays.
I am wondering if it is possible to do the same thing with an Adafruit MagTag using CircuitPython. In the latest version of CircuitPython, there are BitmapTools that seem like they may be able to convert a .jpg file into a .bmp that is dithered appropriately for an e-ink display. There just doesn't seem to be any example code that shows how to do this.
Is it possible?
Any examples of using bitmaptools.dither(dest_bitmap: displayio.Bitmap, source_bitmapp: displayio.Bitmap, source_colorspace: displayio.Colorspace, algorithm: DitherAlgorithm = DitherAlgorithm.Atkinson)→ None
The code would look a little like this ...
import board
import displayio
import adafruit_imageload
display = board.DISPLAY
bitmap, palette = adafruit_imageload.load("/purple.bmp",
bitmap=displayio.Bitmap,
palette=displayio.Palette)
# Create a TileGrid to hold the bitmap
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
# Create a Group to hold the TileGrid
group = displayio.Group()
# Add the TileGrid to the Group
group.append(tile_grid)
# Add the Group to the Display
display.show(group)
# Loop forever so you can enjoy your image
while True:
pass
... but we would pull in a .jpg and have to do a little work on it so that the greyscale image becomes a dithered .bmp.
Any ideas?
I can pull in a .jpg. I just can't process it and display.
I was also looking for an API or something that might be able to pre-process the image from https://placekitten.com so that it would already be a dithered bitmap file. Does that exist?

Related

How do I view an image in dotnet interactive?

I would like to view an image interactively in F# jupyter notebook similarly to how I can do this in python:
from PIL import Image
Image.open("image.png")
and that shows an image.
Is there a straightforward way to do this?
EmguCV, OpenCVSharp and ImageSharp all don't work together with the plotting libraries like Plotly.NET or XPlot to provide this functionality so I can't get something like matplotlib's pyplot.imshow.
In a notebook, you can display an image like this:
#!fsharp
// Load iamge
let data = File.ReadAllBytes("E:\\Downloads\\myImage.png");
// Convert so we can display it as HTML
let b64 = Convert.ToBase64String(data);
HTML($"<img src=\"data:image/png;base64,{b64}\"></img>") // last call without ; gets displayed
// Alt, this one has a semicolon:
// display(HTML($"<img src=\"data:image/png;base64,{b64}\"></img>"));

How do I convert a Color Image to Black and White using ImageMagick?

Background: I performed an Indexed conversion in Gimp of a color image and the result was a nice B&W version of the source Image.
I have tried numerous options in ImageMagick to no avail. I can get close but never quite as clear and crisp as what gimp seems to do effortlessly.
Here is my source:
var bmp = new MagickImage(sourceImage);
bmp.Threshold(new ImageMagick.Percentage(60));
bmp.Resample(200, 200);
bmp.ColorType = ColorType.Bilevel;
bmp.BitDepth(1);
bmp.Settings.Compression = CompressionMethod.Group4;
bmp.Strip();
bmp.Format = MagickFormat.Tiff;
I have been adjusting the Threshold call and have tried various suggestions I have seen online with varying amounts of success.
magickimage has a feature called -monochrome but I have not found how that is achieved in the .net library.
I am sure this is possible but what is the best way to achieve a nice B&W conversion.
With ImageMagick 7, you can do Otsu thresholding.
Input:
magick check.png -alpha off -auto-threshold otsu x.png
Result:
There is no built-in equivalent in ImageMagick 6. However I have a script, otsuthresh that will do that At my web site
So in ImageMagick 6, you just have to do simple thresholding.
convert check.png -alpha off -threshold 50% y.png
Result:
I note that your input image has an opaque alpha channel which needs to be removed to get proper results.
This is possibly the simplest way you can use Greyscale in Magick.NET.
MagickImage image = new MagickImage(imagePath);
image.Grayscale();
string fileName = image.FileName + "_grey.png";
image.Write(fileName);
Instead of image.FileName you can also directly use the image path if you have it.
Optionally you can add a PixelIntensityMethod in Greyscale for eventually better results.
Don't forget to call image.RePage() when you want to crop the image.
Otsu's method will perform better when dealing with texts over a background with slightly gradient colors:
How do I convert a Color Image to Black and White using ImageMagick?
According to #fmw42 's comparison on multiple threshold methods:
the Local Adaptive(its equivalent in imagemagick is -lat) algorithm might work best on texts over background:
Also try to combine with connected components processing that was introduced in another answer made by #fmw42 to remove unnecessary edges/dots which might get confused by any further OCR process.

reading images in r program

I want to read 12 images at a time in R.
I don't know how to do it. I am complete new to working on images in R.
How can I read couple of images from a folder in my system?
I am using windows10 operating system. RAM 8 gb. CORE i5 processor.
GPU is Intel(R) HD Graphics 620.
I am able to read only single image in R and that image is displaying as numeric values. I tried to convert it into raster format and then tried to print image to view the image. But I am still finding the color codes in values but not the image in print.
Can anyone help me on this?
Thanks a lot.
install.packages("magick")
library(magick)
install.packages("rsvg")
install.packages("jpeg")
library(jpeg)
img <- readJPEG("C:/Users/folder/Abc.jpg", native = FALSE)
img1 <- as.raster(img, interpolate = F)
print(img1)
I want to read couple of images at a time into R console and want to view or print images.
The suggested duplicate gives you the basics for how to read in a number of files at once, but there are a few potential gotchas, and it won't help you with displaying the images.
This first bit is purely to set up the example
library(jpeg)
library(grid)
# Create a new directory and move to it
tdir <- "jpgtest"
dir.create(tdir)
setwd(tdir)
# Copy the package:jpeg test image twice, once as .jpg and once as .jpeg
# to the present working directory
file.copy(system.file("img", "Rlogo.jpg", package="jpeg"),
to=c(file.path(getwd(), "test.jpg"), file.path(getwd(), "test.jpeg")))
Then we can list the files, either using a regex match, or choose them interactively, then read and store the images in a list.
# Matches any file ending in .jpg or .jpeg
(flist <- list.files(pattern="*\\.jp[e]?g$"))
# Interactive selection
flist <- file.choose()
jpglist <- lapply(flist, readJPEG)
To display the images I tend to use grid, but there are a number of alternatives.
grid.raster(jpglist[[1]], interpolate=FALSE)
Remove temporary directory
setwd("..")
unlink(tdir)

Writing Comments (metadata) within image file using R

I would like to add parameter information to the plots that we make as part of our research project. I know that png has a text block for adding comments. (I am not a png expert, but I think it uses the tEXt block.) Using Gimp, under image properties, you can add a comment that can be read by other software (e.g. by the "inspector" in mac preview. I'm sure there is a windows equivalent). We make our images with ggsave. I did not see any arguments to ggsave that allows us to write to the "comment" block of a png file. I've tried using png::readPNG, and png::writePNG functions
im <- readPNG("test1.png",native=FALSE,info=TRUE)
md <- c(comment=paste0(date=date(),machine=Sys.info()['nodename']))
writePNG(image=im,target="test1_md1.png",text=md,metadata = md)
im2 <- readPNG("test1_md1.png",native=FALSE,info=TRUE)
attr(img2, "info")
I can open the new png file, it looks just like the old one. However, the comment block is not filled. As you can see, the data is written in the file, but neither gimp nor preview can read it. Is there a way to write the metadata in the comment block so that preview and gimp can read it correctly?
Thank you
update: ok, png::writePNG does actually write into the tEXt block, however neither gimp nor preview read the text block. I wrote a simple java program that reads the blocks.
IHDR null javax.imageio.metadata.IIONamedNodeMap#133314b
width = 2580 height = 1800 bitDepth = 8 colorType = RGBAlpha compressionMethod = deflate filterMethod = adaptive interlaceMethod = none
tEXt null javax.imageio.metadata.IIONamedNodeMap#b1bc7ed
tEXtEntry null javax.imageio.metadata.IIONamedNodeMap#7cd84586
keyword = date value = Tue Dec 4 12:55:45 2018
tEXtEntry null javax.imageio.metadata.IIONamedNodeMap#30dae81
keyword = machine.nodename value = mycomputer.xyz.org
Is there a separate comment block somewhere? That is the block I want to write to.
update 2: It appears that gimp/preview read the iTXt block. So the question becomes can I write to the iTXt block from R.

phpexcel fit to selection

I generate an excel file using the great PHPExcel library. At this moment I need to show all the columns into a single page. You know the feeling when you have to scroll left and right up and down to see all the data. Basically I have 33 columns and I need to fit them on any display
I don't know if is my setup or where is the problem, but I can't make it work. This is the code I'm using
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToPage(true);
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToHeight(0);
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToWidth(1);
On MS Excel I select all thw colums then I choose View-> Zoom, then select Fit Selection. How can I do that programatically when excel file is generated with PHPExcel.
i hope i don't understand you wrong, here it goes:
phpExcel has an method called setZoomScalezoom(), however this does not auto fill the whole screen:
example:
$excel = new PHPExcel();
$sheet = $excel->getActiveSheet();
$sheet->getSheetView()->setZoomScale(300);
$writer = new PHPExcel_Writer_Excel5($excel);
$writer->save('test.xls');
got my information from:
source: http://typo3.org/extension-manuals/phpexcel_library/1.7.4/view/5/4/
Setting worksheet zoom level To set a worksheet’s zoom level, the
following code can be used:
$objPHPExcel->getActiveSheet()->getSheetView()->setZoomScale(75);
Note that zoom level should be in range 10 – 400.

Resources