Create CSS sprites based on colour? - css

I have a large set of thumbnails I wish to display on a page (over 200). I'd like to use CSS sprites to load them to minimise the HTTP requests. I think putting all of them in one massive file is a bad idea, but splitting them into about 6 files of 40-50 thumbnails should work nicely.
All of the thumbnails are fairly low colour (can be reduced to 256 colours without quality drop), but in total all the thumbnails cover a lot more colours.
So, is there an easy way to group them based on their colour? Putting each group of files in separate folder is fine, since I can stitch together with ImageMagick or an online sprite tool later. But doing all of that in one (with CSS) would be nice too.
Update: the reason for grouping by colour:
The idea is to save more bandwidth. If I have 10 mostly-blue thumbnails, 10 green and 10 red, I can combine them to 3 images, reducing each to 256 colours. If I mix thumbnails then reducing to 256 colours makes the images poorer quality.

Firstly, I would suggest not worrying too much, and saving as a 24bit png. It may seem that the image is getting a lot bigger by doing this, but if the thumbnails are small you'll probably find that there is a large amount of bandwidth currently being using with just http headers that will go away that you can use to make your images look better.
However, if you want automate the process you could try working out the average colour (one way of doing something close to this is to resize them to 1x1, then look at the rgb colour for that pixel). Once you have a colour per images, convert to hsv and sort by hue. You can then bundle them based on that sort order. I've not actually tried this, but it may produce acceptable results.
Adjusting the number of images that get bundled will also effect the output quality. If it sucks when you put 30 images per file, try 25 and see how much difference it makes. Actually, might be smarter to think about the number of files...
Put them all into a single file.
Does it look bad, as there aren't enough colours?
add one extra file and split them equally across all the files. Goto step 2.

Well I did some testing by grabbing a sample by hand of one "tint" and comparing it to a montage created by just taking the first N images. There was only a difference of a few kilobytes, which was reduced to about 30 bytes after I found PNGcrush. Fanastic tool!
So in short, my crackpot idea has been disproven. :p

Now, this is nothing more than theoretical blabbering, but, I understand that animated GIFs have support for a distinct color palette per frame. Theoretically, you could place each image on a separate frame of the animation (leaving most of that frame transparent), and set the pause time between frames to 1ms. Set the animation to only go through once, and you could (potentially) have an effective CSS sprite with reduced to 256 colors per image.
Like I said, theoretical blabbering.

Related

How to decided between font size, margins and png() parameters to achieve good definition and consistent visualisation?

