Bitmap/Histogram: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 11: | Line 11: | ||
* Replace each pixel of luminance lesser than the median to black, and others to white. |
* Replace each pixel of luminance lesser than the median to black, and others to white. |
||
Use [[read ppm file | read]]/[[write ppm file]], and [[grayscale image]] solutions. |
Use [[read ppm file | read]]/[[write ppm file]], and [[grayscale image]] solutions. |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/lena30g.PPM lena30g.PPM] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used. |
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/lena30g.PPM lena30g.PPM] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used. |
||
{{libheader|Action! Bitmap tools}} |
{{libheader|Action! Bitmap tools}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="action!">INCLUDE "H6:LOADPPM5.ACT" |
||
DEFINE HISTSIZE="256" |
DEFINE HISTSIZE="256" |
||
Line 126: | Line 125: | ||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bitmap_Histogram.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bitmap_Histogram.png Screenshot from Atari 8-bit computer] |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Histogram of an image: |
Histogram of an image: |
||
<syntaxhighlight lang=ada>type Pixel_Count is mod 2**64; |
<syntaxhighlight lang="ada">type Pixel_Count is mod 2**64; |
||
type Histogram is array (Luminance) of Pixel_Count; |
type Histogram is array (Luminance) of Pixel_Count; |
||
Line 147: | Line 145: | ||
end Get_Histogram;</syntaxhighlight> |
end Get_Histogram;</syntaxhighlight> |
||
Median of a histogram: |
Median of a histogram: |
||
<syntaxhighlight lang=ada>function Median (H : Histogram) return Luminance is |
<syntaxhighlight lang="ada">function Median (H : Histogram) return Luminance is |
||
From : Luminance := Luminance'First; |
From : Luminance := Luminance'First; |
||
To : Luminance := Luminance'Last; |
To : Luminance := Luminance'Last; |
||
Line 165: | Line 163: | ||
end Median;</syntaxhighlight> |
end Median;</syntaxhighlight> |
||
Conversion of an image to black and white art: |
Conversion of an image to black and white art: |
||
<syntaxhighlight lang=ada> F1, F2 : File_Type; |
<syntaxhighlight lang="ada"> F1, F2 : File_Type; |
||
begin |
begin |
||
Open (F1, In_File, "city.ppm"); |
Open (F1, In_File, "city.ppm"); |
||
Line 187: | Line 185: | ||
end; |
end; |
||
Close (F2);</syntaxhighlight> |
Close (F2);</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
[[Image:greyscale_bbc.jpg|right]] |
[[Image:greyscale_bbc.jpg|right]] |
||
[[Image:histogram_bbc.gif|right]] |
[[Image:histogram_bbc.gif|right]] |
||
<syntaxhighlight lang=bbcbasic> INSTALL @lib$+"SORTLIB" |
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SORTLIB" |
||
Sort% = FN_sortinit(0,0) |
Sort% = FN_sortinit(0,0) |
||
Line 251: | Line 248: | ||
SWAP ?^col%,?(^col%+2) |
SWAP ?^col%,?(^col%+2) |
||
= col%</syntaxhighlight> |
= col%</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang=c>typedef unsigned int histogram_t; |
<syntaxhighlight lang="c">typedef unsigned int histogram_t; |
||
typedef histogram_t *histogram; |
typedef histogram_t *histogram; |
||
Line 262: | Line 258: | ||
luminance histogram_median(histogram h);</syntaxhighlight> |
luminance histogram_median(histogram h);</syntaxhighlight> |
||
<syntaxhighlight lang=c>histogram get_histogram(grayimage im) |
<syntaxhighlight lang="c">histogram get_histogram(grayimage im) |
||
{ |
{ |
||
histogram t; |
histogram t; |
||
Line 287: | Line 283: | ||
{{trans|Ada}} |
{{trans|Ada}} |
||
<syntaxhighlight lang=c>luminance histogram_median(histogram h) |
<syntaxhighlight lang="c">luminance histogram_median(histogram h) |
||
{ |
{ |
||
luminance From, To; |
luminance From, To; |
||
Line 309: | Line 305: | ||
An example of usage is the following code. |
An example of usage is the following code. |
||
<syntaxhighlight lang=c>#include <stdio.h> |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include "imglib.h" |
#include "imglib.h" |
||
Line 361: | Line 357: | ||
Which reads from the file specified from the command line and outputs to the standard out the PPM B/W version of the input image. The input image can be of any format handled by ImageMagick (see [[Read image file through a pipe]]) |
Which reads from the file specified from the command line and outputs to the standard out the PPM B/W version of the input image. The input image can be of any format handled by ImageMagick (see [[Read image file through a pipe]]) |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
{{libheader|opticl}} |
{{libheader|opticl}} |
||
<syntaxhighlight lang=lisp>(defpackage #:histogram |
<syntaxhighlight lang="lisp">(defpackage #:histogram |
||
(:use #:cl |
(:use #:cl |
||
#:opticl)) |
#:opticl)) |
||
Line 410: | Line 405: | ||
(bw-image (gray->black&white-image (color->gray-image image)))) |
(bw-image (gray->black&white-image (color->gray-image image)))) |
||
(write-pbm-file "lenna-bw.pbm" bw-image)))</syntaxhighlight> |
(write-pbm-file "lenna-bw.pbm" bw-image)))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Ada}} |
{{trans|Ada}} |
||
It uses the grayscale_image from the Grayscale image Task. The loaded frog image is from the Color quantization Task. |
It uses the grayscale_image from the Grayscale image Task. The loaded frog image is from the Color quantization Task. |
||
<syntaxhighlight lang=d>import grayscale_image; |
<syntaxhighlight lang="d">import grayscale_image; |
||
Color findSingleChannelMedian(Color)(in Image!Color img) |
Color findSingleChannelMedian(Color)(in Image!Color img) |
||
Line 461: | Line 455: | ||
.savePGM("quantum_frog_bin.pgm"); |
.savePGM("quantum_frog_bin.pgm"); |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|FBSL}}== |
=={{header|FBSL}}== |
||
FBSL volatiles and function call concatenation used heavily for brevity. |
FBSL volatiles and function call concatenation used heavily for brevity. |
||
Line 467: | Line 460: | ||
'''24-bpp P.O.T.-size BMP solution:''' |
'''24-bpp P.O.T.-size BMP solution:''' |
||
[[File:FBSLHistogram.PNG|right]] |
[[File:FBSLHistogram.PNG|right]] |
||
<syntaxhighlight lang=qbasic>#DEFINE WM_CLOSE 16 |
<syntaxhighlight lang="qbasic">#DEFINE WM_CLOSE 16 |
||
DIM colored = ".\LenaClr.bmp", grayscale = ".\LenaGry.bmp", blackwhite = ".\LenaBnw.bmp" |
DIM colored = ".\LenaClr.bmp", grayscale = ".\LenaGry.bmp", blackwhite = ".\LenaBnw.bmp" |
||
Line 517: | Line 510: | ||
FILEPUT(FILEOPEN(blackwhite, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save b/w image |
FILEPUT(FILEOPEN(blackwhite, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save b/w image |
||
END SUB</syntaxhighlight> |
END SUB</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<syntaxhighlight lang=forth>: histogram ( array gmp -- ) |
<syntaxhighlight lang="forth">: histogram ( array gmp -- ) |
||
over 256 cells erase |
over 256 cells erase |
||
dup bdim * over bdata + swap bdata |
dup bdim * over bdata + swap bdata |
||
do 1 over i c@ cells + +! loop drop ;</syntaxhighlight> |
do 1 over i c@ cells + +! loop drop ;</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
Line 529: | Line 520: | ||
'''Note''': ''luminance'' range is hard-encoded and is from 0 to 255. This could be enhanced. |
'''Note''': ''luminance'' range is hard-encoded and is from 0 to 255. This could be enhanced. |
||
<syntaxhighlight lang=fortran>module RCImageProcess |
<syntaxhighlight lang="fortran">module RCImageProcess |
||
use RCImageBasic |
use RCImageBasic |
||
implicit none |
implicit none |
||
Line 572: | Line 563: | ||
Example: |
Example: |
||
<syntaxhighlight lang=fortran>program BasicImageTests |
<syntaxhighlight lang="fortran">program BasicImageTests |
||
use RCImageBasic |
use RCImageBasic |
||
use RCImageIO |
use RCImageIO |
||
Line 614: | Line 605: | ||
end program BasicImageTests</syntaxhighlight> |
end program BasicImageTests</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
Histogram and Threshold functions are be added to the Grmap type for this task: |
Histogram and Threshold functions are be added to the Grmap type for this task: |
||
<syntaxhighlight lang=go>package raster |
<syntaxhighlight lang="go">package raster |
||
import "math" |
import "math" |
||
Line 642: | Line 632: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Demonstration program computes the median: |
Demonstration program computes the median: |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
// Files required to build supporting package raster are found in: |
// Files required to build supporting package raster are found in: |
||
Line 687: | Line 677: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
First, an implementation of a black-and-white instance of <tt>Color</tt>. For simplicty, we use ASCII PBM for output instead of the raw format. |
First, an implementation of a black-and-white instance of <tt>Color</tt>. For simplicty, we use ASCII PBM for output instead of the raw format. |
||
<syntaxhighlight lang=haskell>module Bitmap.BW(module Bitmap.BW) where |
<syntaxhighlight lang="haskell">module Bitmap.BW(module Bitmap.BW) where |
||
import Bitmap |
import Bitmap |
||
Line 724: | Line 713: | ||
Every instance of <tt>Color</tt> has a <tt>luminance</tt> method, so we don't need to convert an image to <tt>Gray</tt> to calculate its histogram. |
Every instance of <tt>Color</tt> has a <tt>luminance</tt> method, so we don't need to convert an image to <tt>Gray</tt> to calculate its histogram. |
||
<syntaxhighlight lang=haskell>import Bitmap |
<syntaxhighlight lang="haskell">import Bitmap |
||
import Bitmap.RGB |
import Bitmap.RGB |
||
import Bitmap.BW |
import Bitmap.BW |
||
Line 751: | Line 740: | ||
then (n + 1, left + l, right, ls, rL) |
then (n + 1, left + l, right, ls, rL) |
||
else (n, left, right + r, lL, rs)</syntaxhighlight> |
else (n, left, right + r, lL, rs)</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
Using <code>toGray</code> from [[Grayscale image#J|Grayscale image]]. |
Using <code>toGray</code> from [[Grayscale image#J|Grayscale image]]. |
||
<syntaxhighlight lang=j>getImgHist=: ([: /:~ ~. ,. #/.~)@, |
<syntaxhighlight lang="j">getImgHist=: ([: /:~ ~. ,. #/.~)@, |
||
medianHist=: {."1 {~ [: (+/\ I. -:@(+/)) {:"1 |
medianHist=: {."1 {~ [: (+/\ I. -:@(+/)) {:"1 |
||
toBW=: 255 * medianHist@getImgHist < toGray</syntaxhighlight> |
toBW=: 255 * medianHist@getImgHist < toGray</syntaxhighlight> |
||
Line 764: | Line 752: | ||
Use [http://rosettacode.org/mw/images/b/b6/Lenna100.jpg Lenna100.jpg] for testing (read using the [[j:Addons/media/platimg|media/platimg]] addon and convert to ppm file). |
Use [http://rosettacode.org/mw/images/b/b6/Lenna100.jpg Lenna100.jpg] for testing (read using the [[j:Addons/media/platimg|media/platimg]] addon and convert to ppm file). |
||
<syntaxhighlight lang=j> require 'media/platimg' |
<syntaxhighlight lang="j"> require 'media/platimg' |
||
'Lenna100.ppm' writeppm~ 256#.inv readimg 'Lenna100.jpg' |
'Lenna100.ppm' writeppm~ 256#.inv readimg 'Lenna100.jpg' |
||
786447</syntaxhighlight> |
786447</syntaxhighlight> |
||
Read ppm file, convert to black and white and write to a new ppm file using <code>writeppm</code>, <code>readppm</code> and <code>toColor</code> from the [[read ppm file#J | read]]/[[write ppm file#J|write ppm file]], and [[grayscale image#J|grayscale image]] solutions. |
Read ppm file, convert to black and white and write to a new ppm file using <code>writeppm</code>, <code>readppm</code> and <code>toColor</code> from the [[read ppm file#J | read]]/[[write ppm file#J|write ppm file]], and [[grayscale image#J|grayscale image]] solutions. |
||
<syntaxhighlight lang=j> 'Lenna100BW.ppm' writeppm~ toColor toBW readppm 'Lenna100.ppm' |
<syntaxhighlight lang="j"> 'Lenna100BW.ppm' writeppm~ toColor toBW readppm 'Lenna100.ppm' |
||
786447</syntaxhighlight> |
786447</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="java">import java.awt.image.BufferedImage; |
||
import java.io.File; |
import java.io.File; |
||
import java.io.IOException; |
import java.io.IOException; |
||
Line 836: | Line 823: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
<syntaxhighlight lang=julia>using Images, FileIO |
<syntaxhighlight lang="julia">using Images, FileIO |
||
ima = load("data/lenna50.jpg") |
ima = load("data/lenna50.jpg") |
||
Line 849: | Line 835: | ||
imb[imb .> medcol] = Gray(1.0) |
imb[imb .> medcol] = Gray(1.0) |
||
save("data/lennaGray.jpg", imb)</syntaxhighlight> |
save("data/lennaGray.jpg", imb)</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Uses the image from the [[Percentage difference between images]] task as an example. |
Uses the image from the [[Percentage difference between images]] task as an example. |
||
<syntaxhighlight lang=scala>// version 1.2.10 |
<syntaxhighlight lang="scala">// version 1.2.10 |
||
import java.io.File |
import java.io.File |
||
Line 913: | Line 898: | ||
ImageIO.write(image, "jpg", bwFile) |
ImageIO.write(image, "jpg", bwFile) |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
This solution uses functions defined at: |
This solution uses functions defined at: |
||
Line 920: | Line 904: | ||
[[Basic bitmap storage#Lua]], |
[[Basic bitmap storage#Lua]], |
||
[[Grayscale image#Lua]]. |
[[Grayscale image#Lua]]. |
||
<syntaxhighlight lang=lua>function Histogram( image ) |
<syntaxhighlight lang="lua">function Histogram( image ) |
||
local size_x, size_y = #image, #image[1] |
local size_x, size_y = #image, #image[1] |
||
Line 972: | Line 956: | ||
bitmap = ConvertToColorImage( gray_im ) |
bitmap = ConvertToColorImage( gray_im ) |
||
Write_PPM( "outputimage.ppm", bitmap )</syntaxhighlight> |
Write_PPM( "outputimage.ppm", bitmap )</syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="mathematica">ImageLevels[img]</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nim">import bitmap |
||
import grayscale_image |
import grayscale_image |
||
Line 1,029: | Line 1,011: | ||
# Save image as a PPM file. |
# Save image as a PPM file. |
||
image.writePPM("house_bw.ppm")</syntaxhighlight> |
image.writePPM("house_bw.ppm")</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
{{Trans|C}} |
{{Trans|C}} |
||
<syntaxhighlight lang=ocaml>type histogram = int array |
<syntaxhighlight lang="ocaml">type histogram = int array |
||
let get_histogram ~img:gray_channel = |
let get_histogram ~img:gray_channel = |
||
Line 1,048: | Line 1,029: | ||
;;</syntaxhighlight> |
;;</syntaxhighlight> |
||
<syntaxhighlight lang=ocaml>let histogram_median (h : histogram) = |
<syntaxhighlight lang="ocaml">let histogram_median (h : histogram) = |
||
let from = 0 and to_ = 255 in |
let from = 0 and to_ = 255 in |
||
Line 1,065: | Line 1,046: | ||
main: |
main: |
||
<syntaxhighlight lang=ocaml>let () = |
<syntaxhighlight lang="ocaml">let () = |
||
let img = read_ppm ~filename:"/tmp/foo.ppm" in |
let img = read_ppm ~filename:"/tmp/foo.ppm" in |
||
Line 1,089: | Line 1,070: | ||
output_ppm ~oc:stdout ~img:res; |
output_ppm ~oc:stdout ~img:res; |
||
;;</syntaxhighlight> |
;;</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
'''Using package''' [http://octave.sourceforge.net/image/index.html Image] |
'''Using package''' [http://octave.sourceforge.net/image/index.html Image] |
||
<syntaxhighlight lang=octave>function h = imagehistogram(imago) |
<syntaxhighlight lang="octave">function h = imagehistogram(imago) |
||
if ( isgray(imago) ) |
if ( isgray(imago) ) |
||
for j = 0:255 |
for j = 0:255 |
||
Line 1,144: | Line 1,124: | ||
ibw( img <= m ) = 0; |
ibw( img <= m ) = 0; |
||
jpgwrite("lennamed.jpg", ibw, 100);</syntaxhighlight> |
jpgwrite("lennamed.jpg", ibw, 100);</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Requires read_ppm() from [[Bitmap/Read_a_PPM_file#Phix|Read_a_PPM_file]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]]. <br> |
Requires read_ppm() from [[Bitmap/Read_a_PPM_file#Phix|Read_a_PPM_file]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]]. <br> |
||
Uses demo\rosetta\lena.ppm, included in the distribution, results may be verified with demo\rosetta\viewppm.exw |
Uses demo\rosetta\lena.ppm, included in the distribution, results may be verified with demo\rosetta\viewppm.exw |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="phix">-- demo\rosetta\Bitmap_Histogram.exw (runnable version) |
||
include ppm.e -- black, white, read_ppm(), write_ppm() (covers above requirements) |
include ppm.e -- black, white, read_ppm(), write_ppm() (covers above requirements) |
||
Line 1,187: | Line 1,166: | ||
img = to_bw(img) |
img = to_bw(img) |
||
write_ppm("LenaBW.ppm",img)</syntaxhighlight> |
write_ppm("LenaBW.ppm",img)</syntaxhighlight> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="php"> |
||
define('src_name', 'input.jpg'); // source image |
define('src_name', 'input.jpg'); // source image |
||
define('dest_name', 'output.jpg'); // destination image |
define('dest_name', 'output.jpg'); // destination image |
||
Line 1,259: | Line 1,237: | ||
</i> |
</i> |
||
</div> |
</div> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
{{trans|Forth}} |
{{trans|Forth}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picolisp">(de histogram (Pgm) |
||
(let H (need 256 0) |
(let H (need 256 0) |
||
(for L Pgm |
(for L Pgm |
||
Line 1,268: | Line 1,245: | ||
(inc (nth H (inc G))) ) ) |
(inc (nth H (inc G))) ) ) |
||
H ) )</syntaxhighlight> |
H ) )</syntaxhighlight> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
Also requires PureBasic solutions for [[Bitmap/Read_a_PPM_file#PureBasic|Read a PPM file]], [[Grayscale_image#PureBasic|Grayscale image]], and [[Bitmap/Write_a_PPM_file#PureBasic|Write a PPM file]]. |
Also requires PureBasic solutions for [[Bitmap/Read_a_PPM_file#PureBasic|Read a PPM file]], [[Grayscale_image#PureBasic|Grayscale image]], and [[Bitmap/Write_a_PPM_file#PureBasic|Write a PPM file]]. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="purebasic">Procedure getHistogram(image, Array histogram(1)) |
||
Protected w = ImageWidth(image) - 1 |
Protected w = ImageWidth(image) - 1 |
||
Protected h = ImageHeight(image) - 1 |
Protected h = ImageHeight(image) - 1 |
||
Line 1,334: | Line 1,310: | ||
SaveImageAsPPM(image, outputFile, 1) |
SaveImageAsPPM(image, outputFile, 1) |
||
EndIf</syntaxhighlight> |
EndIf</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Makes use of the Pillow library (PIL) you can install it using pip. The code is probably not the fastest or the image I used (1960x1960) is just too big. |
Makes use of the Pillow library (PIL) you can install it using pip. The code is probably not the fastest or the image I used (1960x1960) is just too big. |
||
<syntaxhighlight lang=python>from PIL import Image |
<syntaxhighlight lang="python">from PIL import Image |
||
# Open the image |
# Open the image |
||
Line 1,379: | Line 1,354: | ||
bw_image.show() |
bw_image.show() |
||
bm_image.show()</syntaxhighlight> |
bm_image.show()</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang=racket> #lang racket |
<syntaxhighlight lang="racket"> #lang racket |
||
(require racket/draw math/statistics racket/require |
(require racket/draw math/statistics racket/require |
||
(filtered-in |
(filtered-in |
||
Line 1,451: | Line 1,425: | ||
Sorry guys... I just give up on linking/displaying these images any other way! |
Sorry guys... I just give up on linking/displaying these images any other way! |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
Line 1,457: | Line 1,430: | ||
Uses pieces from [[Bitmap#Raku| Bitmap]], [[Bitmap/Write_a_PPM_file#Raku| Write a PPM file]] and [[Grayscale_image#Raku| Grayscale image]] tasks. Included here to make a complete, runnable program. |
Uses pieces from [[Bitmap#Raku| Bitmap]], [[Bitmap/Write_a_PPM_file#Raku| Write a PPM file]] and [[Grayscale_image#Raku| Grayscale image]] tasks. Included here to make a complete, runnable program. |
||
<syntaxhighlight lang=raku line>class Pixel { has UInt ($.R, $.G, $.B) } |
<syntaxhighlight lang="raku" line>class Pixel { has UInt ($.R, $.G, $.B) } |
||
class Bitmap { |
class Bitmap { |
||
has UInt ($.width, $.height); |
has UInt ($.width, $.height); |
||
Line 1,511: | Line 1,484: | ||
See [https://github.com/thundergnat/rc/blob/master/img/Lenna.png Lenna], and [https://github.com/thundergnat/rc/blob/master/img/Lenna-bw.png Lenna-bw] images. (converted to .png as .ppm format is not widely supported). |
See [https://github.com/thundergnat/rc/blob/master/img/Lenna.png Lenna], and [https://github.com/thundergnat/rc/blob/master/img/Lenna-bw.png Lenna-bw] images. (converted to .png as .ppm format is not widely supported). |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<syntaxhighlight lang=ruby>class Pixmap |
<syntaxhighlight lang="ruby">class Pixmap |
||
def histogram |
def histogram |
||
histogram = Hash.new(0) |
histogram = Hash.new(0) |
||
Line 1,554: | Line 1,526: | ||
Pixmap.open('file.ppm').save_as_blackandwhite('file_bw.ppm')</syntaxhighlight> |
Pixmap.open('file.ppm').save_as_blackandwhite('file_bw.ppm')</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
<syntaxhighlight lang=rust>extern crate image; |
<syntaxhighlight lang="rust">extern crate image; |
||
use image::{DynamicImage, GenericImageView, ImageBuffer, Rgba}; |
use image::{DynamicImage, GenericImageView, ImageBuffer, Rgba}; |
||
Line 1,625: | Line 1,596: | ||
} |
} |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
See also |
See also |
||
Line 1,632: | Line 1,602: | ||
* [[Read_ppm_file#Scala|Read a PPM File]] image loading |
* [[Read_ppm_file#Scala|Read a PPM File]] image loading |
||
<syntaxhighlight lang=scala>object BitmapOps { |
<syntaxhighlight lang="scala">object BitmapOps { |
||
def histogram(bm:RgbBitmap)={ |
def histogram(bm:RgbBitmap)={ |
||
val hist=new Array[Int](255) |
val hist=new Array[Int](255) |
||
Line 1,666: | Line 1,636: | ||
Usage: |
Usage: |
||
<syntaxhighlight lang=scala>val img=Pixmap.load("image.ppm").get |
<syntaxhighlight lang="scala">val img=Pixmap.load("image.ppm").get |
||
val hist=BitmapOps.histogram(img) |
val hist=BitmapOps.histogram(img) |
||
val mid=BitmapOps.histogram_median(hist); |
val mid=BitmapOps.histogram_median(hist); |
||
Line 1,677: | Line 1,647: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
Uses [[read ppm file#Tcl|readPPM]], [[grayscale image#Tcl|grayscale]] and [[write ppm file#Tcl|output_ppm]] from other pages. |
Uses [[read ppm file#Tcl|readPPM]], [[grayscale image#Tcl|grayscale]] and [[write ppm file#Tcl|output_ppm]] from other pages. |
||
<syntaxhighlight lang=tcl>package require Tcl 8.5 |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
package require Tk |
package require Tk |
||
Line 1,731: | Line 1,700: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
The input image is in edit buffer pointed by numeric register #15. |
The input image is in edit buffer pointed by numeric register #15. |
||
On return, #30 points to buffer containing histogram data. |
On return, #30 points to buffer containing histogram data. |
||
The histogram data is given as ASCII decimal values, one value per line. |
The histogram data is given as ASCII decimal values, one value per line. |
||
<syntaxhighlight lang=vedit>:HISTOGRAM: |
<syntaxhighlight lang="vedit">:HISTOGRAM: |
||
#30 = Buf_Free // #30 = buffer to store histogram data |
#30 = Buf_Free // #30 = buffer to store histogram data |
||
for (#9=0; #9<256; #9++) { |
for (#9=0; #9<256; #9++) { |
||
Line 1,746: | Line 1,714: | ||
} |
} |
||
Return</syntaxhighlight> |
Return</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|DOME}} |
{{libheader|DOME}} |
||
<syntaxhighlight lang=ecmascript>import "dome" for Window |
<syntaxhighlight lang="ecmascript">import "dome" for Window |
||
import "graphics" for Canvas, Color, ImageData |
import "graphics" for Canvas, Color, ImageData |
||
Line 1,834: | Line 1,801: | ||
var Game = ImageHistogram.new("Lenna100.jpg", "Lenna100_B&W.png")</syntaxhighlight> |
var Game = ImageHistogram.new("Lenna100.jpg", "Lenna100_B&W.png")</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|C}} |
{{trans|C}} |
||
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
||
<syntaxhighlight lang=zkl>fcn histogram(image){ |
<syntaxhighlight lang="zkl">fcn histogram(image){ |
||
hist:=List.createLong(256,0); // array[256] of zero |
hist:=List.createLong(256,0); // array[256] of zero |
||
image.data.howza(0).pump(Void,'wrap(c){ hist[c]+=1 }); // byte by byte loop |
image.data.howza(0).pump(Void,'wrap(c){ hist[c]+=1 }); // byte by byte loop |
||
Line 1,852: | Line 1,818: | ||
from |
from |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
<syntaxhighlight lang=zkl>img:=PPM.readPPMFile("lenaGrey.ppm"); // a grey scale image |
<syntaxhighlight lang="zkl">img:=PPM.readPPMFile("lenaGrey.ppm"); // a grey scale image |
||
median:=histogramMedian(histogram(img)); |
median:=histogramMedian(histogram(img)); |
||
median.println(); |
median.println(); |
||
Line 1,865: | Line 1,831: | ||
See the BBC Basic entry or: |
See the BBC Basic entry or: |
||
http://www.zenkinetic.com/Images/RosettaCode/lenaBW.jpg |
http://www.zenkinetic.com/Images/RosettaCode/lenaBW.jpg |
||
{{omit from|AWK}} |
{{omit from|AWK}} |
||
{{omit from|PARI/GP}} |
{{omit from|PARI/GP}} |