This is a question that has me banging my head against a wall for a while now. Much of R coding produces consistent results when used for analysis, in a sense that sometimes there are more than one ways to achieve something but your output would be something shareable and consistent. Let's say a dataframe or a datatable and so on and so forth.
However, I'm finding myself struggling to understand how can I achieve a mainstreamed process when generating plots. Font size, margin size, height, width and resolution. All those influence each other.
You change your resolution and suddenly your font size changes drastically when saving with png(). You go back and you change the dimensions and there you are with extremely small font size or with a pixeled chart looking at you.
So, because I still trust in the ggplot and png() process and believe that it must be me that messes up or doesn't do the correct steps in his workflow the question is:
What is the sweet point between all those factors that makes plotting with R easy, consistent and high-quality?
I understand that some of these factors cannot be standardised since it depends on the amount of information and how complex a chart is. But how do others ensure consistent font size against changes in resolution, height, width and plot margins?
I've came across some useful resources such as:
[https://blog.revolutionanalytics.com/2009/01/10-tips-for-making-your-r-graphics-look-their-best.html][1]
[https://support.rstudio.com/hc/en-us/articles/200488548-Problem-with-Plots-or-Graphics-Device][1]
But none really speaks to how you mainstream a visualization process in R. Still great tips though.
Any advice or ideas are honestly appreciated. Thank you.

How to use JuliaImages to create a smaller image given a starting image?

I just got done exploring the docs for JuliaImages found here. What I want to do is as follows:
I have an image. It is a map of sorts. It takes up a lot of space so I want to index into the image and create a new smaller image that is just essencially a zoomed-in version of the original image. I know I could do this manually, but I want to create a re-usable script that I can use to apply this operation to N number of images. How can I do this using JuliaImages?
If by "zoomed in" you mean focusing on a small portion of the image and making it look bigger, you can do this with ordinary array-indexing tools. For example, img[251:500,147:328] would extract a portion of the image.
If what you're really looking for is a thumbnail, my favorite approach is to use restrict. That is limited to 2-fold reductions. You can also imfilter (best with the IIRGaussian filters of ImageFiltering.KernelFactors) and then call imresize. But there will be no beating the performance of restrict.

How reduce the noise of a image?

I am loading text from some images. With some of them, I am having problems, with this type of image
library(magick)
library(tesseract)
image_read(fichero.jpg) %>%
tesseract::ocr(engine = tesseract("eng")) %>%
cat()
Result
I am assuming (correct me if not) that tesseract fail because of the low quality of the image (it is a scanned document), and I donĀ“t know if there is a way to make the image better.
I tried also some convultion methods with several kernels, trying to reduce the noise of the photo, but it was worse.
Is there a way to handle this or I have to assume that is not possible to get the text in this quality-images?
Regards
Looking at this with the experience of a photographer rather than as a programmer, I would guess that the poor focus and camera jiggle make this image pretty well unreadable by most OCR options. I just used the OCR in Adobe Acrobat to play with it on my own PC and I could get "FECHA" to recognize, but not "NUMERO" and not any of the numbers.
I pulled it into a photo editor and messed around with the contrast, as sometimes it's possible to convert a grayscale image such as this to pure black-and-white and get rid of some of the fuzziness, but I couldn't produce a readable image in my quick-and-dirty experiment.
So realistically, you'll need images that are scanned/photographed with higher resolution and better contrast to get reliable OCR.
It looks like you are trying to create a cow from ground beef. The big problem is that JPEG is not suited for this type of non-photographic image. Your png looks fine because it is a lossless format.
If you don't want this problem, do not save the files as JPEG.

GIMP batch editing (Script-fu/Python-fu)

I have about 500 images, I would like edit all of them in batch, I need to resize them all at to 190x120 dimensions, position then slightly higher (say 10 pixels). And export. Also I would like them all to keep their initial names.
Basically I have a frame and I would like to load images (on layer under it) then size them down (above dimensions) move slightly up and export each individual image with frame so that it keeps its name.
What would be a command I could use in GIMP console (script-fu or Python-fu)?
Or what other method could I use to achieve the result without editing each image individually?
Thanks in advance!
i altered one of my own python-fu scripts, and after some testing it seems to do what you want. the interface itself is pretty self-explanatory.
just download the file and install it as a plug-in.
gist link: https://gist.github.com/MCOfficer/bdf6c0c0935d22da38e72cc99fea6375 (archive.md)

Reduce pdf file size of plot in R

i am plotting some data in R using the following commands:
jj = ts(read.table("overlap.txt"))
pdf(file = "plot.pdf")
plot(jj, ylab="", main="")
dev.off()
The result looks like this:
The problem I have is that the pdf file that I get is quite big (25Mb). Is the a way to reduce the file size? JPEG is not an option because I need a vector graphic.
Take a look at tools::compactPDF - you need to have either qpdf or ghostscript installed, but it can make a huge difference to pdf file size.
If reading a PDF file from disk, there are 3 options for GostScript quality (gs_quality), as indicated in the R help file:
printer (300dpi)
ebook (150dpi)
screen (72dpi)
The default is none. For example to convert all PDFs in folder mypdfs/ to ebook quality, use the command
tools::compactPDF('mypdfs/', gs_quality='ebook')
You're drawing a LOT of lines or points. Vector image formats such as pdf, ps, eps, svg, etc. maintain logical information about all of those points, lines, or other items that increase complexity, which translates to size and drawing time, as the number of points increases. Generally vector images are the best in a number of ways, most compact, scale best, and highest quality reproduction. But, if the number of graphical elements becomes very large then it's often best to go to a raster image format such as png. When you switch to raster it's best to have a good idea what size image you want, both in pixels and also in things like print measurements, in order to produce the best image.
For information from the other direction, too large a raster image, see this answer.
One way of reducing the file size is to reduce the number of values that you have. Assuming you have a dataframe called df:
# take sample of data from dataframe
sampleNo = 10000
sampleData <- df[sample(nrow(df), sampleNo), ]
I think the only other alternative within R is to produce a non-vector. Outside of R you could use Acrobat Professional (which is not free) to optimize the pdf. This can reduce the file size enormously.
Which version of R are you using? In R 2.14.0, pdf() has an argument compress to support compression. I'm not sure how much it can help you, but there are also other tools to compress PDF files such as Pdftk and qpdf. I have two wrappers for them in the animation package, but you may want to use command line directly.
Hard to tell without seeing what the plot looks like - post a screenshot?
I suspect its a lot of very detailed lines and most of the information probably isn't visible - lots of things overlapping or very very small detail. Try thinning your data in one dimension or another. I doubt you'll lose visible information.

Resources